【问题标题】:Discord bot listen to commands on specific channelDiscord bot 侦听特定频道上的命令
【发布时间】:2018-12-01 23:54:46
【问题描述】:

我的 discord 机器人中有一堆命令,我想做的是让机器人仅在某些命令来自特定频道时才听这些命令。

这是一个命令示例:

@bot.command(name='bitcoin',
                brief="Shows bitcoin price for nerds.")
async def bitcoin(pass_context=True):
    url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
    response = requests.get(url)
    value = response.json()['bpi']['USD']['rate']
    await bot.send_message(discord.Object(id='<channel id is inserted here>'), "Bitcoin price is: " + value)
    # await bot.say("Bitcoin price is: " + value)

我可以在我想要的特定渠道中给出答案,但我希望机器人仅在特定渠道中触发命令时才回复,而不是在任何地方。

我尝试了 if/else 与 if message.channel.id = 'id' 但它不起作用。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你可以写一个check 来装饰你的命令。下面我们使用我们的目标频道 ID 创建一个check,然后使用该检查来装饰我们的命令。

    def in_channel(channel_id)
        def predicate(ctx):
            return ctx.message.channel.id == channel_id
        return commands.check(predicate)
    
    @bot.command(name='bitcoin', brief="Shows bitcoin price for nerds.")
    @is_channel('CHANNEL_ID')
    async def bitcoin(pass_context=True):
        url = 'https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
        response = requests.get(url)
        value = response.json()['bpi']['USD']['rate']
        await bot.send_message(discord.Object(id='<channel id is inserted here>'), "Bitcoin price is: " + value)
        # await bot.say("Bitcoin price is: " + value)
    

    【讨论】:

      猜你喜欢
      • 2020-02-11
      • 2020-10-01
      • 2020-05-17
      • 2023-03-27
      • 2022-10-23
      • 2019-05-05
      • 2021-11-11
      • 2018-03-04
      • 1970-01-01
      相关资源
      最近更新 更多