diff --git a/src/commands/Admin/config.js b/src/commands/Admin/config.js index fce39cd..8bb3e31 100644 --- a/src/commands/Admin/config.js +++ b/src/commands/Admin/config.js @@ -2,7 +2,7 @@ const { MessageEmbed } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { buildErrorEmbed, getDefaultSuggestionsChannel, getChannel, validateChannel } = require('../../utils/functions'); +const { buildErrorEmbed, getDefaultSuggestionsChannel, getChannel, validateChannel, getLogsChannel } = require('../../utils/functions'); module.exports = class ConfigCommand extends Command { constructor(client) { @@ -96,7 +96,8 @@ module.exports = class ConfigCommand extends Command { const isDefault = suggestionsChannel === 'suggestions' suggestionsChannel = isDefault - ? await message.guild.channels.fetch({ cache: false }).then(res => res.find(c => c.name === 'suggestions')) + ? await message.guild.channels.fetch({ cache: false }) + .then(res => res.filter(c => ['text', 'news'].includes(c.type)).find(c => c.name === 'suggestions')) : message.guild.channels.forge(suggestionsChannel) if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel); configEmbed.setDescription(`Current suggestions channel: ${suggestionsChannel}`); @@ -123,10 +124,7 @@ module.exports = class ConfigCommand extends Command { } } - const isDefault = [this.client.config.defaultSettings.suggestionsLogs, "suggestions-logs"].includes(suggestionsLogs); - suggestionsLogs = isDefault - ? await message.guild.channels.fetch({ cache: false }).then(res => res.find(c => c.name === this.client.config.suggestionLogs)) - : message.guild.channels.forge(suggestionsLogs); + suggestionsLogs = await getLogsChannel(message, suggestionsLogs) if (!suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel); configEmbed.setDescription(`Current suggestions logs channel: ${suggestionsLogs}`); configEmbed.addField('More Information', `[Link](${confDocs}#suggestions-logs-channel)`); diff --git a/src/commands/Staff/approve.js b/src/commands/Staff/approve.js index 8bf7260..ac44e62 100644 --- a/src/commands/Staff/approve.js +++ b/src/commands/Staff/approve.js @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed } = require('../../utils/functions'); +const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions'); module.exports = class ApproveCommand extends Command { constructor(client) { @@ -69,21 +69,18 @@ module.exports = class ApproveCommand extends Command { Please contact the developer via the Support Discord: ${discord}`); } - const isDefault = [this.client.config.defaultSettings.suggestionsLogs, 'suggestions-logs'].includes(settings.suggestionsLogs); - const willIgnoreLogs = isDefault && !settings.keepLogs; - let suggestionsChannel, suggestionsLogs; try { suggestionsChannel = settings.suggestionsChannel && await message.guild.channels.fetch(settings.suggestionsChannel); if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel); - if (!willIgnoreLogs) { - suggestionsLogs = settings.suggestionsLogs && await message.guild.channels.fetch(settings.suggestionsLogs); + if (!settings.keepLogs) { + suggestionsLogs = settings.suggestionsLogs && await getLogsChannel(message, settings.suggestionsLogs); if (!suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel); } } catch (error) { if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel); - if (!willIgnoreLogs) return this.client.errors.noSuggestionsLogs(message.channel); + if (!settings.keepLogs && !suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel); Logger.errorCmd(this, error.stack); return message.channel.send(buildErrorEmbed(error)); } @@ -191,7 +188,7 @@ module.exports = class ApproveCommand extends Command { `); } - if (!willIgnoreLogs) { + if (suggestionsLogs && !settings.keepLogs) { const missingPermissions = suggestionsLogs.permissionsFor(message.guild.me).missing(logsPermissions); if (missingPermissions.length > 0) return this.client.errors.noChannelPerms(message, suggestionsLogs, missingPermissions); } @@ -212,7 +209,7 @@ module.exports = class ApproveCommand extends Command { try { // Will handle suggestion if there's no logs channel set/default value is set OR keepLogs is enabled and a valid logs channel is set - if (sMessage && (willIgnoreLogs || (settings.keepLogs && suggestionsLogs))) { + if (sMessage && settings.keepLogs) { const logEmbed = new MessageEmbed(logsEmbed) .setTitle('Suggestion Approved'); await sMessage.edit(logEmbed); diff --git a/src/commands/Staff/reject.js b/src/commands/Staff/reject.js index 064af03..a5aa899 100644 --- a/src/commands/Staff/reject.js +++ b/src/commands/Staff/reject.js @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light'); const { stripIndent } = require('common-tags'); const Command = require('../../structures/Command'); const Logger = require('../../utils/logger'); -const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed } = require('../../utils/functions'); +const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions'); module.exports = class RejectCommand extends Command { constructor(client) { @@ -72,21 +72,18 @@ module.exports = class RejectCommand extends Command { Please contact the developer via the Support Discord: ${discord}`); } - const isDefault = [this.client.config.defaultSettings.suggestionsLogs, 'suggestions-logs'].includes(settings.suggestionsLogs); - const willIgnoreLogs = isDefault && !settings.keepLogs; - let suggestionsChannel, suggestionsLogs; try { suggestionsChannel = settings.suggestionsChannel && await message.guild.channels.fetch(settings.suggestionsChannel); if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel); - if (!willIgnoreLogs) { - suggestionsLogs = settings.suggestionsLogs && await message.guild.channels.fetch(settings.suggestionsLogs); + if (!settings.keepLogs) { + suggestionsLogs = settings.suggestionsLogs && await getLogsChannel(message, settings.suggestionsLogs); if (!suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel); } } catch (error) { if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel); - if (!willIgnoreLogs) return this.client.errors.noSuggestionsLogs(message.channel); + if (!settings.keepLogs && !suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel); Logger.errorCmd(this, error.stack); return message.channel.send(buildErrorEmbed(error)); } @@ -194,7 +191,7 @@ module.exports = class RejectCommand extends Command { `); } - if (!willIgnoreLogs) { + if (suggestionsLogs && !settings.keepLogs) { const missingPermissions = suggestionsLogs.permissionsFor(message.guild.me).missing(logsPermissions); if (missingPermissions.length > 0) return this.client.errors.noChannelPerms(message, suggestionsLogs, missingPermissions); } @@ -215,7 +212,7 @@ module.exports = class RejectCommand extends Command { try { // Will handle suggestion if there's no logs channel set/default value is set OR keepLogs is enabled and a valid logs channel is set - if (sMessage && (willIgnoreLogs || (settings.keepLogs && suggestionsLogs))) { + if (sMessage && settings.keepLogs) { const logEmbed = new MessageEmbed(logsEmbed) .setTitle('Suggestion Rejected'); await sMessage.edit(logEmbed); diff --git a/src/commands/Suggestions/suggest.js b/src/commands/Suggestions/suggest.js index 44f03af..7c55f56 100644 --- a/src/commands/Suggestions/suggest.js +++ b/src/commands/Suggestions/suggest.js @@ -45,7 +45,7 @@ module.exports = class SuggestCommand extends Command { } else { sChannel = suggestionsChannel && await message.guild.channels.fetch(suggestionsChannel); if (!sChannel) return this.client.errors.noSuggestions(message.channel); - if (['text', 'news'].includes(sChannel.type)) return this.client.errors.channelNotFound(sChannel, message.channel); + if (!['text', 'news'].includes(sChannel.type)) return this.client.errors.channelNotFound(sChannel, message.channel); } } catch (e) { return this.client.errors.noSuggestions(message.channel); diff --git a/src/utils/functions.js b/src/utils/functions.js index 713c410..d648cc7 100644 --- a/src/utils/functions.js +++ b/src/utils/functions.js @@ -2,7 +2,7 @@ const fs = require('fs'); const path = require('path'); const petitio = require('petitio'); const { CronJob } = require('cron'); -const { Client, MessageMentions, MessageEmbed, GuildChannel } = require('discord.js-light'); +const { Client, MessageMentions, MessageEmbed, GuildChannel, Message, TextChannel } = require('discord.js-light'); const { execSync } = require('child_process'); const sentry = require('@sentry/node'); @@ -261,13 +261,34 @@ const postToHastebin = (content) => { * @return {Promise<*|GuildChannel|null>} */ const getChannel = async (message, channel) => { + const acceptedTypes = ['text', 'news']; const channels = await message.guild.channels.fetch({ cache: false }) - .then(res => res.filter(c => ['text', 'news'].includes(c.type))); + .then(res => res.filter(c => acceptedTypes.includes(c.type))); return channels.find(c => c.name === channel) || channels.get(channel) || await message.mentions.channels.first()?.fetch() - .then(c => c?.type === 'text' ? c : null); + .then(c => c?.type ? acceptedTypes.includes(c.type) && c : null); +}; + +/** + * Returns the degault logs channel if it exists in the server. + * @param {Message} message - The associated message. + * @param {String} channel - The channel name (if default) or id.. + * @return {Promise} The channel object. + */ +const getLogsChannel = async (message, channel) => { + const acceptedTypes = ['text', 'news']; + const defaultNames = [config.defaultSettings.suggestionsLogs, 'suggestions-logs']; + const isDefault = defaultNames.includes(channel); + + const channels = await message.guild.channels.fetch({ cache: false }) + .then(res => res.filter(c => acceptedTypes.includes(c.type))); + + if (isDefault) return channels.find(c => defaultNames.includes(c.name)); + + return channels.get(channel) || await message.mentions.channels.first()?.fetch() + .then(c => c?.type ? acceptedTypes.includes(c.type) && c : null); }; module.exports = { @@ -285,5 +306,6 @@ module.exports = { reportToSentry, buildErrorEmbed, postToHastebin, - getChannel + getChannel, + getLogsChannel };