Skip to content

Commit 125490d

Browse files
committed
feat(framework): migrate to serenity and poise next event dispatch and client setup
1 parent ff6ab5a commit 125490d

2 files changed

Lines changed: 55 additions & 58 deletions

File tree

src/events/mod.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::abstraction::activity_data::FromStringTuple;
22
use lazy_static::lazy_static;
33
use log::{debug, info};
4-
use serenity::all::{ActivityData, Ready, ResumedEvent};
4+
use serenity::all::{ActivityData, Context, EventHandler, FullEvent};
55
use serenity::async_trait;
6-
use serenity::prelude::{Context, EventHandler};
76
use std::env;
87

98
pub struct Handler;
@@ -15,18 +14,22 @@ lazy_static! {
1514

1615
#[async_trait]
1716
impl EventHandler for Handler {
18-
async fn ready(&self, ctx: Context, ready: Ready) {
19-
info!("{} is connected!", ready.user.name);
17+
async fn dispatch(&self, ctx: &Context, event: &FullEvent) {
18+
match event {
19+
FullEvent::Ready { data_about_bot, .. } => {
20+
info!("{} is connected!", data_about_bot.user.name);
2021

21-
if !DISCORD_STATUS.is_empty() && !DISCORD_STATUS_NAME.is_empty() {
22-
ctx.set_activity(Some(ActivityData::from_tuple(
23-
&DISCORD_STATUS,
24-
&DISCORD_STATUS_NAME,
25-
)));
26-
};
27-
}
28-
29-
async fn resume(&self, _ctx: Context, _resume: ResumedEvent) {
30-
debug!("Resumed");
22+
if !DISCORD_STATUS.is_empty() && !DISCORD_STATUS_NAME.is_empty() {
23+
ctx.set_activity(Some(ActivityData::from_tuple(
24+
&DISCORD_STATUS,
25+
&DISCORD_STATUS_NAME,
26+
)));
27+
}
28+
}
29+
FullEvent::Resume { .. } => {
30+
debug!("Resumed");
31+
}
32+
_ => {}
33+
}
3134
}
3235
}

src/main.rs

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ use crate::command::{
99
use abstraction::command::CommandData;
1010
use dotenvy::dotenv;
1111
use log::info;
12-
use serenity::all::GuildId;
12+
use serenity::all::{GuildId, Http, Token};
1313
use serenity::prelude::GatewayIntents;
14+
use std::sync::Arc;
1415

1516
pub mod built_info {
1617
// The file has been placed there by the build script.
@@ -19,7 +20,6 @@ pub mod built_info {
1920

2021
#[tokio::main]
2122
async fn main() -> anyhow::Result<()> {
22-
// Load environment variables from .env file, if present but do nothing if it fails
2323
let _ = dotenv();
2424
tracing_subscriber::fmt::init();
2525

@@ -31,57 +31,51 @@ async fn main() -> anyhow::Result<()> {
3131
built_info::BUILT_TIME_UTC
3232
);
3333

34-
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
35-
34+
let token = Token::from_env("DISCORD_TOKEN")?;
3635
let intents = GatewayIntents::non_privileged();
3736

38-
let framework = poise::Framework::builder()
39-
.options(poise::FrameworkOptions {
40-
commands: get_all_commands(),
41-
post_command: |ctx| {
42-
Box::pin(async move {
43-
let author = ctx.author();
44-
let guild = ctx.guild();
45-
let cmd = ctx.command();
46-
47-
let user_info = format!("{}[{}]", author.name, author.id);
48-
49-
let guild_info = if let Some(guild) = guild {
50-
format!("{}[{}]", guild.name, guild.id)
51-
} else {
52-
"Direct Messages".to_string()
53-
};
37+
let http = Http::new(token.clone());
38+
poise::builtins::register_globally(&http, get_global_commands().iter()).await?;
39+
poise::builtins::register_in_guild(
40+
&http,
41+
retrorealm_server_commands().iter(),
42+
GuildId::new(*RETROREALM_SERVER_ID),
43+
)
44+
.await?;
45+
46+
let options = poise::FrameworkOptions {
47+
commands: get_all_commands(),
48+
post_command: |ctx| {
49+
Box::pin(async move {
50+
let author = ctx.author();
51+
let guild = ctx.guild();
52+
let cmd = ctx.command();
5453

55-
let command_name = cmd.name.to_lowercase();
54+
let user_info = format!("{}[{}]", author.name, author.id);
5655

57-
info!("{user_info} @ {guild_info} {command_name}");
58-
})
59-
},
60-
..Default::default()
61-
})
62-
.setup(|ctx, _ready, _framework| {
63-
Box::pin(async move {
64-
poise::builtins::register_globally(ctx, get_global_commands().as_slice()).await?;
56+
let guild_info = if let Some(guild) = guild {
57+
format!("{}[{}]", guild.name, guild.id)
58+
} else {
59+
"Direct Messages".to_string()
60+
};
6561

66-
poise::builtins::register_in_guild(
67-
ctx,
68-
retrorealm_server_commands().as_slice(),
69-
GuildId::from(*RETROREALM_SERVER_ID),
70-
)
71-
.await?;
62+
let command_name = cmd.name.to_lowercase();
7263

73-
Ok(CommandData::default())
64+
info!("{user_info} @ {guild_info} {command_name}");
7465
})
75-
})
76-
.initialize_owners(true)
77-
.build();
66+
},
67+
..Default::default()
68+
};
69+
70+
let framework = poise::Framework::new(options);
7871

79-
let mut client = serenity::Client::builder(token, intents)
80-
.event_handler(events::Handler)
81-
.framework(framework)
82-
.await?;
72+
let client = serenity::Client::builder(token, intents)
73+
.framework(Box::new(framework))
74+
.event_handler(Arc::new(events::Handler))
75+
.data(Arc::new(CommandData::default()) as _)
76+
.await;
8377

84-
client.start().await?;
78+
client?.start().await?;
8579

8680
Ok(())
8781
}

0 commit comments

Comments
 (0)