(Discord.py) 向所有公会发送消息
(Discord.py) Send a message to all guilds
我一直在尝试向我的机器人所在的所有公会发送相同的消息,但没有任何效果,而且我无法在文档中找到类似的内容。这可能吗?我该怎么做?
编辑:好的,让我们想象一下我的机器人正在检查 BBC 或任何其他新闻网页。该机器人将每五分钟检查一次网络,并将新闻链接发送到执行 .start(例如)的公会。我实现这个的方法是通过一个命令为每个公会生成一个循环,我的想法是每个公会只有一个循环。
for guild in bot.guilds:
await guild.text_channels[0].send(<message>)
这将获取公会中找到的第一个文本频道并向其发送消息。
您可以通过键入 ctx.bot
从上下文变量中获取机器人。
class send_all(commands.Cog):
def __init__(self,bot):
self.bot = bot
@commands.command()
async def send(self,ctx,*message_to_send):
guild = ctx.message.guild
output = ' '
author = ctx.message.author
for word in message_to_send:
output += word
output += ' '
for member in self.bot.get_all_members():
try:
embed = discord.Embed(title="",colour = discord.Colour.green())
embed.add_field(name="**From server:**",value= guild.name)
embed.add_field(name = "**From Mod/Admin:**",value = author.name)
embed.add_field(name="**Message:**",value = output)
# await ctx.send(embed=embed)
await member.send(embed=embed)
except (discord.HTTPException, discord.Forbidden,AttributeError):
continue
我一直在尝试向我的机器人所在的所有公会发送相同的消息,但没有任何效果,而且我无法在文档中找到类似的内容。这可能吗?我该怎么做?
编辑:好的,让我们想象一下我的机器人正在检查 BBC 或任何其他新闻网页。该机器人将每五分钟检查一次网络,并将新闻链接发送到执行 .start(例如)的公会。我实现这个的方法是通过一个命令为每个公会生成一个循环,我的想法是每个公会只有一个循环。
for guild in bot.guilds:
await guild.text_channels[0].send(<message>)
这将获取公会中找到的第一个文本频道并向其发送消息。
您可以通过键入 ctx.bot
从上下文变量中获取机器人。
class send_all(commands.Cog):
def __init__(self,bot):
self.bot = bot
@commands.command()
async def send(self,ctx,*message_to_send):
guild = ctx.message.guild
output = ' '
author = ctx.message.author
for word in message_to_send:
output += word
output += ' '
for member in self.bot.get_all_members():
try:
embed = discord.Embed(title="",colour = discord.Colour.green())
embed.add_field(name="**From server:**",value= guild.name)
embed.add_field(name = "**From Mod/Admin:**",value = author.name)
embed.add_field(name="**Message:**",value = output)
# await ctx.send(embed=embed)
await member.send(embed=embed)
except (discord.HTTPException, discord.Forbidden,AttributeError):
continue