fix: annotated util validation functions

This commit is contained in:
Marius Begby committed 2023-08-27 01:36:58 +02:00
1 parent d38ed18842
commit 1ae9503477
5 files changed
+64 -50

No files matched your search

+25 -21
View File
@@ -1,22 +1,23 @@
import config from 'config';
import { EmbedOptions } from '../../types/configTypes';
const embedOptions: EmbedOptions = config.get('embedOptions');
import { EmbedBuilder } from 'discord.js';
import { EmbedBuilder, GuildMember, VoiceChannel, TextChannel } from 'discord.js';
import loggerModule from '../../services/logger';
import { CannotJoinVoiceOrTalkParams, CannotSendMessageInChannelParams } from '../../types/utilTypes';
export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }) => {
export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }: CannotJoinVoiceOrTalkParams) => {
const logger = loggerModule.child({
source: 'permissionValidator.js',
module: 'utilValidation',
name: 'cannotJoinVoiceOrTalk',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
const channel = interaction.member.voice.channel;
const channel = interaction.member instanceof GuildMember ? interaction.member.voice.channel : null;
if (!channel.joinable || !channel.speakable) {
if (channel instanceof VoiceChannel && (!channel.joinable || !channel.speakable)) {
await interaction.editReply({
embeds: [
new EmbedBuilder()
@@ -36,20 +37,20 @@ export const cannotJoinVoiceOrTalk = async ({ interaction, executionId }) => {
return false;
};
export const cannotSendMessageInChannel = async ({ interaction, executionId }) => {
export const cannotSendMessageInChannel = async ({ interaction, executionId }: CannotSendMessageInChannelParams) => {
const logger = loggerModule.child({
source: 'permissionValidator.js',
module: 'utilValidation',
name: 'cannotSendMessageInChannel',
executionId: executionId,
shardId: interaction.guild.shardId,
guildId: interaction.guild.id
shardId: interaction.guild?.shardId,
guildId: interaction.guild?.id
});
const channel = interaction.channel;
// only checks if channel is viewable, as bot will have permission to send interaction replies if channel is viewable
if (!channel.viewable) {
if (channel instanceof TextChannel && !channel.viewable) {
logger.info(
`User tried to use command '${interaction.commandName}' but the bot had no permission to send reply in text channel.`
);
@@ -66,20 +67,23 @@ export const cannotSendMessageInChannel = async ({ interaction, executionId }) =
`**${embedOptions.icons.warning} Oops!**\nI do not have permission to send message replies in the channel you are in.\n\nPlease make sure I have the **\`View Channel\`** permission in this text channel.`
)
.setColor(embedOptions.colors.warning)
],
ephemeral: true
]
});
} catch (error) {
if (error.message == 'The reply to this interaction has already been sent or deferred.') {
logger.warn(
'Error while sending ephemereal message about insufficient permissions to send message in channel.'
);
logger.debug(error);
if (error instanceof Error) {
if (error.message == 'The reply to this interaction has already been sent or deferred.') {
logger.warn(
'Error while sending ephemereal message about insufficient permissions to send message in channel.'
);
logger.debug(error);
} else {
logger.error(
error,
'Failed to send ephemeral reply to user in channel that bot cannot view/send message in.'
);
}
} else {
logger.error(
error,
'Failed to send ephemeral reply to user in channel that bot cannot view/send message in.'
);
throw error;
}
}