[英]Discord.py - How would I go about making a command that will allow server admins to block the bot from responding in specified channels?
[英]How can I restrict a bot to responding in certain channels with discord.py?
我正在制作一個使用 cogs 並有幾個不同命令的機器人。 我希望這個機器人只在我使用它的服務器的兩個機器人命令通道之一中回復。 我已經看到我可以使用 ctx.channel.id = 不管 Id 是什么,但我希望機器人根本無法在頻道中響應,包括 to.help 命令。 我見過人們用 on_message 做這個,但我不確定我會如何用 cogs 做這個。 任何幫助將非常感激。 我的預期結果基本上是讓機器人只在兩個通道(我指定的兩個機器人通道)中響應任何命令,包括 .help 命令。 謝謝!
最簡單的方法實際上不是通過代碼,而是通過服務器上的權限。 在您的服務器上,您應該找到一個與您的機器人同名的角色,您可以為單獨的頻道更改其權限(包括send messages
)。
正如@Lu M 所說,最簡單的方法是禁用您的機器人的權限。 但是,如果您想在代碼中執行此操作,您可以使用 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.