discord - 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
解决方案
如果你使用
for guild in client.guilds:
channel = discord.utils.get(guild.text_channels, name="general")
if channel != None:
#do something
您可以从每个公会获得#general 频道。那将只是一个频道(Discord 允许多个频道共享名称,但大多数人不会这样做,并且.get()
无论如何只会返回第一个)。
这比遍历所有频道要好得多,也比选择随机频道要好一些,但我仍然会考虑让公会设置自己的广播接收频道。
经过测试,它可以工作,尽管我的机器人只存在于一台服务器中,但我不知道尝试在 100 多台服务器上执行此操作是否会产生未知后果。
推荐阅读
- ios - 防止 AVPlayerController 劫持 MPRemoteCommandCenter
- spring-boot - 如何使用 gradle 5 和 kts 导入 spring-boot-dependenies pom?
- autodesk-forge - 将 .rvt 转换为 .svf 时缺少 3d 视图
- ios - React 本机人脸检测对我不起作用(iOS)
- html - 可滚动的多嵌套 Flexbox,父子元素都可滚动
- html - 跨度不加载动画
- bash - 使用引号时的 Bash 日期命令和额外空间
- android - Android Studio 在哪里存储签名的 APK?
- php - 如何访问树枝中另一个实体内部的实体?
- ip - 以太网帧大小限制(有效负载)