簡體   English   中英

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

[英]Discord.py - how to receive and send message to specific channel?

昨天我正在做一些簡單的事情,其中​​一個命令!name Barty的機器人會打印回Hello Barty

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

然而,我此時面臨的問題是機器人會響應我使用!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)

但這完全不起作用,我沒有想法,我在這里。

如何向給定的特定通道發送命令並從那里獲得響應?

呃,伙計,將定義代碼移出 IF 語句

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

當你這樣做時,你應該能夠做到;

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

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

執行命令后,在其中執行 IF 語句。

message.channel.id 現在需要是一個整數,而不是一個字符串,所以你不需要使用 ''

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

  else:
    # handle your else here, such as null, or log in to ur terminal

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM