簡體   English   中英

Discord Bot:如何通過命令賦予角色?

[英]Discord Bot: How to give roles via command?

所以,這段代碼可能看起來很糟糕,這是因為它實際上很糟糕,但我花了幾個小時尋找類似的代碼,卻找不到一個有效的代碼。 我是這個 discord 機器人開發的初學者,聽說 discord 需要“更新”代碼,我找不到任何可以幫助解決這個問題的東西。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM