Python Discord bot侦听特定通道上的命令

Python Discord bot侦听特定通道上的命令,python,discord,discord.py,Python,Discord,Discord.py,我的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'

我的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)
@bot.command(name='bitcoin',
brief=“向书呆子展示比特币价格。”)
异步def比特币(pass_context=True):
url='1〕https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
response=requests.get(url)
value=response.json()
等待bot.send_消息(discord.Object(id=“”),“比特币价格为:”+value)
#等待机器人说(“比特币价格:”+价值)
我可以在我想要的特定通道中给出答案,但我希望bot只在命令在特定通道中触发时才回复,而不是在任何地方


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

您可以编写一个
检查
,用于修饰您的命令。下面,我们使用目标通道id创建一个
检查
,然后用该检查装饰命令

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)
def输入通道(通道id)
定义谓词(ctx):
返回ctx.message.channel.id==channel\u id
返回命令。检查(谓词)
@命令(name='bitcoin',brief=“为书呆子显示比特币价格。”)
@is_通道(“通道ID”)
异步def比特币(pass_context=True):
url='1〕https://api.coindesk.com/v1/bpi/currentprice/BTC.json'
response=requests.get(url)
value=response.json()
等待bot.send_消息(discord.Object(id=“”),“比特币价格为:”+value)
#等待机器人说(“比特币价格:”+价值)