I want my bot to be able to check...
THEN
The bot would return the action with the message Mod mail is already activated here!
Here's my code
var mg = message.guild
const mythings = mg.channels.filter(chane => chane.client.user.id === `595840576386236437` && chane.name === 'modmail' &&chane.type === `text`);
const mythings2 = mg.channels.filter(chane => chane.client.user.id === `595840576386236437` && chane.name === 'qanda' &&chane.type === `text`);
const mythings3 = mg.roles.filter(role => role.client.user.id === `595840576386236437` && role.name === 'ModMail');
console.log(mythings)
if(mythings) return message.channel.send(`Rejected! There's possible fragments of modmail already set up here!`)
if(mythings2) return message.channel.send(`Rejected! There's possible fragments of modmail already set up here!`)
if(!mythings3) return message.channel.send(`Rejected! There's possible fragments of modmail already set up here!`)
NOTE
I am doing in this in discord.js-commando, not discord.js so this is not in my index.js file so my only limitation is I can't refer to the client variable. Directory is ~/commands/extras/modmail.js/ instead of ~/index.js/ What I mean by this is I cannot start with the client
variable because it will return to me as undefined, but something like channel.client
would be allowed because it's defining the creator of the channel the messgae was sent in.
What went wrong
Here's what went wrong.
OR
I expect: Bot can search the guild for a channel that meets client.id AND name expectations and then return. Also, Bot can search the guild for a role that meets the owner (my(Referring to me being the bot)) id AND name and then return also.
The actual result is the bot returning when it's not supposed to, e.g. Bot returns when it doesn't find a channel when it is supposed to create one. (I cut out the code for it though)
Collection.filter()
will always return a new Collection. Therefore, your first condition is always returning true and executing the return
statement because the variable exists.
Either check the size
property of the Collection, or use Collection.find()
which will only return an element if the predicate function returns true.
Also, you'll have to go through the Audit Logs to check the creator of a channel. The instantiator is the client that created the instance of the object, which is not equivalent to actually creating the channel itself.
// Async context (meaning within an async function) needed to use 'await'
try {
const channelLogs = await mg.fetchAuditLogs({ user: '595840576386236437', type: 10 });
const myChannels = channelLogs.entries.filter(e => e.target && ['modmail', 'qanda'].includes(e.target.name) && e.target.type === 'text');
const roleLogs = await mg.fetchAuditLogs({ user: '595840576386236437', type: 30 });
const myRole = roleLogs.entries.find(e => e.target && e.target.name === 'Modmail');
if (myChannels.size !== 0 || myRole) {
return await message.channel.send('Rejected! There\'s possible fragments of modmail already set up here!');
}
} catch(err) {
console.error(err);
}