Reputation: 11
@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
I am trying to make it so it checks if a user has been unbanned before it sends the message that they have been unbanned. But I keep on getting ValueError
Here is the exact error:
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)
Upvotes: 0
Views: 320
Reputation: 1925
I suggest converting the member argument to a Member object like so. You can then use the attributes of a discord.Member
object for discriminator and etc.
async def unban(self, ctx, member:commands.MemberConverter):
You can retrieve the discriminator by doing member.discriminator
and their name by using member.name
. Check out the discord.Member
docs.
I also suggest just checking if the user and member are the same or not instead of checking their discriminator and name.
if user == member:
You should also delete the exception handling if unbanning doesn't work. This is because if it doesn't unban properly, it will automatically raise an error.
Then, in the strings at the end, cast member
to type string so that it is readable.
In total, the implementation would look something like this:
@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
Upvotes: 1