LilWane
LilWane

Reputation: 13

Check if command is ran in certain channel

I currently have this as my command:

bot.on('message', function (message) {
  if (message.content == '!register') {
    message.member.send("Registered!");
    let memberRole = message.member.guild.roles.find("name", "Verified");
    message.member.addRole(memberRole);
  }
});

I want it so this command can only be ran in a text channel called registration (I have the channel id if needed).

Upvotes: 1

Views: 2333

Answers (1)

Gilles Heinesch
Gilles Heinesch

Reputation: 2980

Here is the code if you only have one textchannel called registration:

bot.on('message', function (message) {
  if (message.content == '!register' && message.channel.name.toLowerCase() === 'registration') {
    message.member.send("Registered!");
    let memberRole = message.member.guild.roles.find("name", "Verified");
    message.member.addRole(memberRole);
  }
});

If you have two textchannels called registration, I would check the ID of the channel. This can be done with this code:

bot.on('message', function (message) {
  if (message.content == '!register' && message.channel.id === 'YOUR CHANNEL ID') {
    message.member.send("Registered!");
    let memberRole = message.member.guild.roles.find("name", "Verified");
    message.member.addRole(memberRole);
  }
});

By the way, you don't have to use message.member to receive the guild object. You can simply do message.guild!

Upvotes: 1

Related Questions