discord.py - 如何添加踢/禁止原因?
Posted
技术标签:
【中文标题】discord.py - 如何添加踢/禁止原因?【英文标题】:discord.py - How do I add a kick/ban reason? 【发布时间】:2019-02-15 23:36:12 【问题描述】:当禁止 Discord 房间的成员时,我想在 +kick
命令中添加可选的 reason
消息。我尝试的代码如下。
命令和示例用法:
+kick <username> <reason>
+kick @Pine#1337 Spamming Messages in Wrong Channel.
代码:
@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member, *, arg, reason):
author = ctx.message.author
data = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
embed = discord.Embed(name="MEMBER_KICKED", description="------------------------------------------------------", color=0x00ff00)
embed.set_author(name="MEMBER_KICKED:\nMember Kicked Successfully")
embed.add_field(name="Kicked by: ", value="".format(author.mention), inline=False)
embed.add_field(name="Kicked: ", value="<@>".format(user.id), inline=False)
embed.add_field(name="Reason: ", value="\n------------------------------------------------------".format(arg), inline=False)
embed.set_footer(text="Requested by \a ".format(author, data), icon_url=author.avatar_url)
await bot.say(embed=embed)
channel = discord.utils.get(user.server.channels, name="logs")
embed = discord.Embed(name="MEMBER_KICKED", description="------------------------------------------------------", color=0xff0000)
embed.set_author(name="MEMBER_KICKED:\nMember Kicked")
embed.add_field(name="Kicked by: ", value="".format(author.mention), inline=False)
embed.add_field(name="Kicked: ", value="<@>".format(user.id), inline=False)
embed.add_field(name="Reason: ", value="\n------------------------------------------------------".format(arg), inline=False)
embed.set_footer(text="Kicked at ".format(data))
await bot.send_message(channel, embed=embed)
if user.bot == False:
embed = discord.Embed(name="KICKED", description="------------------------------------------------------", color=0xff0000)
embed.set_author(name="KICKED:\nYou've been Kicked")
embed.add_field(name="Kicked by: ", value="".format(author.mention), inline=False)
embed.add_field(name="Kicked in: ", value="".format(user.server), inline=False)
embed.add_field(name="Reason: ", value="\n------------------------------------------------------".format(arg), inline=False)
embed.set_footer(text="Kicked at ".format(data))
await bot.send_message(user, embed=embed)
if user.bot == True:
pass
reason = arg
await bot.kick(user, reason=reason)
错误:
TypeError: kick() missing 1 required 1 keyword-only argument 'reason'
【问题讨论】:
你想对 kick 消息进行什么处理?这是重写分支支持的东西,但异步分支不支持。见Guild.kick
当有人键入命令时,例如+ban <user> <reason>
,它禁止一个成员及其arg的原因
已编辑:修复标题语法,提供背景信息,修复代码块
【参考方案1】:
只需删除reason
参数
你踢的原因在arg
@bot.command(pass_context=True)
async def kick(ctx, user: discord.Member, *, arg):
reason = arg
【讨论】:
TypeError: kick() got an unexpected keyword argument 'reason'
kick
方法中没有 reason
参数。就做await bot.kick(user)
【参考方案2】:
在异步分支的功能冻结后,Discord 引入了禁止/踢出原因。它们仅存在于 discord.py-rewrite 分支上。他们有一个关键字参数:
@bot.command()
async def kick(ctx, user: discord.Member, *, reason):
await user.kick(reason=reason)
【讨论】:
以上是关于discord.py - 如何添加踢/禁止原因?的主要内容,如果未能解决你的问题,请参考以下文章