首页 文章

Discord bot在特定 Channels 上收听命令

提问于
浏览
0

我在discord bot中有一堆命令,我想做的是让bot只听一些命令,如果它们来自特定的 Channels .

以下是命令的示例:

@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)

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

如果message.channel.id ='id'我尝试过if / else,但它不起作用 .

1 回答

  • 0

    您可以编写一个 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)
    

相关问题