【问题标题】:Discord.py making bot respond in certain channelsDiscord.py 使机器人在某些渠道做出响应
【发布时间】:2020-09-27 13:12:33
【问题描述】:

我希望使用我的机器人的用户能够将机器人配置为仅在某些渠道中运行。当我使用on_message 函数通过检查消息来自哪个通道时,我能够实现这一点。现在我正在使用 Cogs,但我不确定如何解决这个问题。

我的旧代码是这样的:

val_channels = ['a', 'b', 'c']
if message.channel in val_channels:
  # Do Something
else:
  print('The bot was configured to: ' + ', '.join(val_channels))

【问题讨论】:

  • 您可以将message.channel 替换为ctx.channel 以获得相同的消息对象。

标签: discord discord.py discord.py-rewrite


【解决方案1】:

您可以验证ctx.message.channel.id 是否与您的列表匹配。

class Greetings(commands.Cog):

    @commands.command()
    async def hello(self, ctx):
        """Says hello"""
        if ctx.message.channel.id in val_channels:
            await ctx.send('Hello!')
        else:
            await ctx.send('You can not use this command here.')

但是,如果您仍想在 cog 中使用 on_message 事件,则需要修改装饰器。 Link to doc

class Greetings(commands.Cog):

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.channel.id in val_channels:
            # Do something
        else:
            print('The bot was configured to: ' + ', '.join(val_channels))

【讨论】:

    猜你喜欢
    • 2023-04-10
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 2021-11-03
    • 2021-01-20
    • 2021-10-06
    • 2019-02-09
    相关资源
    最近更新 更多