Rea*_*uby 1 python python-3.x discord.py
如何检查discord.py中是否已存在角色
我正在尝试创建角色的命令,但如果该角色已经存在,则代码将不会创建新角色。
@bot.command()
async def modrole(ctx):
guild = ctx.guild
if guild.has_role(name="BotMod"):
await ctx.send("Role already exists")
else:
await guild.create_role(name="BotMod", colour=discord.Colour(0x0062ff))
Run Code Online (Sandbox Code Playgroud)
您可以使用discord.utils.get迭代来ctx.guild.roles查找具有该名称的名称:
from discord.utils import get
@bot.command()
async def modrole(ctx):
if get(ctx.guild.roles, name="BotMod"):
await ctx.send("Role already exists")
else:
await ctx.guild.create_role(name="BotMod", colour=discord.Colour(0x0062ff))
Run Code Online (Sandbox Code Playgroud)