aboutsummaryrefslogtreecommitdiff
path: root/src/handlers
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-01-18 10:58:36 -0600
committerNathan Ringo <nathan@remexre.com>2024-01-18 10:58:36 -0600
commit00d0bfced902e97eeae5257c14134d4bc7efc710 (patch)
treeee026f328614e03aec3ed373d9f2e6c8e255f834 /src/handlers
parent7017762a4a38266aa88976be141f7bd663647edc (diff)
Commands to interact with discocaml, associated IPC.
Diffstat (limited to 'src/handlers')
-rw-r--r--src/handlers/commands.rs29
-rw-r--r--src/handlers/mod.rs2
-rw-r--r--src/handlers/x500_mapper.rs11
3 files changed, 37 insertions, 5 deletions
diff --git a/src/handlers/commands.rs b/src/handlers/commands.rs
new file mode 100644
index 0000000..d07bdcb
--- /dev/null
+++ b/src/handlers/commands.rs
@@ -0,0 +1,29 @@
+use crate::commands::{handle_interaction, set_commands, CommandsConfig};
+use serenity::{
+ all::{Interaction, Ready},
+ async_trait,
+ client::{Context, EventHandler},
+};
+use sqlx::SqlitePool;
+
+/// A handler that sets up the commands.
+pub struct Commands {
+ pub config: CommandsConfig,
+ pub db: SqlitePool,
+}
+
+#[async_trait]
+impl EventHandler for Commands {
+ async fn ready(&self, ctx: Context, _data_about_bot: Ready) {
+ if let Err(err) = set_commands(&ctx).await {
+ log::error!("failed to set commands: {:?}", err)
+ }
+ }
+
+ async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
+ if let Err(err) = handle_interaction(&ctx, &self.config, &self.db, &interaction).await {
+ log::error!("failed to handle interaction: {:?}", err);
+ log::error!("failed interaction was: {:?}", interaction);
+ }
+ }
+}
diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs
index 46bd57b..4c09fec 100644
--- a/src/handlers/mod.rs
+++ b/src/handlers/mod.rs
@@ -18,10 +18,12 @@ use serenity::{
};
use std::collections::HashMap;
+mod commands;
mod presence_setter;
mod x500_mapper;
pub use self::{
+ commands::Commands,
presence_setter::PresenceSetter,
x500_mapper::{X500Mapper, X500MapperConfig},
};
diff --git a/src/handlers/x500_mapper.rs b/src/handlers/x500_mapper.rs
index 1ec35f2..acd498f 100644
--- a/src/handlers/x500_mapper.rs
+++ b/src/handlers/x500_mapper.rs
@@ -90,9 +90,9 @@ impl X500Mapper {
async fn record_x500(&self, ctx: &Context, member: Member, x500: String) {
let x500 = &x500;
- let uid = member.user.id;
+ let member = &member;
let future = async move {
- let uid = i64::from(uid);
+ let uid = i64::from(member.user.id);
sqlx::query!(
"INSERT OR IGNORE INTO all_seen_uids_to_x500s (uid, x500) VALUES (?, ?)",
uid,
@@ -112,8 +112,8 @@ impl X500Mapper {
.context("failed to insert into uids_to_x500s_clean")?;
if let Some(role) = self.config.students_role {
- log::info!("adding the role {} to {}", role, member.display_name());
if !member.roles.contains(&role) {
+ log::info!("adding the role {} to {:?}", role, member.display_name());
member
.add_role(&ctx.http, role)
.await
@@ -127,8 +127,9 @@ impl X500Mapper {
match future.await {
Ok(()) => (),
Err(err) => log::error!(
- "failed to record that the user with UID {} had the X.500 {}: {:?}",
- uid,
+ "failed to record that the user with UID {} ({:?}) had the X.500 {}: {:?}",
+ member.user.id,
+ member.display_name(),
x500,
err
),