有没有办法在我的论点中加入空格? [英] Is there a way to include spaces in my argument?

查看:81
本文介绍了有没有办法在我的论点中加入空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的不和谐机器人中,我有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\n- - -')

# 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\n-  -  -')
    else:
        await ctx.send('You lack permission.')
    print('Createrole command executed\n-  -  -')

理想情况下,我应该可以执行 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.

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

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