Search code examples
pythondiscorddiscord.pybots

writing unban command, discord.py


when i trie to unban user with "&unban donut_exe_", i get "discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'id'" error Code below:

import discord
import time
from discord.ext import commands

client = commands.Bot(command_prefix="&", help_command=None, intents=discord.Intents.all())

@client.event

async def on_ready():
    print("Huligan online")
    
#Приветствие
@client.command( pass_contex = True )

async def greeting(ctx):
    await ctx.send("Пр")
    
#Очистка чата
@client.command( pass_contex = True )
@commands.has_permissions(manage_messages = True)

async def clear(ctx, amount = 100):
    await ctx.channel.purge(limit = amount + 1)
    
#Кик    
@client.command( pass_contex = True )
@commands.has_permissions(kick_members = True)

async def kick(ctx, member: discord.Member, *, reason = None):
    await member.kick(reason = reason)
    await ctx.send(f"Участник {member.mention} был кикнут по причине «{reason}» администратором {ctx.message.author.mention}")
    
#Бан
@client.command( pass_contex = True )
@commands.has_permissions(ban_members = True)

async def ban(ctx, member: discord.Member, *, reason = None):
    await member.ban(reason = reason)
    await ctx.send(f"Участник {member.mention} был забанен по причине «{reason}» администратором {ctx.message.author.mention}")
    
#Разбан
@client.command( pass_contex = True )
@commands.has_permissions(ban_members = True)

async def unban(ctx, id: str):
    user = id
    await ctx.guild.unban(user)
    await ctx.send(f"Участник {member.mention} был разбанен администратором {ctx.message.author.mention}")





    
token = open("token.txt", r)readline()
client.run(token)

I hope you will help me, mb i specified wrong args


Solution

  • You'll need to pass the ID of the User, not their name. You can obtain a users ID by right-clicking their portrait (If developer mode is turned on), and clicking "Copy User ID" at the bottom

    How to enable Developer mode:

    1. Open up the Discord app
    2. Click on the settings cog in the bottom left corner
    3. Go to Appearance -> allll the way at the bottom
    4. Toggle "Developer Mode" on

    Additionally you'll have to instantiate a discord object with the id (as an integer) before passing it to the unban function

    user_id = int(id)
    user = discord.Object(id=user_id)
    await ctx.guild.unban(user)