Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
980 views
in Technique[技术] by (71.8m points)

discord.py - How to make multiple reaction roles

So I'm working on a reaction role cog, and the commands work so far. There's just one problem. When I create two different reaction roles, it only works for the second one. It's because I only have one dictionary and it updates that every time. I think I've seen people use a payload for reaction roles, but I have no idea what that does and if it would fix my problem. Is there a way to use payload to fix my problem? Thanks!! Here's my code:

import discord
from discord.ext import commands
from discord.ext.commands import BucketType, cooldown, CommandOnCooldown
import random
import json


reaction_title = ""
reactions = {}
reaction_message_id = ""

class ReactionRoles(commands.Cog):
    """Reaction roles!!
You need manage roles permissions to use these"""

    def __init__(self, bot):
        self.bot = bot


    # Bot Commands

    @commands.command(aliases=['rcp'])
    async def reaction_create_post(self, ctx):
        """Creates an embed that shows all the reaction roles commands"""

        embed = discord.Embed(title="Create Reaction Post", color=discord.Colour.dark_purple())
        embed.set_author(name="Botpuns")
        embed.add_field(name="Set Title", value=".rst [New Title]", inline=False)
        embed.add_field(name="Add Role", value=".rar [@Role] [EMOJI]", inline=False)
        embed.add_field(name="Remove Role", value=".rrr [@Role]", inline=False)
        embed.add_field(name="Reaction Send Post", value=".rsp", inline=False)

        await ctx.send(embed=embed)
        await ctx.message.delete()


    @commands.command(aliases=['rst'])
    async def reaction_set_title(self, ctx, *, new_title):

        global reaction_title
        reaction_title = new_title

        await ctx.send(f"The title for the message is now `{new_title}`")
        await ctx.message.delete()


    @commands.command(aliases=['rar'])
    async def reaction_add_role(self, ctx, role: discord.Role, reaction):

        global reactions

        reactions[role.name] = reaction
        await ctx.send(f"Role `{role.name}` has been added with the emoji {reaction}")
        await ctx.message.delete()

        print(reactions)


    @commands.command(aliases=['rrr'])
    async def reaction_remove_role(self, ctx, role: discord.Role):

        if role.name in reactions:
            del reactions[role.name]
            await ctx.send(f"Role `{role.name}` has been deleted")
            await ctx.message.delete()

        else:
            await ctx.send("That role wasn't even added smh")

        print(reactions)


    @commands.command(aliases=['rsp'])
    async def reaction_send_post(self, ctx):

        description = "React to add roles
"

        for role in reactions:
            description += f"`{role}` - {reactions[role]}
"

        embed = discord.Embed(title=reaction_title, description=description, color=discord.Colour.purple())
        embed.set_author(name="Botpuns")

        message = await ctx.send(embed=embed)

        global reaction_message_id

        reaction_message_id = str(message.id)

        for role in reactions:
            await message.add_reaction(reactions[role])

        await ctx.message.delete()


    @commands.Cog.listener()
    async def on_reaction_add(self, reaction, user):

        if not user.bot:

            message = reaction.message

            if str(message.id) == reaction_message_id:

                # Add roles to user
                role_to_give = ""

                for role in reactions:

                    if reactions[role] == reaction.emoji:
                        role_to_give = role

                role_for_reaction = discord.utils.get(user.guild.roles, name=role_to_give)
                await user.add_roles(role_for_reaction)

    @commands.Cog.listener()
    async def on_reaction_remove(self, reaction, user):

        if not user.bot:

            message = reaction.message

            if str(message.id) == reaction_message_id:

                # Add roles to user
                role_to_remove = ""

                for role in reactions:

                    if reactions[role] == reaction.emoji:
                        role_to_remove = role

                role_for_reaction = discord.utils.get(user.guild.roles, name=role_to_remove)
                await user.remove_roles(role_for_reaction)


def setup(bot):
    bot.add_cog(ReactionRoles(bot))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...