当我尝试使用“&unban donut_exe_”取消禁止用户时,我收到“discord.ext.commands.errors.CommandInvokeError:命令引发异常:AttributeError:'str'对象没有属性'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)
我希望你能帮助我,我指定了错误的参数
你可以测试一下
通过用户操作取消禁止:
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}")