有没有办法选择除message.author.id?

2024-03-28 14:28:02 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是你的名字消息作者代码的版本!你知道吗

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

Tags: inclientsendid消息messageforreturn
1条回答
网友
1楼 · 发布于 2024-03-28 14:28:02

有两种方法:

最简单的方法是使用带有converterdiscord.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,还可以访问^{}以获取消息中提到的用户列表。你知道吗

相关问题 更多 >