xXSkillexZ
xXSkillexZ

Reputation: 105

Discord.py | I'm trying to make my bot unban with both Member Name and Member ID

My code(Inside a Cog):

import discord
import datetime
from discord.ext import commands

class unban(commands.Cog):

    def __init__(self, client):
        self.client = client

    @commands.command()
    @commands.has_permissions(ban_members = True)
    async def unban(self, ctx, id: int):
        user = await self.client.fetch_user(id)
        await ctx.guild.unban(user)

        unban= discord.Embed(title=f'A moderation action has been performed!', description='', color=0x90fd05)
        #unban.add_field(name='User Affected:', value=f'`{member.name}`', inline=True)
        #unban.add_field(name='User ID:', value=f'`{member.id}`', inline=True)
        unban.add_field(name='Moderator Name:', value=f'`{ctx.author}`', inline=True)
        unban.add_field(name='Moderator ID:', value=f'`{ctx.author.id}`', inline=True)
        unban.add_field(name='Action Performed:', value='`UnBan`', inline=True)
        unban.set_author(name=f'{ctx.guild}', icon_url=ctx.guild.icon_url)
        #unban.set_thumbnail(url=member.avatar_url)
        unban.timestamp = datetime.datetime.utcnow()

        await ctx.send(embed=unban)
        
        
def setup(client):
    client.add_cog(unban(client))

I've made the command so I can unban people. But I can only unban with their ID. I want it to unban with their Name too. So how could I do that?

I've also tried replacing the:

async def unban(self, ctx, id: int):

with:

async def unban(self, ctx, member : discord.Member):

but nothing worked. and I've tried using both:

async def unban(self, ctx, member : discord.Member, id: int):

but still nothing worked...

Upvotes: 1

Views: 624

Answers (1)

Axisnix
Axisnix

Reputation: 2907

async def unban(self, ctx, member : discord.Member):

It will not work due to a banned member can only be represented by discord.User or by an ID.

async def unban(self, ctx, user_id : int):
    user = await self.client.fetch_user(user_id)
    await ctx.guild.unban(user)

This will only work with ID's so discord can locate the user in their database.

Upvotes: 1

Related Questions