使用命令为多个用户分配角色 - discord.py
Assign a role to multiple users with a command - discord.py
我正在制作一个 discord 机器人,我希望机器人 运行 在特定频道中发出命令。例如,y
角色被赋予了一些人。我该怎么做呢?
条件:
I want the bot run a command in a specific channel
- 您需要一个条件来检查命令是否在正确的通道中。
"y" role is given to x people
- 您需要遍历成员列表。
另一个标准是机器人的最高角色高于它试图分配的任何角色。
代码:
综上所述,让我们编写一个命令来执行此操作,假设这不会在其单独的齿轮中:
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
它将角色作为第一个参数(可以是名称、提及或 ID)。将尝试将以下参数转换为成员。
您也可以随意添加您希望添加的任何错误处理。例如,可能没有找到角色,或者没有提供成员等
参考文献:
commands.check()
TextChannel.id
discord.Role
discord.Member
commands.Greedy
- 这将查看每个参数,尝试将其转换为成员,直到不能再转换为止。
Member.add_roles()
- 是协程,因此需要等待。
asyncio.sleep()
- 也是协程。
Context.send()
- Cog example - 将您的机器人分成单独的模块,既可以组织又可以更好地控制功能。
我正在制作一个 discord 机器人,我希望机器人 运行 在特定频道中发出命令。例如,y
角色被赋予了一些人。我该怎么做呢?
条件:
I want the bot run a command in a specific channel
- 您需要一个条件来检查命令是否在正确的通道中。
"y" role is given to x people
- 您需要遍历成员列表。
另一个标准是机器人的最高角色高于它试图分配的任何角色。
代码:
综上所述,让我们编写一个命令来执行此操作,假设这不会在其单独的齿轮中:
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
它将角色作为第一个参数(可以是名称、提及或 ID)。将尝试将以下参数转换为成员。
您也可以随意添加您希望添加的任何错误处理。例如,可能没有找到角色,或者没有提供成员等
参考文献:
commands.check()
TextChannel.id
discord.Role
discord.Member
commands.Greedy
- 这将查看每个参数,尝试将其转换为成员,直到不能再转换为止。Member.add_roles()
- 是协程,因此需要等待。asyncio.sleep()
- 也是协程。Context.send()
- Cog example - 将您的机器人分成单独的模块,既可以组织又可以更好地控制功能。