有没有办法选择message.author.id以外的其他用户



所以我想制作一个命令,将角色分配给用户,但我不希望它成为消息作者。有人能建议我怎么做吗?谢谢

这是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))

有两种方法:

最简单的方法是将discord.ext.commands扩展与转换器一起使用。

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以获取消息中提到的用户列表。

最新更新