【问题标题】:how to send messages all channel如何在所有频道发送消息
【发布时间】:2020-01-30 22:21:42
【问题描述】:

我想向机器人加入的所有频道发送消息

def check(cnt): 
    while 1:  
        if hash_origin != str(subprocess.check_output(["md5sum",filename])[:-len(filename)-3])[2:-1] :
            print("file destroyed alert!")
            alert = 1
        sleep(0.5)

当特定文件的哈希结果与原始文件不同时,我想向机器人加入的所有不和谐频道发送消息。 我知道如何向频道发送响应消息

@client.event
async def on_message(message):

使用此代码,对吗? 但我想在某些事件发生时向机器人加入的所有频道发送消息。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    为了向机器人所在的每个频道发送消息,您必须执行以下操作:

    @client.event
    async def foo(bar): # change this to the event in which you wish to call it from
        for guild in client.guilds:
            for channel in guild.channels:
                await channel.send(messagedata) # change messagedata to whatever it is you want to send.
    

    【讨论】:

    • 直接使用此代码时,发生错误 AttributeError: 'CategoryChannel' object has no attribute 'send' 我正在使用这样的代码。
    【解决方案2】:
    @client.event
    async def foo(bar): # change this to the event in which you wish to call it from
        for guild in client.guilds:
            for channel in guild.channels:
                await channel.send(messagedata) # change messagedata to whatever it is you want to send.
    

    直接使用这段代码,出现错误

    AttributeError: 'CategoryChannel' object has no attribute 'send'  
    

    因为“guild.channels”对象具有机器人加入的“所有频道”(包括文本、语音等)的列表。 所以,如果我想向频道发送短信,请改用“guild.text_channels”。 无论如何,感谢泰勒!!

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2021-01-01
      • 1970-01-01
      • 2021-04-12
      • 2021-03-24
      相关资源
      最近更新 更多