【问题标题】:allocating bot to single channel discord.py将机器人分配给单通道 discord.py
【发布时间】:2019-04-17 19:37:53
【问题描述】:

我想确保我的机器人仅响应命令/消息并且仅在 1 个特定频道中响应这可能吗?我尝试了多种变体但均未成功。如果我可以为任何事件定义它,那就更好了。有人有什么想法吗?

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    您可以在on_message 事件中检查message.channel,如果它符合您的条件,在本例中为特定频道,则执行process_commands

    下面是一个示例,其中!ping 命令仅在channel.name 为“通用”时才有效。

    from discord.ext import commands
    
    client = commands.Bot(command_prefix='!')
    
    @client.command()
    async def ping():
        await client.say('Pong')
    
    @client.event
    async def on_message(message):
        if message.channel.name == 'general':
            await client.process_commands(message)
    
    client.run('token')
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-26
      • 2020-12-18
      • 1970-01-01
      • 2021-02-21
      • 2018-06-16
      • 2019-04-08
      • 2021-05-03
      • 2020-09-06
      相关资源
      最近更新 更多