Made /join permission errors more obvious

main
DaXcess 2022-11-14 10:22:55 +01:00
parent b8289aeab9
commit 986f3d46f8
2 changed files with 64 additions and 12 deletions

View File

@ -1,7 +1,7 @@
use log::trace;
use log::{error, trace};
use serenity::{
builder::CreateApplicationCommand,
model::prelude::interaction::application_command::ApplicationCommandInteraction,
model::prelude::{interaction::application_command::ApplicationCommandInteraction, Channel},
prelude::Context,
};
@ -43,15 +43,34 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
// Check for Voice Channel permissions
{
let channel = match ctx.cache.guild_channel(channel_id) {
Some(channel) => channel,
None => {
let channel = match channel_id.to_channel(&ctx).await {
Ok(channel) => match channel {
Channel::Guild(channel) => channel,
_ => {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Cannot join voice channel")
.description("The voice channel you are in is not supported")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
},
Err(why) => {
error!("Failed to get channel: {}", why);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Cannot join voice channel")
.description("The voice channel you are in is not available")
.description("The voice channel you are in is not available.\nI might not the permission to see this channel.")
.status(Status::Error)
.build(),
true,
@ -85,15 +104,34 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
// Check for Text Channel permissions
{
let channel = match ctx.cache.guild_channel(&command.channel_id) {
Some(channel) => channel,
None => {
let channel = match command.channel_id.to_channel(&ctx).await {
Ok(channel) => match channel {
Channel::Guild(channel) => channel,
_ => {
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Cannot join voice channel")
.description("The text channel you are in is not supported")
.status(Status::Error)
.build(),
true,
)
.await;
return;
}
},
Err(why) => {
error!("Failed to get channel: {}", why);
respond_message(
&ctx,
&command,
EmbedBuilder::new()
.title("Cannot join voice channel")
.description("The text channel you are in is not available")
.description("The text channel you are in is not available.\nI might not have the permission to see this channel.")
.status(Status::Error)
.build(),
true,
@ -114,7 +152,9 @@ pub fn run(ctx: Context, command: ApplicationCommandInteraction) -> CommandOutpu
&command,
EmbedBuilder::new()
.title("Cannot join voice channel")
.description("I do not have the permissions to speak in this text channel")
.description(
"I do not have the permissions to send messages / links in this text channel",
)
.status(Status::Error)
.build(),
true,

View File

@ -3,7 +3,7 @@
use log::*;
use serenity::{
async_trait,
model::prelude::{interaction::Interaction, Activity, Ready},
model::prelude::{interaction::Interaction, Activity, GuildId, Ready},
prelude::{Context, EventHandler},
};
@ -36,6 +36,18 @@ 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) {
if let Interaction::ApplicationCommand(command) = interaction {
if let Ok(guild_id) = std::env::var("GUILD_ID") {
if let Ok(guild_id) = guild_id.parse::<u64>() {
let guild_id = GuildId(guild_id);
if let Some(interaction_guild_id) = command.guild_id {
if guild_id != interaction_guild_id {
return;
}
}
}
}
// Commands must only be executed inside of guilds
let guild_id = match command.guild_id {