如何获得Discord.py中被禁止的人

时间:2020-10-15 18:32:28

标签: python time discord discord.py

正如标题所述,我的问题是,有没有办法让谁禁止某人的用户对象? 可能是这样的:

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}")

2 个答案:

答案 0 :(得分:1)

是的,可以使用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}')

答案 1 :(得分:0)

您可以使用on_member_ban()事件,但是它只有guilduser参数。您无法找到原因,但可以使用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()}.')