如何在 discord.py 中一次性为所有成员分配角色?

How do I assign all members a role at once in discord.py?

我正在尝试用我的机器人发出一个命令,该命令有一个 'assignall' 命令,它将为服务器中的每个成员指定一个角色。这是我目前拥有的:

import discord
import discord.utils
from discord.ext import commands

client = commands.Bot(command_prefix = ["nik!", "NIK!", "Nik!", "nIk!", "nIK!", "NIk!", "niK!", "NiK!"], help_command=None, case_insensitive=True)

@client.command(aliases = ['assigna'])
async def assignall(ctx, * role: discord.Role):
  if (not ctx.author.guild_permissions.manage_roles):
    await ctx.reply("Error: User is missing permission `Manage Roles`")
    return
  await ctx.reply("Attempting to assign all members that role...")
  for member in ctx.guild.members:
    try:
      await member.add_roles(role)
    except:
      await ctx.reply("Error: Can not assign role!\n"
      "Potential fix: Try moving my role above the role you want to assign to all members.")
  await ctx.reply("I have successfully assigned all members that role!")

client.run("tokenthaticantshowobviously")

例外情况:

Ignoring exception in command assignall:
Traceback (most recent call last):
  File "main.py", line 91, in assignall
    await member.add_roles(role)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 777, in add_roles
  await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'tuple' object has no attribute 'id'

role 参数前的星号 discord.py 的原因是将其作为元组而不是 discord.Role 对象处理。如果您在星号和参数之间放置一个逗号,它将按预期工作。

async def assignall(ctx, *, role: discord.Role):