Reputation: 7
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
Reputation: 6944
I want the bot run a command in a specific channel
"y" role is given to x people
Another criteria for this is that the bot's highest role is above any role it's attempting to assign.
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!")
!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.
commands.check()
TextChannel.id
discord.Role
discord.Member
commands.Greedy
- This will look through each argument, attempting to convert it to a member until it can no more.Member.add_roles()
- Is a coroutine, therefore needs to be awaited.asyncio.sleep()
- Also a coroutine.Context.send()
Upvotes: 1