A person
A person

Reputation: 43

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

I'm trying to make a command with my bot that has an 'assignall' command, which will give every member in the server a role specified. This is what I have currently:

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

Here's the exception:

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'

Upvotes: 0

Views: 1700

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

Cause of the asterisk before the role argument discord.py is processing it as a tuple instead of a discord.Role object. If you put a comma between the asterisk and the argument it'll work as intended.

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

Upvotes: 1

Related Questions