Check if command is ran in certain channel

时光怂恿深爱的人放手 提交于 2021-02-11 06:48:46

问题


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).


回答1:


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!



来源:https://stackoverflow.com/questions/52045687/check-if-command-is-ran-in-certain-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!