【问题标题】:Assigning roles via mention the user in Discord.py通过在 Discord.py 中提及用户来分配角色
【发布时间】:2021-04-17 15:36:39
【问题描述】:

我正在为我的不和谐服务器创建一个机器人,并且我正在尝试通过使用命令“.join”创建一个向某人添加角色的命令,然后提及您尝试赋予该角色的用户.下面是我用于该命令的代码。它不起作用

@client.command(help='Lets you join the specified faction. the user variable is who you are adding to the faction (type it as a mention. eg. @GlitchedData64)')
async def join(ctx, user, rolerequest):                                                         
    author = ctx.message.author
    rolelist = [r.mention for r in author.roles if r != ctx.guild.default_role]
    roles = ", ".join(rolelist)

    if admintoken in roles or ownertoken in roles:
        if 'enders' in rolerequest or 'Enders' in rolerequest:
            if enderstoken in roles:
                await ctx.send(f'{user}, is already in that faction')
                
            elif krypticstoken in roles:
                await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                
            elif dotstoken in roles:
                await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')

            else: 
                role = discord.utils.get(user.guild.roles, name="????Enders????")
                await discord.Member.add_roles(user, role)
                await ctx.send(f'{user.mention} has joined the Enders Faction')
                print(f'{user} has joined the Enders Faction')
                
        elif 'kryptics' in rolerequest or 'Kryptics' in rolerequest:
            if krypticstoken in roles:
                await ctx.send(f'{user}, is already in that faction')                           
            
            elif enderstoken in roles:
                await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                
            elif dotstoken in roles:
                await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                
            else:
                role = discord.utils.get(user.guild.roles, name="????????kryptics????????")
                await discord.Member.add_roles(user, role)
                await ctx.send(f'{user.mention} has joined the Kryptics Faction')
                print(f'{user} has joined the Kryptics Faction')
                
        elif 'dots' in rolerequest or 'Dots' in rolerequest:
            if dotstoken in roles:
                await ctx.send(f'{user}, is already in that faction')
            
            elif krypticstoken in roles:
                await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                
            elif enderstoken in roles:
                await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
            
            else:
                role = discord.utils.get(user.guild.roles, name="????Dots????")
                await discord.Member.add_roles(user, role)
                await ctx.send(f'{user.mention} has joined the Dots Faction')
                print(f'{user} has joined the Dots Faction')
                
        else:
            await ctx.send(f"I'm affraid that faction doesn't exist. you can create it with '.createfaction' followed by the name")

    else:
        await ctx.send(f'This command is not available to Non-administrator users')

我对制作机器人相当陌生,所以如果这是一个非常明显的答案,请告诉(我也在使用 discord.py 的 rewrite 分支)

【问题讨论】:

    标签: python discord discord.py discord.py-rewrite


    【解决方案1】:

    我看到的第一个错误是函数调用,你传递了错误的参数。

    在 discord.py 上,您的命令应该指向特定的 Member 和 Role,这意味着它应该看起来像这样:

    async def join(self, ctx, user: discord.Member, role: discord.Role):
    

    不过,如果您能告诉我们您的输出日志或您的代码为什么不工作,而不是仅仅复制粘贴您的代码并要求我们修复它,那将会很有帮助。

    编辑:执行机器人功能将 X 角色添加到 Y 成员就像:

    @bot.command(pass_context=True)
    async def giverole(ctx, user: discord.Member, role: discord.Role):
        await user.add_roles(role)
        await ctx.send(f"hey {ctx.author.name}, {user.name} has been giving a role called: {role.name}")
    

    【讨论】:

    • 如果我解释得不够好,我深表歉意。输入命令时没有显示错误。它什么也不做
    • 非常感谢。正如我在我的问题中所说,我对制作不和谐机器人和 python 很陌生,我很抱歉我缺乏知识。我已经使用了你的代码并且它有效。谢谢
    • 老实说,我不太了解您在代码中所做(或比较)的大部分内容。查看我的上一版,了解如何轻松完成您想做的事情。
    【解决方案2】:
    @client.command(help='Lets you join the specified faction. the user variable is who you are adding to the faction (type it as a mention. eg. @GlitchedData64)')
    async def join(ctx, user: discord.Member, rolerequest):                                                         
        author = ctx.message.author
        rolelist = [r.mention for r in author.roles if r != ctx.guild.default_role]
        roles = ", ".join(rolelist)
    
        if admintoken in roles or ownertoken in roles:
            if 'enders' in rolerequest or 'Enders' in rolerequest:
                if enderstoken in roles:
                    await ctx.send(f'{user}, is already in that faction')
                    
                elif krypticstoken in roles:
                    await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                    
                elif dotstoken in roles:
                    await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
    
                else: 
                    role = discord.utils.get(user.guild.roles, name="?Enders?")
                    await user.add_roles(role)
                    await ctx.send(f'{user.mention} has joined the Enders Faction')
                    print(f'{user} has joined the Enders Faction')
                    
            elif 'kryptics' in rolerequest or 'Kryptics' in rolerequest:
                if krypticstoken in roles:
                    await ctx.send(f'{user}, is already in that faction')                           
                
                elif enderstoken in roles:
                    await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                    
                elif dotstoken in roles:
                    await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                    
                else:
                    role = discord.utils.get(user.guild.roles, name="??kryptics??")
                    await user.add_roles(role)
                    await ctx.send(f'{user.mention} has joined the Kryptics Faction')
                    print(f'{user} has joined the Kryptics Faction')
                    
            elif 'dots' in rolerequest or 'Dots' in rolerequest:
                if dotstoken in roles:
                    await ctx.send(f'{user}, is already in that faction')
                
                elif krypticstoken in roles:
                    await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                    
                elif enderstoken in roles:
                    await ctx.send(f'{user}, is already in a faction.\nTo join another faction they must leave the one they are currently in')
                
                else:
                    role = discord.utils.get(user.guild.roles, name="?Dots?")
                    await user.add_roles(role)
                    await ctx.send(f'{user.mention} has joined the Dots Faction')
                    print(f'{user} has joined the Dots Faction')
                    
            else:
                await ctx.send(f"I'm affraid that faction doesn't exist. you can create it with '.createfaction' followed by the name")
    
        else:
            await ctx.send(f'This command is not available to Non-administrator users')
    

    你不应该这样做discord.Member.add_roles(user, role)。

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 2021-03-17
      • 2022-01-19
      • 2021-04-08
      • 2021-06-05
      • 1970-01-01
      • 2017-10-13
      相关资源
      最近更新 更多