Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add basic discord name -> discord user lookup #65

Open
wants to merge 2 commits into
base: live-elo
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion live-elo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ clients = { path = "../clients/" }
bililive = { git = "https://github.com/vanorsigma/bililive-rs.git", rev = "e15f7b9" }
futures = { workspace = true }
hubbub = "0.10.1"
sqlx = { version = "0.8.2", features = ["sqlite", "runtime-tokio"] }
sqlx = { version = "0.8.2", features = ["sqlite", "runtime-tokio", "time"] }
twilight-http = "0.15.4"
thiserror = { workspace = true }
time = "0.3.37"
twilight-model = "0.15.4"
100 changes: 100 additions & 0 deletions live-elo/src/converter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::time::Duration;

use sqlx::{
sqlite::{SqliteConnectOptions, SqliteJournalMode},
SqlitePool,
};
use thiserror::Error;

const SCHEMA: &'static str = r#"
CREATE TABLE IF NOT EXISTS discord_username_cache(
user_id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
last_updated_at TIMESTAMP
);
"#;

pub struct Lookup {
pool: SqlitePool,
http: twilight_http::Client,
config: LookupConfig,
}

impl Lookup {
pub async fn new(
discord_token: impl Into<String>,
config: LookupConfig,
) -> Result<Self, LookupError> {
let options = SqliteConnectOptions::new()
.filename("discord_lookup.db")
.create_if_missing(true)
.journal_mode(SqliteJournalMode::Wal);

let pool = SqlitePool::connect_with(options).await?;

sqlx::query(SCHEMA).execute(&pool).await?;

Ok(Self {
pool,
http: twilight_http::Client::new(discord_token.into()),
config,
})
}

pub async fn get_user_by_name(&self, name: &str) -> Result<Option<String>, LookupError> {
let result: Option<(i64,)> =
sqlx::query_as("SELECT user_id FROM discord_username_cache WHERE username = ?")
.bind(name)
.fetch_optional(&self.pool)
.await?;

Ok(result.map(|(id,)| id.to_string()))
}

pub async fn add_user(&self, id: &str) -> Result<(), LookupError> {
let id: twilight_model::id::Id<_> = id.parse()?;
let id_raw: i64 = id
.get()
.try_into()
.expect("somehow we got a really big discord id");

let last_updated: Option<(time::OffsetDateTime,)> =
sqlx::query_as("SELECT last_updated_at FROM discord_username_cache WHERE user_id = ?")
.bind(id_raw)
.fetch_optional(&self.pool)
.await?;

if let Some((last_updated,)) = last_updated {
if last_updated + self.config.max_age > time::OffsetDateTime::now_utc() {
// record has not yet expired, so don't refresh the data
return Ok(());
}
}

let model = self.http.user(id).await?.model().await?;

sqlx::query("INSERT OR REPLACE INTO discord_username_cache(user_id, username, last_updated_at) VALUES (?, ?, ?)").bind(id_raw).bind(match model.discriminator {
0 => model.name,
_ => format!("{}#{}", model.name, model.discriminator()),
}).bind(time::OffsetDateTime::now_utc()).execute(&self.pool).await?;

Ok(())
}
}

#[derive(Debug, Clone)]
pub struct LookupConfig {
pub max_age: Duration,
}

