簡體   English   中英

向所有通道發送消息 — Discord.py

[英]Sending a message to all channels — Discord.py

如何同時向 discord 服務器中的每個通道發送消息?

我在另一篇文章中使用了此代碼,但在運行命令時沒有收到任何響應。

@client.command(pass_context=True)
async def broadcast(ctx, *, msg):
    for guild in bot.guilds:
        for channel in guild.channels:
            try:
                await bot.send_message(channel, msg)
            except Exception:
                continue
            else:
                break

您在某些地方使用了client ,在其他一些地方使用了bot ,此外,此代碼效率不高,因為當您僅從一台服務器調用它時不需要遍歷行會,這會導致多個垃圾郵件服務器。 我還注意到您正在使用舊版本discord.py中的函數。 嘗試改用這個:

@client.command()
async def broadcast(ctx, *, msg):
    for channel in ctx.guild.text_channels:
        try:
            await channel.send(msg)
        except:
            continue

暫無
暫無

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

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