Discord.py为什么删除角色不起作用?

时间:2020-10-25 21:50:43

标签: python-3.x discord.py

赋予角色正常工作。但是绝对复制的“ on_reaction_remove”不会给出任何结果,即使是例外。我尝试在第二个帐户中选择反应,是的,bot发挥了作用,但是当我删除反应程序时,bot没有做任何事情:/

import discord
from discord import utils
from discord.ext import commands
import config

client = commands.Bot(command_prefix = '#')

@client.event
async def on_ready():
    print('Logged on as {0}!'.format(client.user.name))

@client.event
async def on_reaction_add(reaction, user):
    if str(reaction.message.channel) == 'giving-roles':
        try:
            channel = reaction.message.channel
            role = utils.get(user.guild.roles, id = config.ROLES[str(reaction)])

            await user.add_roles(role)
            print(f'[SUCCESS] Role [{config.ROLES[str(reaction)]} for [{user}] was added ')

        except Exception as e:
            print(repr(e))

@client.event
async def on_reaction_remove(reaction, user):
    if str(reaction.message.channel) == 'giving-roles':
        try:
            channel = reaction.message.channel
            role = utils.get(user.guild.roles, id = config.ROLES[str(reaction)])

            await user.remove_roles(role)
            print(f'[SUCCESS] Role [{config.ROLES[str(reaction)]} for [{user}] was removed ')

        except Exception as e:
            print(repr(e))

# RUN
client.run(config.TOKEN)

1 个答案:

答案 0 :(得分:0)

我尝试过一段时间,发现使用on_raw_reaction_add(payload)on_raw_reaction_remove(payload)效果更好。

@client.event
async def on_raw_reaction_add(payload):
    message_id = payload.message_id

#Soft Ocean   
    if message_id == 677963887122841625: # This can be changed but i was using an individual message
        guild_id = payload.guild_id 
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds) # This gets the guild

        if payload.emoji.name == 'tick': #This is the name of the emoji that is used
            role = discord.utils.get(guild.roles, name='Soft Ocean') # Enter the role name here

        if role is not None: # If role exists
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members) #Gets the member
            if member is not None: # Checks if member is real
                await member.add_roles(role) # Gives the role
            else:
                print("Member not found")
        else:
            print("Role not found")

@client.event
async def on_raw_reaction_remove(payload):
    message_id = payload.message_id

#Soft Ocean   
    if message_id == 677963887122841625:
        guild_id = payload.guild_id
        guild = discord.utils.find(lambda g : g.id == guild_id, client.guilds)

        if payload.emoji.name == 'tick':
            role = discord.utils.get(guild.roles, name='Soft Ocean')

        if role is not None:
            member = discord.utils.find(lambda m : m.id == payload.user_id, guild.members)
            if member is not None:
                await member.remove_roles(role)
            else:
                print("Member not found")
        else:
            print("Role not found")