【问题标题】:Discord Bot: How to give roles via command?Discord Bot:如何通过命令赋予角色?
【发布时间】:2020-04-07 18:52:48
【问题描述】:

所以,这段代码可能看起来很糟糕,这是因为它实际上很糟糕,但我花了几个小时寻找类似的代码,但找不到一个可以工作的代码。 我是这个不和谐机器人开发的初学者,听说不和谐需要“更新”代码,但我找不到任何可以帮助解决这个问题的东西。

@client.command()
async def role(ctx):
    role = discord.utils.get(member.server.roles, id="id-here")
    await bot.add_roles(member, role)

我也尝试了一个事件:(不工作)

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content == 'give me admin':
        role = get(message.server.roles, id='also id here')
        await client.add_roles(message.author, role)

【问题讨论】:

标签: python discord discord.py


【解决方案1】:

所以 Benjin 在评论部分发送的代码帮助我解决了我的问题。如果您遇到与我相同的问题,那么您应该使用以下代码:

@client.command()
async def giverole(ctx, arg: discord.Member):
    await ctx.send(arg)
    knownrole = discord.utils.get(ctx.guild.roles, name="the name of the role")
    await arg.add_roles(knownrole)

【讨论】:

    猜你喜欢
    • 2021-10-11
    • 2018-07-24
    • 2022-01-09
    • 2021-08-09
    • 2020-02-26
    • 2020-10-07
    • 2020-02-11
    • 1970-01-01
    • 2020-11-16
    相关资源
    最近更新 更多