如何制作多重反应角色

时间:2021-01-10 23:23:56

标签: discord.py discord.py-rewrite

所以我正在研究一个反应角色齿轮,到目前为止这些命令都有效。只有一个问题。当我创建两个不同的反应角色时,它只适用于第二个。这是因为我只有一本字典,而且每次都会更新。我想我已经看到人们将有效载荷用于反应角色,但我不知道它的作用是什么,以及它是否能解决我的问题。有没有办法使用有效载荷来解决我的问题?谢谢!!这是我的代码:

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!!\nYou 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\n"

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

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

0 个答案:

没有答案