在我的 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!
你有几个选择:
使用角色转换器并要求提及角色:
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.
这篇关于有没有办法在我的论点中包含空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!