問題描述
如何在 Discord.py 中創建一個機器人,該機器人將分配 role.json
文件中存在的角色,同時使用相同的命令來刪除和添加相同的角色.例如,?role
將添加和刪除角色,具體取決于用戶是否分配了角色.我對如何實現這一點有點困惑.
How do I make a bot in Discord.py that will assign roles present in a role.json
file, while using the same command to both remove and add the same role. For example, ?role <rolename>
will both add and remove a role, depending on if the user has the role assigned. I'm a bit confused on how to achieve this.
我當前的機器人使用 ?roleadd <rolename>
?roleremove <rolename>
.
My current bot uses ?roleadd <rolename>
?roleremove <rolename>
.
推薦答案
我不確定你的 role.json
文件在哪里發揮作用,但我將如何實現這樣的命令
I'm not sure where your role.json
file comes into play, but here's how I would implement such a command
@bot.command(name="role")
async def _role(ctx, role: discord.Role):
if role in ctx.author.roles:
await ctx.author.remove_roles(role)
else:
await ctx.author.add_roles(role)
這使用 Role
轉換器 自動從 role
對象的名稱、id 或提及中解析.
This uses the Role
converter to automatically resolve the role
object from its name, id, or mention.
這篇關于使用機器人 Discord.py 授予和刪除角色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!