【问题标题】:Assigning/deleting discord roles分配/删除不和谐角色
【发布时间】:2021-10-19 06:52:24
【问题描述】:

我的编码很糟糕,所以我需要一些帮助。

我希望机器人在我命令它这样做时分配/删除用户的角色。有人可以帮我写一个简单的代码让我这样做吗?

我需要的是:命令:/role {user} {role},为提到的用户分配提到的角色。

其他方式相同:命令:/delrole {user} {role},从提及的用户中删除提及的角色。 (假设该人已经拥有该角色)

例如/role member: test role: admin(分配 Test Admin 角色)

例如/delrole member: test role: admin(从 Test 中删除 Admin 角色)


希望你能帮忙!

谢谢,佩皮恩·科伦布兰德

【问题讨论】:

标签: discord discord.js discord.py bots


【解决方案1】:

你可以这样做:

import discord
from discord.ext import commands

class ConfigureRoles(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command(name="role")
    @commands.bot_has_permissions(manage_roles=True)
    @commands.has_permissions(manage_roles=True, manage_guild=True)
    async def role_creation(self, ctx, users:commands.Greedy[discord.Member], *, role:commands.Greedy[discord.Role]):
        if not len(users): #Check if author sent users
            await ctx.send("One or more required arguments are missing.")
        else:
            for user in users: #You can mention multiple users
                if not role in user.roles: #Check if user doesn't have specified role already
                    if (ctx.guild.me.top_role.position > user.top_role.position #Check to make sure you have perms
                            and not user.guild_permissions.administrator):
                        await user.add_roles(role) #Adds role

    @commands.command(name="delrole")
    @commands.bot_has_permissions(manage_roles=True)
    @commands.has_permissions(manage_roles=True, manage_guild=True)
    async def role_deletion(self, ctx, users: commands.Greedy[discord.Member], *, role: commands.Greedy[discord.Role]):
        if not len(users):  # Check if author sent users
            await ctx.send("One or more required arguments are missing.")
        else:
            for user in users:  # You can mention multiple users
                if role in user.roles:  # Check if user has specified role
                    if (ctx.guild.me.top_role.position > user.top_role.position  # Check to make sure you have perms
                            and not user.guild_permissions.administrator):
                        await user.remove_roles(role)  # Removes role
         

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

【讨论】:

    【解决方案2】:

    只需使用它来分配角色:

    await user.add_roles(role)
    

    并使用它来删除角色:

    await user.remove_roles(role)
    

    user: discord.Member定义user

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 2021-01-05
      • 2021-01-06
      • 2022-10-24
      • 2020-10-16
      • 1970-01-01
      • 2018-11-25
      • 1970-01-01
      • 2021-09-20
      相关资源
      最近更新 更多