[英]How To Get The Person Who Banned In Discord.py
正如标题所说,我的问题是,有没有办法让谁禁止某人的用户 object? 它可能是这样的:
async def someone_got_banned(banner, banned, reason, time):
print(f"{banner} has just banned {banned} (The time is {time}), and their reason for doing so is {reason}")
是的,你可以,使用Guild.audit_logs()
下面是修改后的代码
@bot.event
async def on_member_ban(guild, member):
logs = await guild.audit_logs(limit=1, action=discord.AuditLogAction.ban).flatten()
channel = guild.get_channel(CHANNEL_ID)
logs = logs[0]
if logs.target == member:
await channel.send(f'{logs.user} has just banned {logs.target} (The time is {logs.created_at}), and their reason for doing so is {logs.reason}')
您可以使用on_member_ban()
事件,但它只有guild
和user
参数。 您无法获得原因,但您可以使用datetime
模块获得时间。 下面是一个例子:
from datetime import datetime
@client.event
async def on_member_ban(guild, member):
await guild.text_channels[0].send(f'{member} banned at {datetime.now()}.')
在此代码中,它将消息发送到第一个文本通道。 如果要更改频道,可以使用discord.utils.get
定义获取频道。
from datetime import datetime
@client.event
async def on_member_ban(guild, member):
channel = discord.utils.get(guild.text_channels, name='the channel name that the message will send')
await channel.send(f'{member} banned at {datetime.now()}.')
@bot.event async def on_member_ban(guild, user): logs = [log async for log in guild.audit_logs(limit=1, action=discord.AuditLogAction.ban)] logs = logs[0] print('logs: ', logs) # printing the logs print(logs.user) # the admin the banned the user print(log.target) # the user that got print(logs.created_at) # ban time print(logs.reason) # reason, it will return None if the admin didn't state a reason
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.