From 4106f2f99c89686b429709ac5ecefe3f2ad0eed0 Mon Sep 17 00:00:00 2001 From: syzygial Date: Fri, 3 Mar 2023 17:30:00 -0500 Subject: [PATCH] ReOrg: TODO: Init per guild join timer --- src/commands.rs | 7 +++++ src/commands/ping.rs | 11 +++++++ src/events.rs | 23 +++++++++++++++ src/main.rs | 70 ++++++++++---------------------------------- 4 files changed, 56 insertions(+), 55 deletions(-) create mode 100644 src/commands.rs create mode 100644 src/commands/ping.rs create mode 100644 src/events.rs diff --git a/src/commands.rs b/src/commands.rs new file mode 100644 index 0000000..68070e2 --- /dev/null +++ b/src/commands.rs @@ -0,0 +1,7 @@ +use serenity::framework::standard::macros::{group}; +pub mod ping; +use ping::*; + +#[group] +#[commands(ping)] +pub struct General; diff --git a/src/commands/ping.rs b/src/commands/ping.rs new file mode 100644 index 0000000..5b9f031 --- /dev/null +++ b/src/commands/ping.rs @@ -0,0 +1,11 @@ +use serenity::framework::standard::macros::command; +use serenity::framework::standard::CommandResult; +use serenity::model::channel::Message; +use serenity::prelude::Context; + +#[command] +pub async fn ping(ctx: &Context, msg: &Message) -> CommandResult { + msg.channel_id.say(&ctx.http, "pong!").await?; + + Ok(()) +} diff --git a/src/events.rs b/src/events.rs new file mode 100644 index 0000000..4f691a3 --- /dev/null +++ b/src/events.rs @@ -0,0 +1,23 @@ +use serenity::async_trait; +use serenity::model::gateway::Ready; +use serenity::prelude::Context; +use serenity::prelude::*; + +pub struct Handler; + +#[async_trait] +impl EventHandler for Handler { + // Set a handler to be called on the `ready` event. This is called when a + // shard is booted, and a READY payload is sent by Discord. This payload + // contains data like the current user's guild Ids, current user data, + // private channels, and more. + // + // In this case, just print what the current user's username is. + async fn ready(&self, ctx: Context, ready: Ready) { + println!("{} is connected!", ready.user.name); + for g_id in ctx.cache.guilds() { + let g = ctx.cache.guild(g_id).expect("Unable to fetch guild"); + println!("Initializing {}...", g.name); + } + } +} diff --git a/src/main.rs b/src/main.rs index ed0d08b..350f5cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,65 +2,28 @@ use std::env; -use serenity::async_trait; -use serenity::model::channel::Message; -use serenity::model::gateway::Ready; use serenity::prelude::*; -use serenity::framework::standard::macros::{command, group}; -use serenity::framework::standard::{CommandResult, StandardFramework}; +use serenity::framework::standard::{StandardFramework}; -#[command] -async fn about(ctx: &Context, msg: &Message) -> CommandResult { - msg.channel_id.say(&ctx.http, "A simple test bot").await?; +// This trait adds the `register_songbird` and `register_songbird_with` methods +// to the client builder below, making it easy to install this voice client. +// The voice client can be retrieved in any command using `songbird::get(ctx).await`. +use songbird::SerenityInit; - Ok(()) -} +// Import the `Context` to handle commands. +use serenity::client::Context; -#[command] -async fn ping(ctx: &Context, msg: &Message) -> CommandResult { - msg.channel_id.say(&ctx.http, "pong!").await?; - Ok(()) -} +mod commands; +use commands::*; -#[group] -#[commands(about, ping)] -struct General; - -struct Handler; - -#[async_trait] -impl EventHandler for Handler { - // Set a handler for the `message` event - so that whenever a new message - // is received - the closure (or function) passed will be called. - // - // Event handlers are dispatched through a threadpool, and so multiple - // events can be dispatched simultaneously. - async fn message(&self, ctx: Context, msg: Message) { - if msg.content == "!ping" { - // Sending a message can fail, due to a network error, an - // authentication error, or lack of permissions to post in the - // channel, so log to stdout when some error happens, with a - // description of it. - if let Err(why) = msg.channel_id.say(&ctx.http, "Pong!").await { - println!("Error sending message: {:?}", why); - } - } - } - - // Set a handler to be called on the `ready` event. This is called when a - // shard is booted, and a READY payload is sent by Discord. This payload - // contains data like the current user's guild Ids, current user data, - // private channels, and more. - // - // In this case, just print what the current user's username is. - async fn ready(&self, _: Context, ready: Ready) { - println!("{} is connected!", ready.user.name); - } -} +mod events; +use events::*; #[tokio::main] async fn main() { + // Trace async functions + tracing_subscriber::fmt::init(); // Configure the client with your Discord bot token in the environment. let token = env::var("DISCORD_TOKEN").expect("Expected a token in the environment"); // Set gateway intents, which decides what events the bot will be notified about @@ -74,17 +37,14 @@ async fn main() { | GatewayIntents::MESSAGE_CONTENT; let framework = StandardFramework::new() - .configure(|c| c.prefix("~")) - // The `#[group]` (and similarly, `#[command]`) macro generates static instances - // containing any options you gave it. For instance, the group `name` and its `commands`. - // Their identifiers, names you can use to refer to these instances in code, are an - // all-uppercased version of the `name` with a `_GROUP` suffix appended at the end. + .configure(|c| c.prefix("ALAN! ")) .group(&GENERAL_GROUP); let mut client = Client::builder(&token, intents) .event_handler(Handler) .framework(framework) + .register_songbird() .await.expect("Err creating client"); if let Err(why) = client.start().await {