1. <legend id='kPjAY'><style id='kPjAY'><dir id='kPjAY'><q id='kPjAY'></q></dir></style></legend>

    <small id='kPjAY'></small><noframes id='kPjAY'>

    <tfoot id='kPjAY'></tfoot>
      <bdo id='kPjAY'></bdo><ul id='kPjAY'></ul>

    1. <i id='kPjAY'><tr id='kPjAY'><dt id='kPjAY'><q id='kPjAY'><span id='kPjAY'><b id='kPjAY'><form id='kPjAY'><ins id='kPjAY'></ins><ul id='kPjAY'></ul><sub id='kPjAY'></sub></form><legend id='kPjAY'></legend><bdo id='kPjAY'><pre id='kPjAY'><center id='kPjAY'></center></pre></bdo></b><th id='kPjAY'></th></span></q></dt></tr></i><div id='kPjAY'><tfoot id='kPjAY'></tfoot><dl id='kPjAY'><fieldset id='kPjAY'></fieldset></dl></div>

      Discord bot 侦听特定频道上的命令

      时间:2023-10-10

      <small id='ZkJl2'></small><noframes id='ZkJl2'>

      <legend id='ZkJl2'><style id='ZkJl2'><dir id='ZkJl2'><q id='ZkJl2'></q></dir></style></legend>
          <tbody id='ZkJl2'></tbody>
        <tfoot id='ZkJl2'></tfoot>
        • <bdo id='ZkJl2'></bdo><ul id='ZkJl2'></ul>
          <i id='ZkJl2'><tr id='ZkJl2'><dt id='ZkJl2'><q id='ZkJl2'><span id='ZkJl2'><b id='ZkJl2'><form id='ZkJl2'><ins id='ZkJl2'></ins><ul id='ZkJl2'></ul><sub id='ZkJl2'></sub></form><legend id='ZkJl2'></legend><bdo id='ZkJl2'><pre id='ZkJl2'><center id='ZkJl2'></center></pre></bdo></b><th id='ZkJl2'></th></span></q></dt></tr></i><div id='ZkJl2'><tfoot id='ZkJl2'></tfoot><dl id='ZkJl2'><fieldset id='ZkJl2'></fieldset></dl></div>

              • 本文介绍了Discord bot 侦听特定频道上的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

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

                I have a bunch of commands in my discord bot and what I am trying to do is to make the bot listen to some commands ONLY if they come from a specific channel.

                这是一个命令示例:

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

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

                I can give the answer in a specific channel that I want, but I want the bot to only reply if the command is triggered in a specific channel, not everywhere.

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

                I tried a if/else with if message.channel.id = 'id' but it doesn't work.

                推荐答案

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

                You can write a check that you can use to decorate your command. Below we create a check using our target channel id, and then decorate our command with that 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)
                

                这篇关于Discord bot 侦听特定频道上的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Discord.py 获取消息嵌入 下一篇:当我的机器人发送时,Discord 没有嵌入链接

                相关文章

                最新文章

                <i id='9OnYN'><tr id='9OnYN'><dt id='9OnYN'><q id='9OnYN'><span id='9OnYN'><b id='9OnYN'><form id='9OnYN'><ins id='9OnYN'></ins><ul id='9OnYN'></ul><sub id='9OnYN'></sub></form><legend id='9OnYN'></legend><bdo id='9OnYN'><pre id='9OnYN'><center id='9OnYN'></center></pre></bdo></b><th id='9OnYN'></th></span></q></dt></tr></i><div id='9OnYN'><tfoot id='9OnYN'></tfoot><dl id='9OnYN'><fieldset id='9OnYN'></fieldset></dl></div>

                  1. <tfoot id='9OnYN'></tfoot>
                  2. <legend id='9OnYN'><style id='9OnYN'><dir id='9OnYN'><q id='9OnYN'></q></dir></style></legend>
                      <bdo id='9OnYN'></bdo><ul id='9OnYN'></ul>
                  3. <small id='9OnYN'></small><noframes id='9OnYN'>