|
| 1 | +use serenity::Error as SerenityError; |
1 | 2 | use std::sync::Arc; |
2 | 3 |
|
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; |
10 | 6 |
|
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}; |
20 | 8 |
|
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 { |
23 | 13 | pub queue: Arc<WaffleQueue>, |
24 | | - #[allow(dead_code)] |
25 | | - pub context: &'a Context, |
26 | 14 | } |
27 | 15 |
|
28 | 16 | pub struct WaffleBot { |
29 | 17 | token: String, |
30 | | - guild_id: GuildId, |
31 | 18 | queue: Arc<WaffleQueue>, |
32 | | - commands: Vec<Box<(dyn CommandHandler + 'static)>>, |
33 | 19 | } |
34 | 20 |
|
35 | 21 | 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 | + } |
38 | 25 |
|
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?; |
48 | 58 |
|
49 | | - Self { |
50 | | - token, |
51 | | - guild_id, |
52 | | - queue, |
53 | | - commands, |
54 | | - } |
| 59 | + client.start().await |
55 | 60 | } |
| 61 | +} |
56 | 62 |
|
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 { |
60 | 68 | if let Some(orakel_role_id) = roles |
61 | 69 | .values() |
62 | 70 | .find(|r| r.name.to_lowercase() == "orakel") |
63 | 71 | .map(|r| r.id) |
64 | 72 | { |
65 | | - return member.roles.contains(&orakel_role_id); |
| 73 | + return Ok(member.roles.contains(&orakel_role_id)); |
66 | 74 | } |
67 | 75 | } |
68 | 76 | } |
69 | | - |
70 | | - false |
71 | 77 | } |
72 | 78 |
|
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) |
125 | 80 | } |
0 commit comments