所以我想创建一个将角色分配给用户的命令,但我不希望它成为消息作者。谁能建议我该怎么做?谢谢!
这是message.author版本的代码!
if message.author == client.user:
return
if message.content.upper().startswith('!ROLE ADMIN', userID):
role = get(message.server.roles, id="517064875134943262")
userID = message.author.id
if not "343249790047485952" in [role.id for role in
message.author.roles]:
return
elif "517064875134943262" in [role.id for role in message.author.roles]:
await client.send_message(message.channel, 'You are already an admin
<@%s>' % (userID))
else:
await client.add_roles(message.author, role)
await client.send_message(message.channel, 'Admin given to <@%s>' %
(userID))
答案 0 :(得分:1)
有两种方法:
最简单的方法是将discord.ext.commands
扩展名与converter一起使用。
from discord.ext.commands import Bot
from discord import User, Role
bot = Bot(command_prefix='!')
@bot.command(pass_context=True, name="role")
async def _role(ctx, role: Role, user:User=None):
user = user or ctx.message.author
await client.add_roles(user, role)
await bot.say("Added role {} to user {}".format(role.mention, user.mention))
bot.run("token")
这使您可以通过使用名称/提及来指定要赋予的角色:!role @AdminRole @User
如果您想坚持使用on_message
,还可以访问message.mentions
以获得消息中提到的用户列表。