0
votes

I want my bot to be able to check...

  • If a channel my bot made matches a certain name... OR
  • If a role my bot made matches a certain name...

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.

  1. Bot doesn't find the channel channel
  2. Bot returns

OR

  1. Bot finds channel
  2. Bot continues to make all the channels and roles.

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)

1
"...my only limitation is I can't refer to the client variable." You're using client in the code snippet?slothiful
As in the client that initated the channel / role, not client as in referring to the bot.login and other actions the bot does in the index.js file. It means that I can't start with client because it is undefined but chane.client isn't.SomePerson

1 Answers

0
votes

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);
}