【问题标题】:How do you create a new role in discord.py?如何在 discord.py 中创建新角色?
【发布时间】:2020-12-11 17:55:22
【问题描述】:

我的机器人中有以下函数来创建 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")

机器人不会创建新角色或打印出消息。 (根本没有输出到控制台,就好像函数刚刚说通过)我做错了什么?

【问题讨论】:

  • 这可能与this question重复
  • 这能回答你的问题吗? How to add and create roles in discord.py?
  • 您的代码应该可以工作,除非您一遍又一遍地尝试扮演相同的角色。尝试使规则独一无二,可能使用日期和时间以及用户作者。

标签: python bots discord.py discord.py-rewrite


【解决方案1】:

ctx.guildNone,因为您没有启用 intents.guilds 来解决您需要启用一些基本意图的问题,您还应该输入 x 参数,以便将其转换为整数。

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

【讨论】:

    猜你喜欢
    • 2018-06-21
    • 2021-05-05
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2020-09-13
    • 2021-05-15
    • 2021-08-05
    • 1970-01-01
    相关资源
    最近更新 更多