首页 > 解决方案 > Discord Python 重写 - 广播

问题描述

是否可以让机器人在机器人所在的每台服务器上发送 1 条消息而不将消息发送到每个通道?我有一个工作代码,但它发送到所有频道

@client.command()
async def broadcast(ctx, *, message):
    for guild in client.guilds:
        for channel in guild.channels:
            
            if guild.name == ctx.author.guild.name:
                pass

            else:
                try:
                    await channel.send(message)
                    await ctx.send(
                        'Sent to {} (ID: {}, Owner: {}#{} With {} Members)'.format(
                            guild.name,
                            guild.id,
                            guild.owner.name,
                            guild.owner.discriminator,
                            guild.member_count
                            ))

                except:
                await ctx.send(
                    'Could not send at {} (Channel ID: {}). (Owner: {}#{})'.format(
                        guild.name,
                        channel.id,
                        guild.owner.name,
                        guild.owner.discriminator,
                        ))
                    return

                else:
                    break
please help

标签: discorddiscord.pydiscord.py-rewrite

解决方案


如果你使用

for guild in client.guilds:
    channel = discord.utils.get(guild.text_channels, name="general")
    if channel != None:
       #do something

您可以从每个公会获得#general 频道。那将只是一个频道(Discord 允许多个频道共享名称,但大多数人不会这样做,并且.get()无论如何只会返回第一个)。
这比遍历所有频道要好得多,也比选择随机频道要好一些,但我仍然会考虑让公会设置自己的广播接收频道。

经过测试,它可以工作,尽管我的机器人只存在于一台服务器中,但我不知道尝试在 100 多台服务器上执行此操作是否会产生未知后果。


推荐阅读