如何解封用户?
How to unban a user?
我知道如何禁止会员,我知道如何踢他们,但我不知道如何解禁他们。我有以下输出错误的代码:
discord.ext.commands.errors.CommandInvokeError: Command raised an
exception: AttributeError: 'generator' object has no attribute 'id'
代码:
@bot.command(pass_context=True)
@commands.has_role("Moderator")
async def unban2(ctx):
mesg = ctx.message.content[8:]
banned = client.get_user_info(mesg)
await bot.unban(ctx.message.server, banned)
await bot.say("Unbanned!")
下面要unban a user, you need their user object. The way you seem to be doing it is by passing a user_id in your command and then creating the user object based on that. You could also do it using get_bans()解释,不过我会先回答你的问题。
在命令中传递 user_id
在您的代码中,mseg
是 user_id 而 banned
是 用户对象.
mesg = ctx.message.content[8:]
banned = await client.get_user_info(mesg)
编辑:正如 squaswin 所指出的,您需要等待 get_user_info()
您将 user_id 定义为 ctx.message.content[8:]
,在本例中是您的消息中来自 8th 字符向前 第一个字符为 0。
根据您的代码,以下应该有效:
(下面的数字只是表示字符位置)
!unban2 148978391295820384
012345678...
这个问题是如果你的命令名称或前缀改变了长度,那么你将不得不改变 ctx.message.content[8:]
中的索引以与 user_id 在您的消息中。
更好的方法是将 user_id 作为参数传递给您的命令:
async def unban(ctx, user_id):
banned = await client.get_user_info(user_id)
现在您可以直接使用它 client.get_user_info()
。
使用get_bans()
您可以改为使用 get_bans() 获取禁止用户列表,然后使用该列表获取有效的 用户对象 。例如:
async def unban(ctx):
ban_list = await self.bot.get_bans(ctx.message.server)
# Show banned users
await bot.say("Ban list:\n{}".format("\n".join([user.name for user in ban_list])))
# Unban last banned user
if not ban_list:
await bot.say("Ban list is empty.")
return
try:
await bot.unban(ctx.message.server, ban_list[-1])
await bot.say("Unbanned user: `{}`".format(ban_list[-1].name))
except discord.Forbidden:
await bot.say("I do not have permission to unban.")
return
except discord.HTTPException:
await bot.say("Unban failed.")
return
要将其变成一组有效的命令,您可以创建一个命令来显示被禁用户的索引列表,另一个命令根据列表索引取消对用户的禁止。
get_user_info
是协程。这意味着它必须以与 unban
和 say
.
相同的方式进行 await
ed
根据经验,除非您实际使用生成器,否则您遇到的任何生成器错误都可能是由于没有等待协程造成的。
banned = await bot.get_user_info(mesg)
哦,文档中还写到此函数可能会抛出错误,因此也值得确保不会出错。
try:
banned = await bot.get_user_info(mesg)
except discord.errors.NotFound:
await bot.say("User not found")
我知道如何禁止会员,我知道如何踢他们,但我不知道如何解禁他们。我有以下输出错误的代码:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'generator' object has no attribute 'id'
代码:
@bot.command(pass_context=True)
@commands.has_role("Moderator")
async def unban2(ctx):
mesg = ctx.message.content[8:]
banned = client.get_user_info(mesg)
await bot.unban(ctx.message.server, banned)
await bot.say("Unbanned!")
下面要unban a user, you need their user object. The way you seem to be doing it is by passing a user_id in your command and then creating the user object based on that. You could also do it using get_bans()解释,不过我会先回答你的问题。
在命令中传递 user_id
在您的代码中,mseg
是 user_id 而 banned
是 用户对象.
mesg = ctx.message.content[8:]
banned = await client.get_user_info(mesg)
编辑:正如 squaswin 所指出的,您需要等待 get_user_info()
您将 user_id 定义为 ctx.message.content[8:]
,在本例中是您的消息中来自 8th 字符向前 第一个字符为 0。
根据您的代码,以下应该有效:
(下面的数字只是表示字符位置)
!unban2 148978391295820384
012345678...
这个问题是如果你的命令名称或前缀改变了长度,那么你将不得不改变 ctx.message.content[8:]
中的索引以与 user_id 在您的消息中。
更好的方法是将 user_id 作为参数传递给您的命令:
async def unban(ctx, user_id):
banned = await client.get_user_info(user_id)
现在您可以直接使用它 client.get_user_info()
。
使用get_bans()
您可以改为使用 get_bans() 获取禁止用户列表,然后使用该列表获取有效的 用户对象 。例如:
async def unban(ctx):
ban_list = await self.bot.get_bans(ctx.message.server)
# Show banned users
await bot.say("Ban list:\n{}".format("\n".join([user.name for user in ban_list])))
# Unban last banned user
if not ban_list:
await bot.say("Ban list is empty.")
return
try:
await bot.unban(ctx.message.server, ban_list[-1])
await bot.say("Unbanned user: `{}`".format(ban_list[-1].name))
except discord.Forbidden:
await bot.say("I do not have permission to unban.")
return
except discord.HTTPException:
await bot.say("Unban failed.")
return
要将其变成一组有效的命令,您可以创建一个命令来显示被禁用户的索引列表,另一个命令根据列表索引取消对用户的禁止。
get_user_info
是协程。这意味着它必须以与 unban
和 say
.
相同的方式进行 await
ed
根据经验,除非您实际使用生成器,否则您遇到的任何生成器错误都可能是由于没有等待协程造成的。
banned = await bot.get_user_info(mesg)
哦,文档中还写到此函数可能会抛出错误,因此也值得确保不会出错。
try:
banned = await bot.get_user_info(mesg)
except discord.errors.NotFound:
await bot.say("User not found")