Discord.py - 如何接收和发送消息到特定频道?

Thr*_*t86 3 python discord discord.py

昨天我正在做一些简单的事情,其中​​一个机器人!name Barty会打印回来Hello Barty

@bot.command()
async def name(ctx, args):
    await ctx.send("hello {}".format(args)
Run Code Online (Sandbox Code Playgroud)

然而,我此时面临的问题是机器人会响应我使用的任何频道,!name XXXX而我想要做的是我只想对不和谐的给定特定频道做出反应。

我试图做:

@bot.event
async def on_message(message):

    if message.channel.id == 1234567:

        @bot.command()
        async def name(ctx, args):
            await ctx.send("hello {}".format(args)
Run Code Online (Sandbox Code Playgroud)

但这完全行不通,我没有想法,我在这里。

如何向给定的特定通道发送命令并从那里获得响应?

小智 6

呃,伙计,将定义代码移出 IF 语句

@bot.command()
  async def name(ctx, args):
  await ctx.send("hello {}".format(args)
Run Code Online (Sandbox Code Playgroud)

当你这样做时,你应该能够做到;

if (message.channel.id == 'channel id'): 
  await message.channel.send('message goes here')

  else:
    # handle your else here, such as null, or log it to ur terminal
Run Code Online (Sandbox Code Playgroud)

也可以查看你知道的文档 - https://discordpy.readthedocs.io

执行命令后,在其中执行 IF 语句。