简体   繁体   English

如何使用 discord.py 限制机器人在某些通道中响应?

[英]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.我正在制作一个使用 cogs 并有几个不同命令的机器人。 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.我已经看到我可以使用 ctx.channel.id = 不管 Id 是什么,但我希望机器人根本无法在频道中响应,包括 to.help 命令。 I have seen people do this with on_message, but I'm not sure how I would do that with cogs.我见过人们用 on_message 做这个,但我不确定我会如何用 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.我的预期结果基本上是让机器人只在两个通道(我指定的两个机器人通道)中响应任何命令,包括 .help 命令。 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.在您的服务器上,您应该找到一个与您的机器人同名的角色,您可以为单独的频道更改其权限(包括send messages )。

As @Lu M said, the easiest way to do this would be to disable permissions for your bot.正如@Lu M 所说,最简单的方法是禁用您的机器人的权限。 But if you want to do this in your code, you can use if statements to check what the channel ID is.但是,如果您想在代码中执行此操作,您可以使用 if 语句来检查通道 ID 是什么。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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