有没有办法在我的论点中包含空格?

Is there a way to include spaces in my argument?(有没有办法在我的论点中包含空格?)
本文介绍了有没有办法在我的论点中包含空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在我的 discord 机器人中,我有 2 个命令可以提供和创建角色.它们工作得很好,但如果角色名称包含空格,我就有问题了.它将第二个单词计入第二个参数,使命令产生错误.

In my discord bot, I have 2 commands do give and create roles. They work perfectly fine, but if the role name includes a space, I have an issue. It counts the second word toward the second argument, making the command produce an error.

# Giverole
@client.command(name='giverole',
                aliases=['gr'],
                brief='Assgins role to a user',
                pass_ctx=True)
async def giverole(ctx, rname, *, member: discord.Member):
    role = get(member.guild.roles, name=rname)
    await member.add_roles(role)
    await ctx.send(f'Role added to user {member.mention}')
    print('Giverole command executed
- - -')

# Createrole

@client.command(name='createrole',
                brief='Creates a role',
                aliases=['cr','makerole'],
                pass_ctx=True)
async def createrole(ctx, rname: str, clr: discord.Colour):
    if ctx.author.guild_permissions.manage_roles:
        await ctx.guild.create_role(name=rname, colour=clr)
        await ctx.send('Role created with name: ' + rname)
        print('Createrole command executed
-  -  -')
    else:
        await ctx.send('You lack permission.')
    print('Createrole command executed
-  -  -')

理想情况下,我应该能够执行 k!giverole Bot Tester @user 之类的操作,但我得到了无效用户"错误.我有什么方法可以支持角色名称中的空格吗?提前致谢!

Ideally, I should be able to do something like k!giverole Bot Tester @user, but instead I get an "Invalid user" error. Is there any way for me to support spaces in the role name? Thanks in advance!

推荐答案

你有几个选择:

  1. 使用角色转换器并要求提及角色:

  1. Use a role converter and require the role to be mentioned:

async def giverole(ctx, role: discord.Role, member: discord.Member):

  • 要求角色用引号括起来:

  • Require the role to be wrapped in quotes:

    k!giverole "Bot Tester" @user
    

  • 切换两个参数的位置,以便转换后的用户排在第一位,并且可能有间隔的名称作为仅关键字参数出现.

  • Switch the positions of the two arguments so that the converted user comes first, and the potentially spaced name comes as the keyword-only argument.

    async def giverole(ctx, member: discord.Member, *, rname):
    

  • 我会推荐第一个选项,但您也可以尝试其他选项,看看您更喜欢哪个.

    I would recommend the first option, but you may want to try the others as well to see which one you prefer.

    这篇关于有没有办法在我的论点中包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

    相关文档推荐

    How do I make a list of all members in a discord server using discord.py?(如何使用 discord.py 列出不和谐服务器中的所有成员?)
    how to change discord.py bot activity(如何更改 discord.py 机器人活动)
    Issues with getting VoiceChannel.members and Guild.members to return a full list(让 VoiceChannel.members 和 Guild.members 返回完整列表的问题)
    Add button components to a message (discord.py)(将按钮组件添加到消息(discord.py))
    on_message() and @bot.command issue(on_message() 和@bot.command 问题)
    How to edit a message in discord.py(如何在 discord.py 中编辑消息)