feat(events/bot): adding the scrap of users at the start
- Also adding the connection with a status but not a major issue
This commit is contained in:
parent
a26a21964d
commit
6f7fd043d2
1 changed files with 109 additions and 3 deletions
|
|
@ -1,14 +1,75 @@
|
||||||
use serenity::all::*;
|
use serenity::all::*;
|
||||||
|
use sqlx::PgPool;
|
||||||
use crate::commands::SlashCommand;
|
use crate::commands::SlashCommand;
|
||||||
use crate::events::{BotEvent, EventEntry};
|
use crate::database::{guild, user, guild_user, bot};
|
||||||
|
use crate::events::{
|
||||||
|
BotEvent,
|
||||||
|
EventEntry
|
||||||
|
};
|
||||||
|
use crate::models::DbBot;
|
||||||
|
use crate::models::bot::{
|
||||||
|
BotActivity,
|
||||||
|
BotPresence
|
||||||
|
};
|
||||||
|
|
||||||
pub struct ReadyHandler;
|
pub struct ReadyHandler;
|
||||||
|
|
||||||
|
async fn fetch_all_members(
|
||||||
|
ctx: &Context,
|
||||||
|
guild_id: GuildId,
|
||||||
|
) -> Result<Vec<Member>, serenity::Error> {
|
||||||
|
let mut all_members: Vec<Member> = Vec::new();
|
||||||
|
let mut last_id: Option<UserId> = None;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let batch: Vec<Member> = guild_id.members(ctx, Some(1000), last_id).await?;
|
||||||
|
|
||||||
|
if batch.is_empty() {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_id = Some(batch.last().unwrap().user.id);
|
||||||
|
let count: usize = batch.len();
|
||||||
|
all_members.extend(batch);
|
||||||
|
|
||||||
|
if count < 1000 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(all_members)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async fn bot_activity(ctx: &Context, db: &PgPool) {
|
||||||
|
if let Some(config) = bot::get(db).await.expect("Erreur bot::get") {
|
||||||
|
let activity: ActivityData = match config.activity_type {
|
||||||
|
BotActivity::Playing => ActivityData::playing(&config.status),
|
||||||
|
BotActivity::Watching => ActivityData::watching(&config.status),
|
||||||
|
BotActivity::Listening => ActivityData::listening(&config.status),
|
||||||
|
BotActivity::Competing => ActivityData::competing(&config.status),
|
||||||
|
BotActivity::Streaming => ActivityData::streaming(&config.status, "https://twitch.tv/EniumRaphael")
|
||||||
|
.expect("ERROR STREAMING"),
|
||||||
|
};
|
||||||
|
|
||||||
|
let presence: OnlineStatus = match config.presence {
|
||||||
|
BotPresence::Online => OnlineStatus::Online,
|
||||||
|
BotPresence::Idle => OnlineStatus::Idle,
|
||||||
|
BotPresence::Dnd => OnlineStatus::DoNotDisturb,
|
||||||
|
BotPresence::Invisible => OnlineStatus::Invisible,
|
||||||
|
};
|
||||||
|
|
||||||
|
ctx.set_presence(Some(activity), presence);
|
||||||
|
println!("🎮 Status: \"{}\" | {:?} | {:?}", config.status, config.activity_type, config.presence);
|
||||||
|
} else {
|
||||||
|
println!("⚠️ Aucune config bot en DB — table `bots` vide ?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[serenity::async_trait]
|
#[serenity::async_trait]
|
||||||
impl BotEvent for ReadyHandler {
|
impl BotEvent for ReadyHandler {
|
||||||
fn event_type(&self) -> &'static str { "ready" }
|
fn event_type(&self) -> &'static str { "ready" }
|
||||||
|
|
||||||
async fn on_ready(&self, ctx: &Context, ready: &Ready, commands: &[Box<dyn SlashCommand>]) {
|
async fn on_ready(&self, ctx: &Context, ready: &Ready, commands: &[Box<dyn SlashCommand>], db: &PgPool) {
|
||||||
println!("TTY is now running as: '{}'", ready.user.name);
|
println!("TTY is now running as: '{}'", ready.user.name);
|
||||||
|
|
||||||
let cmds: Vec<CreateCommand> = commands.iter().map(|c| c.register()).collect();
|
let cmds: Vec<CreateCommand> = commands.iter().map(|c| c.register()).collect();
|
||||||
|
|
@ -16,7 +77,52 @@ impl BotEvent for ReadyHandler {
|
||||||
.await
|
.await
|
||||||
.expect("❌ | Cannot register commands");
|
.expect("❌ | Cannot register commands");
|
||||||
|
|
||||||
println!("\nTTY now running with {} commands loaded", commands.len());
|
println!("TTY now running with {} commands loaded\n", commands.len());
|
||||||
|
|
||||||
|
bot_activity(ctx, &db).await;
|
||||||
|
|
||||||
|
println!("Synchronizing {} guilds", ready.guilds.len());
|
||||||
|
|
||||||
|
let mut count: i32 = 0;
|
||||||
|
|
||||||
|
for unavailable_guild in &ready.guilds {
|
||||||
|
let guild_id: GuildId = unavailable_guild.id;
|
||||||
|
let guild_id_str: String = guild_id.to_string();
|
||||||
|
|
||||||
|
if let Err(e) = guild::get_or_create(&db, &guild_id_str).await {
|
||||||
|
eprintln!(" ❌ Guild {} — {}", guild_id, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let members: Vec<Member> = match fetch_all_members(ctx, guild_id).await {
|
||||||
|
Ok(m) => m,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!(" ❌ Guild {} — fetch members: {}", guild_id, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
println!("\t✅ | Guild {}", guild_id);
|
||||||
|
for member in &members {
|
||||||
|
if member.user.bot {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let member_id: String = member.user.id.to_string();
|
||||||
|
if let Err(e) = user::get_or_create(&db, &member_id).await {
|
||||||
|
eprintln!("\t\t❌ | User {} — {}", member_id, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if let Err(e) = guild_user::get_or_create(&db, &member_id, &guild_id_str).await {
|
||||||
|
eprintln!("\t\t❌ | GuildUser {}/{} — {}", guild_id, member_id, e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
println!("\t\t✅ | Member {}", guild_id);
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("🚀 | Synchronization complete! {} users registered", count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue