【问题标题】:Making a bot that can add multiple roles to a user at once制作一个可以一次为用户添加多个角色的机器人
【发布时间】:2023-02-22 06:08:49
【问题描述】:

基本上我使用的是 nextcord,它是 discord.py 的一个分支。总的来说,我对编程有点陌生,但由于我喜欢或已经有一段时间不和谐,并且想从事需要编程的职业,所以我说做一个像这样的小项目将是一个好的开始。


import asyncio
import nextcord
from nextcord.ext import commands
from nextcord.ext.commands import guild_only

@client.command()
@commands.has_role("Admin")
async def giverole(ctx, member : nextcord.Member, role : nextcord.Role):
    if role in member.roles:
        await member.add_roles(role, atomic = True)
        embed8a = nextcord.Embed(description = f"Removed **{role}** from **{member}**", color = nextcord.Color.orange())
        await ctx.send(embed = embed8a)
    else: 
        await member.add_roles(role, atomic = True)
        embed7a = nextcord.Embed(description = f"Added **{role}** to **{member}**", color = nextcord.Color.orange())
        await ctx.send(embed = embed7a)

Anyways, my issue is that in the code below I am able to add one role to a user at a time, however not multiple. According to the nextcord/discord.py documentation, using the "Atomic = true" attribute should make this possible but I've gotten several errors. I've listed the most current one below.

Traceback (most recent call last): File "C:\Users\De'shon\AppData\Local\Programs\Python\Python311\Lib\site-packages\nextcord\ext\commands\bot.py", line 1381, in invoke await ctx.command.invoke(ctx) File "C:\Users\De'shon\AppData\Local\Programs\Python\Python311\Lib\site-packages\nextcord\ext\commands\core.py", line 948, in invoke await injected(*ctx.args, **ctx.kwargs) File "C:\Users\De'shon\AppData\Local\Programs\Python\Python311\Lib\site-packages\nextcord\ext\commands\core.py", line 174, in wrapped raise CommandInvokeError(exc) from exc nextcord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'tuple' object has no attribute 'id'

【问题讨论】:

    标签: python discord discord.py nextcord


    【解决方案1】:

    您收到的错误是由于您将角色元组传递给 member.add_roles() 而不是单个角色。

    要解决此问题,您可以使用 commands.Greedy 更改角色参数以接受多个角色,这将允许您传入多个角色名称,并以空格分隔。

    @client.command()
    @commands.has_role("Admin")
    async def giverole(ctx, member: nextcord.Member, *roles: commands.Greedy[nextcord.Role]):
        added_roles = []
        removed_roles = []
        for role in roles:
            if role in member.roles:
                await member.remove_roles(role, atomic=True)
                removed_roles.append(role)
            else:
                await member.add_roles(role, atomic=True)
                added_roles.append(role)
        if added_roles:
            embed7a = nextcord.Embed(description=f"Added {', '.join(role.mention for role in added_roles)} to {member.mention}", color=nextcord.Color.orange())
            await ctx.send(embed=embed7a)
        if removed_roles:
            embed8a = nextcord.Embed(description=f"Removed {', '.join(role.mention for role in removed_roles)} from {member.mention}", color=nextcord.Color.orange())
            await ctx.send(embed=embed8a)
    

    【讨论】:

      猜你喜欢
      • 2020-10-04
      • 2021-07-27
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 2021-05-29
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      相关资源
      最近更新 更多