@bot.command()
async def unban(ctx, *, member):
banned_users = await ctx.guild.bans()
try:
member_name, member_discriminator = member.split("#")
except:
await ctx.send(":x: unbanned: User#0001")
return
for ban_entry in banned_users:
user = ban_entry.user
if (user.name, user.discriminator) == (member_name, member_discriminator):
await ctx.guild.unban(user)
else:
await ctx.send(":hmmmm i think this user is unbaned or he isn't banned")
return
我询问了聊天gpt,但他无法提供帮助
这看起来像是来自一个非常糟糕的教程的代码。不要使用这个,而是使用这个:
@bot.command()
async def unban(ctx, user_id: int): # explanation below
user = discord.Object(id=user_id)
try:
await ctx.guild.unban(user)
except discord.NotFound: # this error will be raised if the ban wasn't found
await ctx.send("This user is not banned.")
好的,所以当您在discord.py 命令回调中键入参数时,库会自动尝试将其转换为该数据模型。
user_id
现在是一个需要用户 ID 的 int,这是在 unban 命令中最推荐使用的形式。
您可以像这样调用命令:
<yourprefix>unban 216415112095858688
,其中 user_id 是您要取消禁止的人的 id。