我只是用 python 编写我自己的不和谐机器人,并且我有一个终止命令。它所做的只是,当你输入 *kill
并提及某人 (*kill @goose.mp4
) 时,它会给你一个随机场景,告诉你如何杀死他们,类似于Dank Memer 机器人。我正在尝试获取所提到的用户 ID,这将是该函数的第二个参数。但我被困住了。在通读 API 并多次搜索后,我只知道如何获取作者的 ID 并使用机器人 ping 他们,而不是作者提到的人。
这是我当前正在使用的代码。其中一个变量的值仅用于测试目的。
if message.content.startswith('*kill'):
print("kill command recieved")
kill_mention_killer = message.author.mention
kill_mention_victm = 'some guy'
print(kill_mention_killer)
kill_responses = [kill_mention_killer + ' kills ' + kill_mention_victim]
kill_message = kill_responses[random.randint(-1, len(kill_responses) -1)]
await message.channel.send(kill_message)
最佳答案
您当前执行此命令的方式不允许您获取参数。如果您尝试执行如下命令:*kill @user
,那么您将需要能够获取所提到的用户(这是您的问题)。操作方法如下:
第一步
import discord, random
from discord.ext import commands
这些导入非常重要。他们将被需要。
第二步
client = commands.Bot(command_prefix='*')
这将初始化在整个代码中使用的客户端
。现在进入您实际发出命令的部分。
@client.command()
async def kill(ctx, member: discord.Member): # This command will be named kill and will take two arguments: ctx (which is always needed) and the user that was mentioned
kill_messages = [
f'{ctx.message.author.mention} killed {member.mention} with a baseball bat',
f'{ctx.message.author.mention} killed {member.mention} with a frying pan'
] # This is where you will have your kill messages. Make sure to add the mentioning of the author (ctx.message.author.mention) and the member mentioning (member.mention) to it
await ctx.send(random.choice(kill_messages))
就是这样!这就是制定标准终止命令的方法。只需确保将 kill_messages
数组更改为您想要的任何消息即可。
关于python - Discord.py:有没有办法使用 on_message() 函数获取命令的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65634607/