PSYCHO OP
PSYCHO OP

Reputation: 7

Assign a role to multiple users with a command - discord.py

I am making a discord bot, and I want the bot run a command in a specific channel. For example, y role is given to a number of people. How can I go about doing this?

Upvotes: 1

Views: 2005

Answers (1)

Diggy.
Diggy.

Reputation: 6944

Conditions:

I want the bot run a command in a specific channel

  • You'll need a condition checking whether the command is in the correct channel.

"y" role is given to x people

  • You'll need to iterate through a list of members.

Another criteria for this is that the bot's highest role is above any role it's attempting to assign.


Code:

So with all this being said, let's write up a command to do it, assuming this isn't going to be in its separate cog:

import asyncio

from discord.ext import commands


def is_correct_channel(ctx):
    return ctx.channel.id == 112233445566778899


@commands.check(is_correct_channel)
@bot.command()
async def giverole(ctx, role: discord.Role, members: commands.Greedy[discord.Member]):
    for m in members:
        await m.add_roles(role)
        await asyncio.sleep(1)  # You don't want to get ratelimited!
    await ctx.send("Done!")

Syntax:

!giverole rolename @user#1234 998877665544332211 username It will take the role as the first argument (can be name, mention, or ID). The following arguments will be attempted to be converted to members.

Feel free to also add whatever error handling you wish to add. For example, the role may not have been found, or no members were given, etc.


References:

Upvotes: 1

Related Questions