我的代码的任务: 当主持人发布或删除任何服务器参与者的角色时,机器人会读取日志,然后向指定的频道发送一条消息,说明哪个角色被更改了,等等。
我的问题: 当主持人一次添加或删除多个角色时,机器人会立即发送一条消息,其中包含每个角色的信息。但我希望在发送事件时有延迟。这会阻塞聊天记录并且很烦人。
例如: 我一次删除了 5 个角色,机器人有 30 秒的延迟发送消息。在这条消息中,他添加了所有 5 个角色,而不是一次添加一个。
代码:
@Bot.event
async def on_member_update(before, after):
if before.roles != after.roles:
channel = Bot.get_channel(827986763606786099)
emb = discord.Embed(description = f'**Updating user roles - {before.mention}**', colour = discord.Color.red())
emb.add_field(name = '**Roles before**', value = ", ".join([r.mention for r in before.roles]))
emb.add_field(name = '**Roles after**', value = ", ".join([r.mention for r in after.roles]))
async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
if getattr(event.target, "id", None) != before.id:
continue
emb.add_field(name="Changed roles", value = ", ".join([getattr(r, "mention", r.id) for r in event.before.roles or event.after.roles]))
emb.add_field(name="Moderator", value = event.user)
break
await channel.send(embed = emb)
答案 0 :(得分:1)
运行前一定要导入 asyncio import asyncio
cooldown = []
@bot.event()
async def on_member_update(before, after):
if before.roles != after.roles:
global cooldown
if before in cooldown:
return
cooldown.append(before)
await asyncio.sleep(10) #here you can change how long the cooldown should be
cooldown.remove(before)
channel = bot.get_channel(688344722082627686)
emb = discord.Embed(description=f'**Updating user roles - {before.mention}**', colour=discord.Color.red())
emb.add_field(name='**Roles before**', value=", ".join([r.mention for r in before.roles]))
emb.add_field(name='**Roles after**', value=", ".join([r.mention for r in after.roles]))
changed_roles = []
for role in before.roles:
if role in after.roles:
pass
else:
changed_roles.append(role)
for role in after.roles:
if role in before.roles:
pass
else:
if role in changed_roles:
pass
else:
changed_roles.append(role)
text = ""
for role in changed_roles:
text = text + role.mention
emb.add_field(name="Changed roles", value=text)
async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
if getattr(event.target, "id", None) != before.id:
continue
emb.add_field(name="Moderator", value=event.user)
break
await channel.send(embed=emb)
不得不稍微改变一下角色的变化,因为我无法获得应该获取多少审计日志。
那么会发生什么: 用户被添加到冷却列表中,bot 等待 10 秒,以便主持人可以完成删除/添加角色,在该 bot 收集所有角色后,将用户从冷却中删除并发送嵌入。
答案 1 :(得分:0)
这是完整的工作代码:
@Bot.event
async def on_member_update(before, after):
if before.roles != after.roles:
global cooldown
if before in cooldown:
return
cooldown.append(before)
await asyncio.sleep(5) # here you can change how long the cooldown should be
cooldown.remove(before)
channel = Bot.get_channel(ID log channel)
emb = discord.Embed(description=f'**Updating user roles - {before.mention}**', colour=discord.Color.orange())
emb.add_field(name='Roles before', value=", ".join([r.mention for r in before.roles][1:]), inline=False)
emb.add_field(name='Roles after', value=", ".join([r.mention for r in after.roles][1:]), inline=False)
changed_roles = []
for role in before.roles:
if role in after.roles:
pass
else:
changed_roles.append(role)
for role in after.roles:
if role in before.roles:
pass
else:
if role in changed_roles:
pass
else:
changed_roles.append(role)
text = ""
blacklist=[797920206407598098,817750571330961410,797916381621256202] # list roles, that should not be read
for role in changed_roles:
if role.id in blacklist:
return
text = text + role.mention
emb.add_field(name="Changed roles", value=text, inline=False)
async for event in before.guild.audit_logs(limit=1, action=discord.AuditLogAction.member_role_update):
if getattr(event.target, "id", None) != before.id:
continue
emb.add_field(name="Moderator", value=f"{event.user} \n**ID**: {event.user.id}")
break
await channel.send(embed=emb)
非常感谢您帮助@NimVrod