编写解除封禁命令,discord.py

0 投票
2 回答
50 浏览
提问于 2025-04-13 15:15

当我尝试用 "&unban donut_exe_" 来解除对用户的禁言时,我收到了一个错误信息:“discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'str' object has no attribute 'id'”。下面是代码:

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)

我希望你能帮我,可能是我指定的参数有问题。

2 个回答

0

你可以测试一下这个

用 userping 来解除封禁:

unban <@{id of the user}>

用 ID 来解除封禁:

unban {id of the user}

这里是代码:

@client.command(pass_context=True)
@commands.has_permissions(ban_members=True)
async def unban(ctx, *, member):
    banned_users = await ctx.guild.bans()

    # Check if the provided argument is a mention (user ID)
    if member.startswith('<@') and member.endswith('>'):
        # Extract the user ID from the mention
        user_id = int(member[2:-1])
        user = discord.Object(id=user_id)
    else:
        # Search for the user by name and discriminator
        member_name, _, member_discriminator = member.partition('#')
        for ban_entry in banned_users:
            user = ban_entry.user
            if (user.name, user.discriminator) == (member_name, member_discriminator):
                break
        else:
            # If user not found, return and notify
            await ctx.send("Участник с таким именем не найден в списке забаненных.")
            return

    await ctx.guild.unban(user)
    await ctx.send(f"Участник {user.mention} был разбанен администратором {ctx.author.mention}")
0

你需要传递用户的ID,而不是他们的名字。你可以通过右键点击他们的头像(前提是开发者模式已经开启),然后在底部选择“复制用户ID”来获取用户的ID。

如何开启开发者模式:

  1. 打开Discord应用
  2. 点击左下角的设置齿轮
  3. 进入外观设置 -> 一直往下滚动
  4. 把“开发者模式”开关打开

另外,在将ID(作为整数)传递给解除禁言的功能之前,你还需要先创建一个discord对象。

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

撰写回答