【问题标题】:I want to make the bot create a category and a text or voice chat in it我想让机器人在其中创建一个类别和一个文本或语音聊天
【发布时间】:2020-06-03 13:07:24
【问题描述】:
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def new_voice(ctx, name):
    await ctx.guild.create_voice_channel(name, *, category)

我还是编程新手,请不要严格判断,请充分解释我的错误是什么,我将不胜感激。

【问题讨论】:

  • 你能包含完整的错误吗?此外,您应该尝试manage_channels=True 而不是管理员,因为管理频道也有权创建新频道

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

以下是您在其中创建新类别和频道的方法:

@client.command() # no need for pass_context, as it's passed automatically in rewrite
@commands.has_permissions(manage_channels=True) # base permission for channel creation
async def newcategory(ctx, *, name):
    category = await ctx.guild.create_category(name)
    await category.create_voice_channel(f"{ctx.author.name}'s VC!")

或者你可以检查某个类别是否已经存在:

@client.command()
@commands.has_permissions(manage_channels=True)
async def newchannel(ctx, *, name):
    category = discord.utils.get(ctx.guild.categories, name="Category name!")
    if category: # checks if category exists (returns None otherwise)
        await category.create_voice_channel(name)
    else:
        category = await ctx.guild.create_category("Category name!")
        await category.create_voice_channel(name)

* 表示“消耗休息”,它允许你有一个带有空格的参数(请记住,这只能是命令的最后一个参数,但你可以在它之前有参数)


参考资料:

【讨论】:

    猜你喜欢
    • 2017-03-05
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2021-05-24
    • 2019-06-03
    • 1970-01-01
    • 2017-10-12
    相关资源
    最近更新 更多