Skip to content

Commit 2ad0851

Browse files
committed
Use the poise framework
1 parent d44b120 commit 2ad0851

File tree

14 files changed

+236
-548
lines changed

14 files changed

+236
-548
lines changed

Cargo.lock

Lines changed: 45 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ tower = { version = "0.5.2", features = ["util"] }
1717
tower-http = { version = "0.6.2", features = ["fs", "trace"] }
1818
tracing = "0.1.41"
1919
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
20+
poise = "0.6.1"

README

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,3 @@ HOW TO TEST
1515

1616
You can test the application with `cargo test -- --test-threads=1`. Remember to
1717
have docker running.
18-
19-
COMMANDS
20-
--------
21-
22-
/kø
23-
24-
Sjekker hvor i køen brukeren er.
25-
26-
/vaffel
27-
28-
Bestiller en vaffel og setter deg i køen. Om du allerede er i køen, vil den bare
29-
vise hvor i øken brukeren er.
30-
31-
/stekt [number]
32-
33-
Steker antall vafler, og fjerner de fra køen.
34-
35-
/tøm
36-
37-
Fjerner alle vafler fra køen. Brukes når det er tomt for vafler.
38-
39-
/stopp
40-
41-
Stopper steking av vafler, lar ingen kommer inn i køen.
42-
43-
/start
44-
45-
Starter steking av vafler, lar alle komme inn i køen.

src/bot.rs

Lines changed: 52 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,80 @@
1+
use serenity::Error as SerenityError;
12
use std::sync::Arc;
23

3-
use serenity::all::UserId;
4-
use serenity::async_trait;
5-
use serenity::model::application::Interaction;
6-
use serenity::model::gateway::Ready;
7-
use serenity::model::id::GuildId;
8-
use serenity::prelude::*;
9-
use tracing::{debug, error, info};
4+
use poise::FrameworkOptions;
5+
use serenity::all::GatewayIntents;
106

11-
use crate::commands::bake::BakeCommand;
12-
use crate::commands::close::CloseCommand;
13-
use crate::commands::empty::EmptyCommand;
14-
use crate::commands::open::OpenCommand;
15-
use crate::commands::ping::PingCommand;
16-
use crate::commands::queue_size::QueueSizeCommand;
17-
use crate::commands::waffle::WaffleCommand;
18-
use crate::commands::CommandHandler;
19-
use crate::queue::WaffleQueue;
7+
use crate::{commands, queue::WaffleQueue};
208

21-
pub struct WaffleContext<'a> {
22-
pub is_oracle: bool,
9+
pub type Error = Box<dyn std::error::Error + Send + Sync>;
10+
pub type Context<'a> = poise::Context<'a, Data, Error>;
11+
12+
pub struct Data {
2313
pub queue: Arc<WaffleQueue>,
24-
#[allow(dead_code)]
25-
pub context: &'a Context,
2614
}
2715

