如何修改discord.py的banlogs命令 使封禁日志输出为单个Embed列表
discord.py banlogs 命令合并Embed修改方案
修改后的完整代码
@client.command(aliases=['bl', 'bans', 'banslog', 'banslogs', 'banlog']) async def banlogs(ctx): guild = ctx.guild bans = await guild.bans() # 循环外全局初始化唯一Embed embed = discord.Embed(title="Ban Logs", colour=discord.Color.red()) if not bans: embed.description = "当前服务器暂无任何封禁记录" else: for index, ban in enumerate(bans, start=1): # 拼接单条封禁的所有信息 ban_content = f""" 用户名:{ban.user} 用户ID:{ban.user.id} 是否为机器人:{'是' if ban.user.bot else '否'} 封禁原因:{ban.reason if ban.reason else '未填写原因'} """ # 每条封禁记录单独作为一个字段,关闭inline避免同行排列 embed.add_field(name=f"封禁记录 {index}", value=ban_content.strip(), inline=False) # 所有信息拼接完成后统一发送一次 await ctx.send(embed=embed)
关键修改说明
- 将Embed的初始化和发送逻辑从
for循环内部移到外部,全程只生成一个Embed、只发送一次 - 新增空封禁列表的特殊处理,避免发送内容为空的Embed
- 用
enumerate给每条封禁记录增加编号,方便查阅 - 优化字段展示逻辑,把单用户的所有封禁信息整合到同一个字段的value中,排版更规整
- 修正原代码中注释的用户ID字段写法,同时对空封禁原因做默认值处理,避免输出无效的
None内容 - 字段
inline参数设为False,保证每条封禁记录单独占一行,不会出现排版混乱问题
内容的提问来源于stack exchange,提问作者Iam pro
相关产品推荐
相关产品推荐

