簡體   English   中英

(Discord.py) 我為用戶提供角色的命令不起作用

[英](Discord.py) My command that gives roles to a user is not working

我正在嘗試對其進行編碼,以便有人可以使用 .role <@member> 命令為另一個人提供角色,因此它看起來像這樣:

.role @Daily Cringe

理想情況下,機器人會給 Daily 一個“畏縮”的角色。 但是,它不起作用。

這是代碼:

@client.command()
async def role(ctx, member: discord.Member = ctx.author, role: discord.Role):
   await ctx.member.add_roles(role)
   await ctx.send(f"The role ''**{role}**'' was just given to you")

我嘗試將member的默認值設置為ctx.author但隨后出現問題:

async def role(ctx, member: discord.Member = ctx.author, role: discord.Role):
                   ^
SyntaxError: non-default argument follows default argument

我試圖改變它,使role變量的默認值等於無,但這似乎也不起作用。

有任何想法嗎?

我使用 Visual Studio Code、Python 3.8.6 64 位和 MacOS Catalina 10.15.7

你的代碼有幾個錯誤。 起初,你不能做member: discord.Member = ctx.author因為ctx還沒有定義。 您必須將成員的默認值設置為None並檢查該成員是否為None

那么你不能用ctx.member.add_roles(role)添加角色。 你必須做member.add_roles(role)

@client.command()
async def role(ctx, member: discord.Member = None, role: discord.Role):
    if not member:
        member = ctx.author
    await member.add_roles(role)
    await ctx.send(f"The role ''**{role}**'' was just given to you")

暫無
暫無

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

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