2816
pub struct WaffleBot {
2917
token: String,
30-
guild_id: GuildId,
3118
queue: Arc<WaffleQueue>,
32-
commands: Vec<Box<(dyn CommandHandler + 'static)>>,
3319
}
3420

3521
impl WaffleBot {
36-
pub fn new(token: String, guild_id: u64, queue: Arc<WaffleQueue>) -> Self {
37-
let guild_id = GuildId::new(guild_id);
22+
pub fn new(token: String, queue: Arc<WaffleQueue>) -> Self {
23+
Self { token, queue }
24+
}
3825

39-
let commands: Vec<Box<dyn CommandHandler>> = vec![
40-
Box::new(PingCommand::new()),
41-
Box::new(OpenCommand::new()),
42-
Box::new(QueueSizeCommand::new()),
43-
Box::new(WaffleCommand::new()),
44-
Box::new(BakeCommand::new()),
45-
Box::new(CloseCommand::new()),
46-
Box::new(EmptyCommand::new()),
47-
];
26+
pub async fn start(self) -> Result<(), SerenityError> {
27+
let options: FrameworkOptions<Data, Error> = poise::FrameworkOptions {
28+
commands: vec![
29+
commands::bake::bake(),
30+
commands::close::close(),
31+
commands::open::open(),
32+
commands::ping::ping(),
33+
commands::queue_size::queue(),
34+
commands::waffle::waffle(),
35+
],
36+
prefix_options: poise::PrefixFrameworkOptions {
37+
prefix: Some("!".into()),
38+
..Default::default()
39+
},
40+
..Default::default()
41+
};
42+
43+
let framework = poise::Framework::builder()
44+
.setup(move |ctx, _ready, framework| {
45+
Box::pin(async move {
46+
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
47+
Ok(Data {
48+
queue: self.queue.clone(),
49+
})
50+
})
51+
})
52+
.options(options)
53+
.build();
54+
55+
let mut client = serenity::Client::builder(self.token.clone(), GatewayIntents::empty())
56+
.framework(framework)
57+
.await?;
4858

49-
Self {
50-
token,
51-
guild_id,
52-
queue,
53-
commands,
54-
}
59+
client.start().await
5560
}
61+
}
5662

57-
pub async fn is_user_oracle(&self, ctx: &Context, user_id: UserId) -> bool {
58-
if let Ok(member) = self.guild_id.member(ctx, user_id).await {
59-
if let Ok(roles) = self.guild_id.roles(ctx).await {
63+
#[allow(unused)]
64+
pub async fn check_is_oracle(ctx: Context<'_>) -> Result<bool, Error> {
65+
if let Ok(member) = ctx.guild_id().unwrap().member(ctx, ctx.author().id).await {
66+
if let Some(guild_id) = ctx.guild_id() {
67+
if let Ok(roles) = guild_id.roles(ctx).await {
6068
if let Some(orakel_role_id) = roles
6169
.values()
6270
.find(|r| r.name.to_lowercase() == "orakel")
6371
.map(|r| r.id)
6472
{
65-
return member.roles.contains(&orakel_role_id);
73+
return Ok(member.roles.contains(&orakel_role_id));
6674
}
6775
}
6876
}
69-
70-
false
7177
}
7278

73-
pub async fn start(self) -> Result<(), serenity::Error> {
74-
let mut client = Client::builder(self.token.clone(), GatewayIntents::empty())
75-
.event_handler(self)
76-
.await?;
77-
78-
client.start().await
79-
}
80-
}
81-
82-
#[async_trait]
83-
impl EventHandler for WaffleBot {
84-
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
85-
if let Interaction::Command(command) = interaction {
86-
let cmd = self
87-
.commands
88-
.iter()
89-
.find(|cmd| cmd.name() == command.data.name);
90-
91-
if let Some(cmd) = cmd {
92-
let is_oracle = self.is_user_oracle(&ctx, command.user.id).await;
93-
let c = WaffleContext {
94-
is_oracle,
95-
queue: self.queue.clone(),
96-
context: &ctx,
97-
};
98-
99-
let content = cmd.execute(&c, &command);
100-
101-
if let Err(why) = command.create_response(&ctx.http, content).await {
102-
debug!("Cannot respond to slash command: {why}");
103-
}
104-
}
105-
}
106-
}
107-
108-
async fn ready(&self, ctx: Context, ready: Ready) {
109-
info!("{} is connected!", ready.user.name);
110-
111-
let commands = self
112-
.guild_id
113-
.set_commands(
114-
&ctx.http,
115-
self.commands.iter().map(|cmd| cmd.register()).collect(),
116-
)
117-
.await;
118-
119-
if let Err(why) = commands {
120-
error!("Error registering commands: {why}");
121-
} else {
122-
info!("Commands registered successfully");
123-
}
124-
}
79+
Ok(false)
12580
}

0 commit comments

Comments
 (0)