Performance upgrades, fixes for 2 issues
This commit is contained in:
1 parent
dccf8c2057
commit
f3dff49c06
10 files changed
+177
-87
No files matched your search
@@ -44,13 +44,32 @@ pub async fn respond_message(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn defer_message(
|
||||
ctx: &Context,
|
||||
command: &ApplicationCommandInteraction,
|
||||
ephemeral: bool,
|
||||
) {
|
||||
if let Err(why) = command
|
||||
.create_interaction_response(&ctx.http, |response| {
|
||||
response
|
||||
.kind(InteractionResponseType::DeferredChannelMessageWithSource)
|
||||
.interaction_response_data(|message| message.ephemeral(ephemeral))
|
||||
})
|
||||
.await
|
||||
{
|
||||
error!("Error deferring message: {:?}", why);
|
||||
}
|
||||
}
|
||||
|
||||
pub type CommandOutput = Pin<Box<dyn Future<Output = ()> + Send>>;
|
||||
pub type CommandExecutor = fn(Context, ApplicationCommandInteraction) -> CommandOutput;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CommandManager {
|
||||
commands: HashMap<String, CommandInfo>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct CommandInfo {
|
||||
pub name: String,
|
||||
pub executor: CommandExecutor,
|
||||
|
||||
@@ -5,7 +5,7 @@ use serenity::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
bot::commands::{respond_message, CommandOutput},
|
||||
bot::commands::{defer_message, respond_message, CommandOutput},
|
||||
session::manager::{SessionCreateError, SessionManager},
|
||||
utils::embed::{EmbedBuilder, Status},
|
||||
};
|
||||
@@ -46,6 +46,7 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
|
||||
|
||||
// Check if another session is already active in this server
|
||||
let session_opt = session_manager.get_session(guild.id).await;
|
||||
|
||||
if let Some(session) = &session_opt {
|
||||
if let Some(owner) = session.get_owner().await {
|
||||
let msg = if owner == command.user.id {
|
||||
@@ -91,6 +92,8 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
|
||||
return;
|
||||
}
|
||||
|
||||
defer_message(&ctx, &command, true).await;
|
||||
|
||||
if let Some(session) = &session_opt {
|
||||
if let Err(why) = session.update_owner(&ctx, command.user.id).await {
|
||||
// Need to link first
|
||||
|
||||
+21
-8
@@ -23,7 +23,10 @@ impl EventHandler for Handler {
|
||||
|
||||
debug!("Ready received, logged in as {}", ready.user.name);
|
||||
|
||||
command_manager.register_commands(&ctx).await;
|
||||
// Set this to true only when a command is removed/updated/created
|
||||
if false {
|
||||
command_manager.register_commands(&ctx).await;
|
||||
}
|
||||
|
||||
ctx.set_activity(Activity::listening(MOTD)).await;
|
||||
|
||||
@@ -32,10 +35,15 @@ impl EventHandler for Handler {
|
||||
|
||||
// INTERACTION_CREATE event, emitted when the bot receives an interaction (slash command, button, etc.)
|
||||
async fn interaction_create(&self, ctx: Context, interaction: Interaction) {
|
||||
trace!("interaction_create START");
|
||||
|
||||
if let Interaction::ApplicationCommand(command) = interaction {
|
||||
// Commands must only be executed inside of guilds
|
||||
if command.guild_id.is_none() {
|
||||
command
|
||||
|
||||
let guild_id = match command.guild_id {
|
||||
Some(guild_id) => guild_id,
|
||||
None => {
|
||||
if let Err(why) = command
|
||||
.create_interaction_response(&ctx.http, |response| {
|
||||
response
|
||||
.kind(serenity::model::prelude::interaction::InteractionResponseType::ChannelMessageWithSource)
|
||||
@@ -43,17 +51,20 @@ impl EventHandler for Handler {
|
||||
message.content("You can only execute commands inside of a server")
|
||||
})
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
.await {
|
||||
error!("Failed to send run-in-guild-only error message: {}", why);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
trace!("interaction_create END2");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
trace!(
|
||||
"Received command interaction: command={} user={} guild={}",
|
||||
command.data.name,
|
||||
command.user.id,
|
||||
command.guild_id.unwrap()
|
||||
guild_id
|
||||
);
|
||||
|
||||
let data = ctx.data.read().await;
|
||||
@@ -61,5 +72,7 @@ impl EventHandler for Handler {
|
||||
|
||||
command_manager.execute_command(&ctx, command).await;
|
||||
}
|
||||
|
||||
trace!("interaction_create END");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user