M
M
multiapi2020-07-09 05:32:22
Python
multiapi, 2020-07-09 05:32:22

How to add a couple of functions to on_member_update?

Hello, I ran into the problem of adding two necessary functions to on_member_update, I need to make it so that it would be written who issued the role, and what role was issued, in fact, after reading the docks, I found that on_member_update only before, after, as I actually say add function data?

@client.event
async def on_member_update(before, after):
    if before.roles != after.roles:
        channel = client.get_channel(729733881129074768)
        emb = discord.Embed(title = '', description = f'**Обновление ролей пользователя -  {before.mention}**', colour = discord.Color.red())
        emb.add_field(name = '**Роли до**', value = [r.mention for r in before.roles] ) 
        emb.add_field(name = '**Роли после**', value = [r.mention for r in  after.roles] ) 

        await channel.send(embed = emb)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Максим Невзоров, 2020-07-09
@multiapi

Получать данные из лога аудита.

@client.event
async def on_member_update(before, after):
    if before.roles != after.roles:
        channel = client.get_channel(729733881129074768)
        emb = discord.Embed(description = f'**Обновление ролей пользователя -  {before.mention}**', colour = discord.Color.red())
        emb.add_field(name = '**Роли до**', value = ", ".join([r.mention for r in before.roles])) 
        emb.add_field(name = '**Роли после**', 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): # https://discordpy.readthedocs.io/en/v1.3.4/api.html#discord.AuditLogAction.member_role_update
            # event: AuditLogEntry — https://discordpy.readthedocs.io/en/v1.3.4/api.html#discord.AuditLogEntry
            if getattr(event.target, "id", None) != before.id:
                # изменение ролей пользователя прошло без записи в логах аудита, или в лог аудита попала другая запись перед выполнением текущего участка кода
                continue
            emb.add_field(name="Изменённые роли", value = ", ".join([getattr(r, "mention", r.id) for r in event.before.roles or event.after.roles]))  # event.before, event.after: AuditLogDiff — https://discordpy.readthedocs.io/en/v1.3.4/api.html#discord.AuditLogDiff 
            emb.add_field(name="Модератор", value = event.user)
            break
        await channel.send(embed = emb)

R
RAINGM, 2020-07-09
@RAINGM

@commands.command(name = 'on_member_update')
async def hueta123(before, after):
    if before.roles != after.roles:
        channel = client.get_channel(729733881129074768)
        emb = discord.Embed(title = '', description = f'**Обновление ролей пользователя -  {before.mention}**', colour = discord.Color.red())
        emb.add_field(name = '**Роли до**', value = [r.mention for r in before.roles] ) 
        emb.add_field(name = '**Роли после**', value = [r.mention for r in  after.roles] ) 

        await channel.send(embed = emb)

Так можно с любой функцией в discord.py

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question