#[derive(Debug, Error)]
pub enum LookupError {
#[error("something went wrong with discord: {0:?}")]
Twilight(#[from] twilight_http::Error),
#[error("something went wrong with sqlite: {0:?}")]
Sqlx(#[from] sqlx::Error),
#[error("failed to parse discord user id: {0:?}")]
DiscordId(#[from] std::num::ParseIntError),
#[error("failed to deserialize discord body: {0:?}")]
DeserializeFailed(#[from] twilight_http::response::DeserializeBodyError),
}
1 change: 1 addition & 0 deletions live-elo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod converter;
pub mod exporter;
pub mod filter;
pub mod performances;
Expand Down
146 changes: 73 additions & 73 deletions live-elo/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ mod config;

use ::app_state::create_app_state;
use config::GLOBAL_CONFIG;
use lbo::{performances::{LeaderboardFilter, StandardLeaderboard}, Pipeline};
use lbo::{
performances::{LeaderboardFilter, StandardLeaderboard},
Pipeline,
};
use live_elo::{
exporter::{
persistence::UnconnectedDatabaseManager,
shared_processor::SharedHandle,
DummyExporter,
MultiExporter,
timed_committer::UnstartedTimedLeaderboardCommitter
persistence::UnconnectedDatabaseManager, shared_processor::SharedHandle,
timed_committer::UnstartedTimedLeaderboardCommitter, DummyExporter, MultiExporter,
},
filter::DummyFilter,
performances::FanoutPerformances,
Expand All @@ -19,17 +19,14 @@ use live_elo::{
raid::RaidScoring,
subs::SubCountScoring,
text::{DiscordTextScoring, TwitchTextScoring},
MessageCountScoring,
MultiScorer
MessageCountScoring, MultiScorer,
},
sources::{
bilibili::B2MessageSourceHandle,
discord::{DiscordHandleOptions, DiscordMessageSourceHandle},
emote::EmoteExtractingSource,
twitch::TwitchMessageSourceHandle,
CancellableSource,
Message,
TokioTaskSource
CancellableSource, Message, TokioTaskSource,
},
};
use std::{str::FromStr, sync::Arc, time::Duration};
Expand Down Expand Up @@ -66,11 +63,16 @@ async fn main() {
};

let db_manager = UnconnectedDatabaseManager::new(
GLOBAL_CONFIG.database_filename.clone()
.unwrap_or_else(|| "./leaderboard.db".to_string())
GLOBAL_CONFIG
.database_filename
.clone()
.unwrap_or_else(|| "./leaderboard.db".to_string()),
);

let db_manager = db_manager.connect().await.expect("should connect to database");
let db_manager = db_manager
.connect()
.await
.expect("should connect to database");

let leaderboard_names = vec![
"message_count",
Expand Down Expand Up @@ -99,11 +101,7 @@ async fn main() {

let shared_handle = SharedHandle::new(
Arc::new(loaded_leaderboards),
Duration::from_secs(
GLOBAL_CONFIG
.perf_expiry_mins
.unwrap_or(1) * 60,
),
Duration::from_secs(GLOBAL_CONFIG.perf_expiry_mins.unwrap_or(1) * 60),
);

let websocket_server =
Expand All @@ -117,7 +115,10 @@ async fn main() {
)
.await;

let timed_committer = UnstartedTimedLeaderboardCommitter::new(shared_handle.clone(), std::time::Duration::from_secs(10));
let timed_committer = UnstartedTimedLeaderboardCommitter::new(
shared_handle.clone(),
std::time::Duration::from_secs(10),
);

let mut tokio_task_builder = TokioTaskSource::builder();
let mut fanout_performances =
Expand Down Expand Up @@ -205,78 +206,77 @@ async fn main() {
EmoteCountScoring::new(),
MultiExporter::pair(
DummyExporter::new(),
shared_handle.create_consumer_for_leaderboard(
LeaderboardName::new("emote".to_string())
)).append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("emote".to_string()),
shared_handle.clone(),
)),
shared_handle
.create_consumer_for_leaderboard(LeaderboardName::new("emote".to_string())),
)
.append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("emote".to_string()),
shared_handle.clone(),
)),
app_state.clone(),
))
.add_performance_processor(StandardLeaderboard::new(
MultiScorer::pair(
MessageCountScoring::new(),
TwitchTextScoring::new(),
).append(SubCountScoring::new())
.append(BitsCountScoring::new())
.append(RaidScoring::new()),
MultiScorer::pair(MessageCountScoring::new(), TwitchTextScoring::new())
.append(SubCountScoring::new())
.append(BitsCountScoring::new())
.append(RaidScoring::new()),
MultiExporter::pair(
DummyExporter::new(),
shared_handle.create_consumer_for_leaderboard(
LeaderboardName::new("overall".to_string()),
)).append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("overall".to_string()),
shared_handle.clone(),
shared_handle.create_consumer_for_leaderboard(LeaderboardName::new(
"overall".to_string(),
)),
)
.append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("overall".to_string()),
shared_handle.clone(),
)),
app_state.clone(),
))
.add_performance_processor(LeaderboardFilter::new(
StandardLeaderboard::new(
MultiScorer::pair(
MessageCountScoring::new(),
TwitchTextScoring::new(),
).append(SubCountScoring::new())
.append(BitsCountScoring::new())
.append(RaidScoring::new()),
MultiScorer::pair(MessageCountScoring::new(), TwitchTextScoring::new())
.append(SubCountScoring::new())
.append(BitsCountScoring::new())
.append(RaidScoring::new()),
MultiExporter::pair(
DummyExporter::new(),
shared_handle.create_consumer_for_leaderboard(
LeaderboardName::new("non-vips".to_string()),
)).append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("non-vips".to_string()),
shared_handle.clone(),
shared_handle.create_consumer_for_leaderboard(LeaderboardName::new(
"non-vips".to_string(),
)),
app_state.clone()),
|message| {
match message.as_ref() {
Message::Twitch(message) => !message.special_role,
_ => false,
}
}
)
.append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("non-vips".to_string()),
shared_handle.clone(),
)),
app_state.clone(),
),
|message| match message.as_ref() {
Message::Twitch(message) => !message.special_role,
_ => false,
},
))
.add_performance_processor(LeaderboardFilter::new(
StandardLeaderboard::new(
MultiScorer::pair(
MessageCountScoring::new(),
TwitchTextScoring::new(),
).append(SubCountScoring::new())
.append(BitsCountScoring::new())
.append(RaidScoring::new()),
MultiScorer::pair(MessageCountScoring::new(), TwitchTextScoring::new())
.append(SubCountScoring::new())
.append(BitsCountScoring::new())
.append(RaidScoring::new()),
MultiExporter::pair(
DummyExporter::new(),
shared_handle.create_consumer_for_leaderboard(
LeaderboardName::new("partner".to_string()),
)).append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("partner".to_string()),
shared_handle.clone(),
shared_handle.create_consumer_for_leaderboard(LeaderboardName::new(
"partner".to_string(),
)),
app_state.clone()),
|message| {
match message.as_ref() {
Message::Twitch(message) => message.partner,
_ => false,
}
}
)
.append(db_manager.create_consumer_for_leaderboard(
LeaderboardName::new("partner".to_string()),
shared_handle.clone(),
)),
app_state.clone(),
),
|message| match message.as_ref() {
Message::Twitch(message) => message.partner,
_ => false,
},
));
}

Expand Down