You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py如何让ban命令生成的Embed消息发送到多个频道

你只需要两步即可实现Embed同步发送到管理员日志频道的需求:

  1. 提前获取管理员专属日志频道的ID:在Discord客户端开启开发者模式后,右键点击目标日志频道,选择「复制ID」即可。
  2. 在现有ban命令代码中新增获取日志频道对象、发送Embed的逻辑即可,修改后的代码示例如下:
@commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason="No reason provided"):
        await user.ban(reason=reason)
        ban = discord.Embed(title=f"User {user.name}#{user.discriminator} has been **banned** from the server. <a:m_verifyblack:850825891780100096>", color=0xff0000, description=f"Reason: {reason}\nBy: {ctx.author.mention}")
        await ctx.message.delete()
        # 原有触发频道发送逻辑保留
        await ctx.channel.send(embed=ban)
        # 原有用户私聊通知逻辑保留
        await user.send(embed=ban)
        # 新增:发送Embed到管理员日志频道
        # 替换下方括号内的数字为你实际的日志频道ID
        log_channel = ctx.guild.get_channel(123456789012345678)
        # 增加非空判断避免频道ID填错/机器人无权限时抛出异常
        if log_channel:
            await log_channel.send(embed=ban)

注意事项

  • 请确认你的机器人在日志频道拥有「发送消息」「嵌入链接」两个权限,否则会发送失败。
  • 如果你的机器人需要适配多个服务器,可以将不同服务器的日志频道ID存储到配置文件或数据库中,根据ctx.guild.id动态读取对应服务器的日志频道ID即可。

内容的提问来源于stack exchange,提问作者miller3513

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 22:27:02