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

使用Discord.py扩展审计日志:追踪频道位置变更操作者

实现Discord机器人追踪频道位置变更操作者
  • 你之前的问题在于用错了审计日志的动作类型,频道位置变更对应的是discord.AuditLogAction.channel_position_update,而非channel_update。
  • 在on_guild_channel_update事件中,先判断频道位置是否真的发生变化,再拉取对应的审计日志即可获取操作者信息,示例代码如下:
import discord

intents = discord.Intents.default()
intents.guilds = True

client = discord.Client(intents=intents)

@client.event
async def on_guild_channel_update(before, after):
    # 仅当频道位置确实变更时触发后续逻辑
    if before.position != after.position:
        # 拉取最新的1条频道位置变更审计日志
        audit_logs = await after.guild.audit_logs(
            action=discord.AuditLogAction.channel_position_update,
            limit=1
        ).flatten()
        
        if audit_logs:
            entry = audit_logs[0]
            # 打印操作者及变更信息
            print(f"操作者:{entry.user}(ID:{entry.user.id})")
            print(f"频道:{after.name}(ID:{after.id})")
            print(f"位置变更:{before.position} → {after.position}")

client.run("你的机器人Token")
  • 注意事项:
    • 机器人必须拥有查看审计日志的服务器权限,否则无法获取审计日志内容。
    • 若遇到审计日志同步延迟的情况,可以在拉取日志前添加await asyncio.sleep(0.5)来等待日志同步,但多数场景下无需额外等待。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 08:59:50