Python ValueError应为2,但得到了1个discord.py
我试图让它检查,如果用户已经取消,然后再发送消息,他们已经取消退火。但我一直在犯错误 下面是确切的错误:Python ValueError应为2,但得到了1个discord.py,python,discord,discord.py,bots,Python,Discord,Discord.py,Bots,我试图让它检查,如果用户已经取消,然后再发送消息,他们已经取消退火。但我一直在犯错误 下面是确切的错误: @commands.command() @has_permissions(ban_members=True) async def unban(self, ctx, *, member): embed = discord.Embed(title="Unban", description=f"Unbanned - {memb
@commands.command()
@has_permissions(ban_members=True)
async def unban(self, ctx, *, member):
embed = discord.Embed(title="Unban", description=f"Unbanned - {member}", colour=discord.Colour.purple())
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
if (member_name, member_discriminator) in banned_users:
await ctx.send(f'Error unbanning {member.mention}.')
return
else:
pass
await ctx.send(embed=embed)
print(f'{current_time} {member} has been unbanned by @{ctx.message.author}. Server Name: {ctx.guild.name}, Server ID: {ctx.guild.id}\n')
with open('textfiles/text-logs/unban-log.txt', 'a') as unbanlog:
unbanlog.writelines(f'{current_time} {member} has been unbanned by @{ctx.message.author}. Server Name: {ctx.guild.name}, Server ID: {ctx.guild.id}\n')
return
我建议将member参数转换为member对象,如下所示。然后,您可以将
discord.Member
对象的属性用于鉴别器等
async def unban(self、ctx、member:commands.MemberConverter):
您可以通过执行member.discriminator
检索鉴别器,并使用member.name
检索其名称。看看这本书
我还建议只检查用户和成员是否相同,而不是检查他们的鉴别器和名称
如果用户==成员:
如果取消扫描不起作用,还应该删除异常处理。这是因为如果它没有正确地取消ban,它将自动引发一个错误
然后,在末尾的字符串中,将member
转换为键入字符串,使其可读
总体而言,实施情况如下所示:
Ignoring exception in on_command_error
Traceback (most recent call last):
File "C:\Users\ajkit\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "c:\Users\ajkit\Desktop\Programming\Python\discord.py\Galactia\cogs\ModeratorTools.py", line 105, in unban
member_name, member_discriminator = member.split('#')
ValueError: not enough values to unpack (expected 2, got 1)
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\ajkit\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "c:\Users\ajkit\Desktop\Programming\Python\discord.py\Galactia\cogs\ErrorHandling.py", line 21, in on_command_error
raise Error
File "C:\Users\ajkit\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\ajkit\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\ajkit\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ValueError: not enough values to unpack (expected 2, got 1)
这意味着
成员
参数中没有一个“#”
,那么我会改变什么呢?当我完成所有这些时,它会完全改变breaks@Axkkzy嗯,奇怪,让我试着把整个实现。谢谢!我不知道转换器是如何工作的,所以我还有很长的路要走。谢谢你帮我
@commands.command()
@has_permissions(ban_members=True)
async def unban(self, ctx, member:commands.MemberConverter):
embed = discord.Embed(title="Unban", description=f"Unbanned - {member}", colour=discord.Colour.purple())
banned_users = await ctx.guild.bans()
for ban_entry in banned_users:
user = ban_entry.user
if user == member:
await ctx.guild.unban(user)
await ctx.send(embed=embed)
print(f'{current_time} {str(member)} has been unbanned by @{ctx.message.author}. Server Name: {ctx.guild.name}, Server ID: {ctx.guild.id}\n')
with open('textfiles/text-logs/unban-log.txt', 'a') as unbanlog:
unbanlog.writelines(f'{current_time} {str(member)} has been unbanned by @{ctx.message.author}. Server Name: {ctx.guild.name}, Server ID: {ctx.guild.id}\n')
return