Reputation: 113
How do i make an unban command in Discord.py Rewrite i wanted to clear my question because there is no bot.unban command (maybe it is) and the player is not in the list so i can mention the user
Edit:
Traceback (most recent call last):
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\client.py", line 227, in _run_event
await coro(*args, **kwargs)
File "C:\Users\BKhushi\Desktop\gg\Discordgang.py", line 125, in on_command_error
raise error
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 814, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 675, in invoke
await self.prepare(ctx)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 640, in prepare
await self._parse_arguments(ctx)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 561, in _parse_arguments
transformed = await self.transform(ctx, param)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 427, in transform
return await self.do_conversion(ctx, converter, argument, param)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 382, in do_conversion
return await self._actual_conversion(ctx, converter, argument, param)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 328, in _actual_conversion
ret = await instance.convert(ctx, argument)
File "C:\Users\BKhushi\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\converter.py", line 158, in convert
raise BadArgument('User "{}" not found'.format(argument))
discord.ext.commands.errors.BadArgument: User "@<552510634303029298>" not found```
Upvotes: 3
Views: 20347
Reputation: 1
Try using this It worked for me. instead of member:discord.Member do member:discord.User as the user is for a user in discord and member is a member in a discord server
@client.command()
@commands.has_role(1234567890) # Role ID
@commands.cooldown(1,15, commands.BucketType.user)
async def unban(ctx, member:discord.User, *, reason=None):
if reason == None:
reason = f"No Reason Provided"
await ctx.guild.unban(member, reason=reason)
await ctx.send(f"{member.mention} has been **unbanned**", delete_after=15)
embed = discord.Embed(title="Unban Log", description=f"{member.mention} has been **unbanned** by {ctx.author.mention}\n\nReason: `{reason}`\n\nUnbanned from: `{ctx.guild.name}`", color=0x1355ed)
embed.add_field(name="User", value=f"{member}", inline=True)
embed.add_field(name="UserID", value=f"{member.id}", inline=True)
embed.add_field(name="Moderator", value=f"{ctx.author}", inline=True)
embed.set_footer(text=f"Unban log - Banned user: {member.name}")
embed.set_thumbnail(url=member.avatar_url)
embed.timestamp = datetime.datetime.utcnow()
logchannel = client.get_channel(988416086217203732)
await logchannel.send(embed=embed)
await ctx.message.delete()
print(f"Sucsessfully unbanned {member.name}")
Upvotes: 0
Reputation: 31
I would consider the below command. My apologies that it does not send a message after said action, to get more on how to write in Discord.Py Rewrite I recommend you watch here!
async def pardon(ctx, *, member):
banned_users = await ctx.guild.bans()
member_name, member_discriminator = member.split('#')
for ban_entry in banned_users:
user = ban_entry.banned_users
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
Upvotes: 3
Reputation: 61014
If you have a Member
object representing a banned Member
, you can use Member.unban
. Most of the time, however, you'll instead have to get a User
object representing that user and use Guild.unban
:
from discord import User
from discord.ext.commands import Bot, guild_only
bot = Bot("!")
@bot.command(name='unban')
@guild_only() # Might not need ()
async def _unban(ctx, id: int):
user = await bot.fetch_user(id)
await ctx.guild.unban(user)
bot.run("TOKEN")
Upvotes: 5