Skip to content

Commit

Permalink
fix: ignore poise prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jul 14, 2023
1 parent 264318e commit 5643623
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
25 changes: 2 additions & 23 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::serenity;

use crate::{Context, Error, ProxyConfiguration, CLYDE_ID};
use poise::serenity_prelude::{CacheHttp, ChannelId};
use poise::serenity_prelude::{CacheHttp};

/// Show this help menu.
#[poise::command(prefix_command)]
Expand Down Expand Up @@ -67,24 +67,3 @@ pub async fn proxy(

Ok(())
}

pub async fn proxy_message(
ctx: &serenity::Context,
proxy_config: &mut ProxyConfiguration,
from_channel_id: &ChannelId,
author: &serenity::User,
message: &String,
) -> Result<(), Error> {
// Remember the current channel ID so we can send a message back to it.
proxy_config.from_channel_id = *from_channel_id;

proxy_config
.to_channel_id
.say(
&ctx.http,
format!("<@{}> Hello, my name is {}. {}", CLYDE_ID, author, message),
)
.await?;

Ok(())
}
76 changes: 49 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const CLYDE_ID: u64 = 1081004946872352958;
#[derive(Default)]
struct Handler {
options: poise::FrameworkOptions<Data, Error>,
command_names: Vec<String>,
data: Data,
bot_id: UserId,
shard_manager: std::sync::Mutex<Option<Arc<tokio::sync::Mutex<serenity::ShardManager>>>>,
Expand Down Expand Up @@ -59,8 +60,15 @@ impl Default for Locking {
// Custom handler to dispatch poise events.
impl Handler {
pub fn new(options: poise::FrameworkOptions<Data, Error>, bot_id: u64) -> Self {
let command_names = options
.commands
.iter()
.map(|c| c.name.clone())
.collect::<Vec<_>>();

Self {
options,
command_names,
bot_id: UserId(bot_id),
..Default::default()
}
Expand Down Expand Up @@ -108,34 +116,48 @@ impl serenity::EventHandler for Handler {
};

if proxy {
// Lock the event bus to prevent other messages from being sent before Clyde replied to the sent message.
self.data
.locking
.semaphore
.acquire()
.await
.unwrap()
.forget();

if let Some(ref mut proxy_config) = &mut self.data.config.lock().await.proxy_config
let message_to_proxy = &new_message
.content
.replace(&format!("<@{}>", self.bot_id), "");

let trimmed_message_to_proxy = message_to_proxy.trim();
if !self
.command_names
.iter()
.any(|name| trimmed_message_to_proxy.starts_with(name))
{
let _ = commands::proxy_message(
&ctx,
proxy_config,
&new_message.channel_id,
&new_message.author,
&new_message
.content
.replace(&format!("<@{}>", self.bot_id).to_string(), ""),
)
.await;

self.data.locking.shared_state.lock().await.replace(
State {
last_message: new_message.clone(),
}
.into(),
);
// Lock the event bus to prevent other messages from being sent before Clyde replied to the sent message.
self.data
.locking
.semaphore
.acquire()
.await
.unwrap()
.forget();

if let Some(ref mut proxy_config) =
&mut self.data.config.lock().await.proxy_config
{
proxy_config.from_channel_id = new_message.channel_id;

let _ = proxy_config
.to_channel_id
.say(
&ctx.http,
format!(
"<@{}> Hello, my name is {}. {}",
CLYDE_ID, new_message.author, message_to_proxy
),
)
.await;

self.data.locking.shared_state.lock().await.replace(
State {
last_message: new_message.clone(),
}
.into(),
);
}
}
}
}
Expand Down

0 comments on commit 5643623

Please sign in to comment.