Skip to content

Commit f254527

Browse files
authored
Merge pull request #14 from f-code-club/feature/event-handler
Feature/event handler
2 parents 32eb7f1 + 2d51e52 commit f254527

3 files changed

Lines changed: 46 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Shows a help message, either for all commands or for a specific command.
6060

6161
Add one or more candidate IDs to the candidates database from a text file.
6262

63-
**Permissions:** `MANAGE_MESSAGES` or `MANAGE_THREADS`
63+
**Permissions:** `Moderator`
6464

6565
**Parameters:**
6666
* `id`: A text file with candidate IDs (one per line, UTF-8).
@@ -74,7 +74,7 @@ Add one or more candidate IDs to the candidates database from a text file.
7474

7575
Delete a candidate by ID from the candidates database.
7676

77-
**Permissions:** `MANAGE_MESSAGES` or `MANAGE_THREADS`
77+
**Permissions:** `Moderator`
7878

7979
**Parameters:**
8080
* `id`: The ID of the candidate to delete.

src/event_handler.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use anyhow::{Error, Result};
2+
use poise::serenity_prelude as serenity;
3+
4+
use crate::State;
5+
6+
pub async fn event_handler(
7+
_: &serenity::Context,
8+
event: &serenity::FullEvent,
9+
_framework: poise::FrameworkContext<'_, State, Error>,
10+
_: &State,
11+
) -> Result<()> {
12+
if let serenity::FullEvent::Ready { data_about_bot, .. } = event {
13+
tracing::info!("Logged in as {}", data_about_bot.user.name);
14+
}
15+
16+
Ok(())
17+
}

src/main.rs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod check;
22
pub mod command;
33
pub mod config;
44
pub mod database;
5+
pub mod event_handler;
56
pub mod message;
67
pub mod state;
78
pub mod util;
@@ -10,7 +11,7 @@ use std::sync::Arc;
1011
use std::time::Duration;
1112

1213
use poise::serenity_prelude::{Client, GatewayIntents};
13-
use poise::{Framework, FrameworkOptions};
14+
use poise::{CreateReply, Framework, FrameworkOptions};
1415

1516
pub use crate::config::Config;
1617
pub use crate::message::Message;
@@ -38,6 +39,31 @@ pub async fn build_bot() -> anyhow::Result<()> {
3839
))),
3940
..Default::default()
4041
},
42+
event_handler: |ctx, event, framework, data| {
43+
Box::pin(event_handler::event_handler(ctx, event, framework, data))
44+
},
45+
on_error: |error| {
46+
Box::pin(async move {
47+
if let poise::FrameworkError::Command { ctx, .. } = error {
48+
let _ = ctx
49+
.send(
50+
CreateReply::default()
51+
.content(Message::Error)
52+
.ephemeral(true),
53+
)
54+
.await;
55+
56+
return;
57+
}
58+
59+
if let poise::FrameworkError::Setup { error, .. } = error {
60+
tracing::error!("Framework setup error: {:?}", error);
61+
return;
62+
}
63+
64+
tracing::error!("Other framework error (no user context to reply to).");
65+
})
66+
},
4167
..Default::default()
4268
};
4369

0 commit comments

Comments
 (0)