問題描述
在我的 discord 機(jī)器人中,我有 2 個命令可以提供和創(chuàng)建角色.它們工作得很好,但如果角色名稱包含空格,我就有問題了.它將第二個單詞計入第二個參數(shù),使命令產(chǎn)生錯誤.
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
- - -')
理想情況下,我應(yīng)該能夠執(zhí)行 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!
推薦答案
你有幾個選擇:
使用角色轉(zhuǎn)換器并要求提及角色:
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
切換兩個參數(shù)的位置,以便轉(zhuǎn)換后的用戶排在第一位,并且可能有間隔的名稱作為僅關(guān)鍵字參數(shù)出現(xiàn).
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.
這篇關(guān)于有沒有辦法在我的論點中包含空格?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!