[英]How do you create a new role in discord.py?
I have the following function in my bot to create x empty new roles (for this example I'll make it all the bot does):我在我的机器人中有以下 function 来创建 x 个空的新角色(对于这个例子,我将使它成为机器人所做的一切):
import discord
from discord.ext import commands
CLIENT = commands.Bot(command_prefix='$')
@CLIENT.event
async def on_ready():
"""triggers when bot is running"""
print("Bot online")
@CLIENT.command(pass_context=True)
async def role(ctx, x=1):
"""creates x roles"""
guild = ctx.guild
for i in range(x):
await guild.create_role(name="new role")
print("created " + str(x) + " roles")
CLIENT.run("token")
The bot doesn't create the new roles or print out the message.机器人不会创建新角色或打印出消息。 (no output to the console at all, its as if the function had just said pass) What did I do wrong? (根本没有 output 到控制台,好像 function 刚刚说通过)我做错了什么?
ctx.guild
is None
because you didn't enable intents.guilds
to fix that you need to enable some basic intents, also you should typehint the x
arg so it's converted to an integer. ctx.guild
是None
因为您没有启用intents.guilds
来解决您需要启用一些基本意图的问题,您还应该输入x
arg 以便将其转换为 integer。
intents = discord.Intents.default()
CLIENT = commands.Bot(..., intents=intents)
@CLIENT.command()
async def role(ctx, x: int=1):
for i in range(x):
await ctx.guild.create_role(f'new role {i}')
print(f"Created {x} roles")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.