summary refs log tree commit diff
diff options
context:
space:
mode:
authorAshelyn Rose <git@ashen.earth>2024-11-10 12:52:02 -0700
committerAshelyn Rose <git@ashen.earth>2024-11-10 12:52:02 -0700
commit66208d1d9fcd32a88dcd9ecba13e87723812028a (patch)
treea3a080fa136c50e7d160f79c1b83a58e5d3e418d
parent058fb241e4373e083f1ab706904bd601abbf509a (diff)
Implement nick command
-rw-r--r--src/system/bot/client.rs14
-rw-r--r--src/system/bot/mod.rs4
-rw-r--r--src/system/mod.rs7
-rw-r--r--src/system/types.rs3
4 files changed, 27 insertions, 1 deletions
diff --git a/src/system/bot/client.rs b/src/system/bot/client.rs
index a2d1b28..19fcbe6 100644
--- a/src/system/bot/client.rs
+++ b/src/system/bot/client.rs
@@ -23,6 +23,20 @@ impl Client {
         }
     }
 
+    pub async fn set_nick<'a>(&self, server_id: ServerId, nick: &'a str) -> Result<(), TwiError> {
+        let client = self.client.lock().await;
+
+        let current_user = client.current_user()
+            .await?.model().await.expect("Could not retrieve current user");
+
+        client
+           .update_guild_member(server_id, current_user.id)
+           .nick(Some(nick)).expect("Invalid nick")
+           .await?;
+
+        Ok(())
+    }
+
     pub async fn fetch_message(&self, message_id: MessageId, channel_id: ChannelId) -> FullMessage {
         let client = self.client.lock().await;
 
diff --git a/src/system/bot/mod.rs b/src/system/bot/mod.rs
index 2aa3e0a..0928a73 100644
--- a/src/system/bot/mod.rs
+++ b/src/system/bot/mod.rs
@@ -62,6 +62,10 @@ impl Bot {
         self.gateway.set_status(status).await;
     }
 
+    pub async fn set_nick(&self, server_id: ServerId, nick: String) {
+        self.client.set_nick(server_id, nick.as_str()).await.expect("Could not update nick")
+    }
+
     pub fn start(&self) {
         self.gateway.start_listening()
     }
diff --git a/src/system/mod.rs b/src/system/mod.rs
index e2bd1ee..30850d3 100644
--- a/src/system/mod.rs
+++ b/src/system/mod.rs
@@ -341,6 +341,13 @@ impl Manager {
                 )));
             }
 
+            message_parser::ParsedMessage::Command(Command::Nick(member_id, nick)) => {
+                let member = self.bots.get(&member_id).unwrap();
+                let server_id = message.guild_id.expect("Message has no guild");
+
+                member.set_nick(server_id, nick).await;
+            }
+
             message_parser::ParsedMessage::Command(Command::UnknownCommand) => {
                 let member_id = if let Some((member_id, _)) = self.latch_state {
                     member_id
diff --git a/src/system/types.rs b/src/system/types.rs
index 9a410aa..2698f9d 100644
--- a/src/system/types.rs
+++ b/src/system/types.rs
@@ -1,12 +1,13 @@
 pub use twilight_model::channel::Message as TwiMessage;
 use twilight_model::gateway::payload::incoming::MessageUpdate as PartialMessage;
-use twilight_model::id::marker::{ChannelMarker, MessageMarker, UserMarker};
+use twilight_model::id::marker::{ChannelMarker, MessageMarker, UserMarker, GuildMarker};
 use twilight_model::id::Id;
 use twilight_model::util::Timestamp;
 
 pub type MemberId = usize;
 pub type MessageId = Id<MessageMarker>;
 pub type ChannelId = Id<ChannelMarker>;
+pub type ServerId = Id<GuildMarker>;
 pub type UserId = Id<UserMarker>;
 pub type FullMessage = TwiMessage;