如何阻止我的机器人发送不和谐的垃圾邮件.py



我正在制作一个设置命令。一切正常,但命令发送垃圾邮件,而不是发送一次

@kara.command()
async def setup(ctx):
guild = ctx.guild
await ctx.message.delete()
await guild.create_role(name=silenced, permissions = discord.Permissions(send_messages=False), reason=f"Invoked by {ctx.author}")
role = discord.utils.get(guild.roles, name = "Silenced")
for chan in guild.channels:
await chan.set_permissions(silenced, send_messages=False)
setup=discord.Embed(title="Kara's setup", color=c.teal(), url=rick_roll, description="If the setup goes wrong make sure I have the correct permissions!")
setup.add_field(name="Silenced Role", value="Setting up my own muted role...")
msg = await ctx.send(embed=setup)

setup2=discord.Embed(title="Kara's setup", color=c.teal(), url=rick_roll, description="If the setup goes wrong make sure I have the correct permissions!")
setup2.add_field(name="Silenced Role", value="Setting up my own muted role...")
setup2.add_field(name="Coming Soon...", value="Currently this is all to the setup soo... yea")
msg = await ctx.send(embed=setup)
await asyncio.sleep(3.4)
await msg.edit(embed=setup2)

它只是垃圾邮件设置,我已经尝试将发送移动到for之外,但它不起作用,任何帮助都很感激。

有几个选项可以尝试:

您应该在await msg.edit(embed=setup2)下添加一个break

另一个原因可能是您的on_message事件未命中await bot.process_commands(message)。请注意,这应该与async def on_message(msg):的缩进相匹配。只需确保在活动结束时添加它即可。

正确的解决方案需要解释为什么在for循环中使用*.send()是不好的。

在for循环中,程序在可迭代对象上进行迭代。在这个例子中是一个列表。如果可迭代项包含1个以上的项,则for循环中的代码将运行多次。

你描述的行为;垃圾邮件";,是由具有1个以上项目的迭代引起的,特别是ctx.guild.channels,它包含表示discord服务器中频道的频道对象。

解决方案

你应该完成你对这些频道的任何处理。(即(很好地格式化它们,完成后,建议在for循环之外使用*.send()

最新更新