Python 有人知道我如何将此命令转换为on message命令吗
这是我的密码:Python 有人知道我如何将此命令转换为on message命令吗,python,discord.py,Python,Discord.py,这是我的密码: @client.command(pass_context=True) async def banall(ctx): await ctx.message.delete() for user in list(ctx.guild.members): try: await ctx.guild.ban(user) await ctx.send(f"{user.n
@client.command(pass_context=True)
async def banall(ctx):
await ctx.message.delete()
for user in list(ctx.guild.members):
try:
await ctx.guild.ban(user)
await ctx.send(f"{user.name} has been banned from {ctx.guild.name}")
except:
await ctx.send(f"{user.name} has FAILED to be banned from {ctx.guild.name}")
我试图使它成为这样,当有人说“banall”时,它将禁止服务器中的所有成员。我发现这个命令可以工作,但例如!banall“我想摆脱”!所以它将是“banall”。有人知道我将如何做这件事吗。谢谢:如果你想剪掉“!“从”!banall“您需要执行以下操作:
string = "!banall"
string2 = string.replace('!', '')
on_message()
。这可以通过删除方法装饰器(@client.command(pass_context=True)
)并将ctx
更改为message来完成,因为on_message()
函数获取message
对象async def banall(消息):
等待消息。删除()
对于列表中的用户(message.guild.members):
尝试:
等待消息.guild.ban(用户)
wait message.channel.send(f“{user.name}已被禁止进入{message.guild.name}”)
除:
wait ctx.send(f“{user.name}未能从{ctx.guild.name}中被禁止”)
on_message()
中运行函数。可以使用if语句检查消息内容是否为“banall”@client.event
异步def on_消息(消息):
如果message.content==“banall”:
等待banall(消息)
discord.py
有一个命令处理程序,当它接收到带有代码中定义的前缀的命令时,该处理程序会运行一个函数(例如此函数)。这就是OP试图在\u message()上的内运行函数的原因
因此,该命令不需要前缀。您可以在此处找到有关discord.py
的命令处理程序的更多信息:因此,OP希望拥有它,以便当有人输入“banall”时,会发生一些事情,而不仅仅是“!巴纳尔“?