Skip to content

Commit

Permalink
Fix: Making sure a command is there before checking permissions (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdellaert authored Oct 30, 2024
1 parent c716a2a commit 8d8a805
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/events/messageCreateHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,6 @@ export default event(Events.MessageCreate, async (_, message) => {
if (!thisBot) return;
const { id: channelId, name: channelName } = channel;
const { id: guildId } = guild;
const botPermissions = channel.permissionsFor(thisBot);
const requiredBotPermissions = [
PermissionsBitField.Flags.SendMessages,
PermissionsBitField.Flags.ReadMessageHistory,
];
// No point in continuing if the bot can't post a message or reply to a message
if (!botPermissions || !botPermissions.has(requiredBotPermissions)) {
Logger.info(`Bot does not have required permissions in channel ${channelName} (${channelId}) of server ${guildId}. Unable to process message ${messageId} from user ${authorId}.`);
return;
}
Logger.debug(`Processing message ${messageId} from user ${authorId} in channel ${channelId} of server ${guildId}.`);

const inMemoryCache = getInMemoryCache();
Expand Down Expand Up @@ -181,6 +171,17 @@ export default event(Events.MessageCreate, async (_, message) => {
// Step 3: Check if the command exists itself and process it
const cachedCommandDetails = await inMemoryCache.get(`${MemoryCachePrefix.COMMAND}:${commandText.toLowerCase()}`);
if (cachedCommandDetails) {
// Checking if the bos has proper permissions
const botPermissions = channel.permissionsFor(thisBot);
const requiredBotPermissions = [
PermissionsBitField.Flags.SendMessages,
PermissionsBitField.Flags.ReadMessageHistory,
];
// No point in continuing if the bot can't post a message or reply to a message
if (!botPermissions || !botPermissions.has(requiredBotPermissions)) {
Logger.info(`Bot does not have required permissions in channel ${channelName} (${channelId}) of server ${guildId}. Unable to process message ${messageId} from user ${authorId}.`);
return;
}
const commandDetails = PrefixCommand.hydrate(cachedCommandDetails);
const { name, contents, isEmbed, embedColor, permissions } = commandDetails;
const { roles: permRoles, rolesBlocklist, channels: permChannels, channelsBlocklist, quietErrors, verboseErrors } = permissions ?? new PrefixCommandPermissions();
Expand Down Expand Up @@ -308,7 +309,6 @@ export default event(Events.MessageCreate, async (_, message) => {
}
});
} else {
Logger.debug(`Prefix Command - Executing version "${commandVersionName}" for command "${name}" based on user command "${commandText}"`);
await sendReply(message, commandTitle, commandContent || '', isEmbed || false, embedColor || constantsConfig.colors.FBW_CYAN, commandImage || '');
}
}
Expand Down

0 comments on commit 8d8a805

Please sign in to comment.