简体   繁体   中英

How can I restrict a bot to responding in certain channels with discord.py?

I am making a bot that is using cogs and has a couple different commands. I want this bot to only reply in one of the two bot commands channels of the server I'm using it on. I have seen that i can use ctx.channel.id = whatever the Id is, but i would prefer the bot to not be able to respond in the channel at all, including to.help commands. I have seen people do this with on_message, but I'm not sure how I would do that with cogs. Any help would be much appreciated. My intended result is basically to have the bot only respond in two channels, the two bot channels that i specify, to any commands including the.help command. Thanks!

The easiest way to do this is not actually via code but via permissions on the server. On your server you should find a role with the same name as your bot, whose permissions (including send messages ) you can change for seperate channels.

As @Lu M said, the easiest way to do this would be to disable permissions for your bot. But if you want to do this in your code, you can use if statements to check what the channel ID is.

if ctx.channel.id == <channel_id_here>:
   return 
   # if this if statement returns True, this will stop the command from doing 
   further actions.

else:
   # do stuff here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM