Skip to content

Commit

Permalink
feat: add event handler.
Browse files Browse the repository at this point in the history
this is basically a 1:1 copy of the event handler used in the old
codebase but without all the database stuff or the unnecessary event
monitoring.
  • Loading branch information
evieluvsrainbows committed Jun 19, 2024
1 parent 7dba962 commit 5295f04
Show file tree
Hide file tree
Showing 5 changed files with 221 additions and 1 deletion.
157 changes: 157 additions & 0 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ humantime = "2.1.0"
itertools = "0.10.3"
lastfm-rs = "0.5.0"
poise = "0.6.1"
serenity = "0.12.2"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
tokio = { version = "1.38.0", features = ["full"] }
toml = "0.5.8"
tracing = "0.1.40"
tracing-futures = "0.2.5"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }

[dependencies.reqwest]
version = "0.11.12"
Expand Down
30 changes: 30 additions & 0 deletions src/listeners/handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serenity::{all::{ActivityData, Context, EventHandler, OnlineStatus, Ready}, async_trait};
use tracing::info;

pub struct Handler;

#[async_trait]
impl EventHandler for Handler {
async fn ready(&self, context: Context, ready: Ready) {
let http = &context.http;

let api_version = ready.version;
let bot_gateway = http.get_bot_gateway().await.unwrap();
let bot_owner = http.get_current_application_info().await.unwrap().owner.expect("Could not get owner!");
let t_sessions = bot_gateway.session_start_limit.total;
let r_sessions = bot_gateway.session_start_limit.remaining;

info!("Successfully logged into Discord as the following user:");
info!("Bot username: {}", ready.user.tag());
info!("Bot user ID: {}", ready.user.id);
info!("Bot owner: {}", bot_owner.tag());

let guild_count = ready.guilds.len();

info!("Connected to the Discord API (version {api_version}) with {r_sessions}/{t_sessions} sessions remaining.");
info!("Connected to and serving a total of {guild_count} guild(s).");

let presence = format!("on {guild_count} guilds | e.help");
context.set_presence(Some(ActivityData::playing(presence)), OnlineStatus::Online);
}
}
1 change: 1 addition & 0 deletions src/listeners/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod handler;
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
use crate::serenity::GatewayIntents;
use listeners::handler::Handler;
use poise::serenity_prelude as serenity;
use poise::{builtins::register_application_commands_buttons, Framework, FrameworkOptions, PrefixFrameworkOptions};
use tracing::{info, Level};
use tracing_log::LogTracer;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
use utils::read_config;

mod config;
mod listeners;
mod utils;

type Error = Box<dyn std::error::Error + Send + Sync>;
Expand All @@ -20,6 +25,28 @@ async fn register(ctx: Context<'_>) -> Result<(), Error> {
#[tokio::main(worker_threads = 16)]
async fn main() -> Result<(), Error> {
let configuration = read_config("config.toml");
if configuration.bot.logging.enabled {
LogTracer::init()?;

let level = match configuration.bot.logging.level.as_str() {
"error" => Level::ERROR,
"warn" => Level::WARN,
"info" => Level::INFO,
"debug" => Level::DEBUG,
_ => Level::TRACE,
};

let subscriber = FmtSubscriber::builder()
.with_target(false)
.with_max_level(level)
.with_env_filter(EnvFilter::from_default_env())
.finish();

tracing::subscriber::set_global_default(subscriber)?;

info!("Tracing initialized with logging level set to {}.", level);
}

let token = configuration.bot.discord.token;
let framework = Framework::builder()
.options(FrameworkOptions {
Expand All @@ -33,7 +60,7 @@ async fn main() -> Result<(), Error> {
.setup(move |_ctx, _ready, _framework| Box::pin(async move { Ok(Data {}) }))
.build();

let client = serenity::Client::builder(token, GatewayIntents::all()).framework(framework).await;
let client = serenity::Client::builder(token, GatewayIntents::all()).event_handler(Handler).framework(framework).await;
client.unwrap().start().await.unwrap();

Ok(())
Expand Down

0 comments on commit 5295f04

Please sign in to comment.