简体   繁体   English

事件可以执行命令吗? 如果是这样,我怎样才能让我的人这样做?

[英]Can an event execute a command? If so, how can I make my one do so?

So I am trying to have an event where, once the user types a specific word (not a command, literally a word/string), the event will trigger an existing command.所以我试图有一个事件,一旦用户输入一个特定的词(不是命令,字面意思是一个词/字符串),该事件将触发一个现有的命令。 Yeah you may wonder "Why not have the user type the command itself?"是的,您可能想知道“为什么不让用户自己键入命令?” well, the reason why this isn't the case it's kinda hard to explain.好吧,为什么不是这样的原因有点难以解释。 Check this out:看一下这个:

My event will work only if the person types "nothing" (literally the word nothing).我的事件只有在此人输入“无”(字面意思是“无”)时才会起作用。 Eventually, the person won't expect the bot to actually take this as a command, and so he/she won't type it as command (with the prefix and all that) This is my code:最终,此人不会期望机器人实际上将其作为命令,因此他/她不会将其键入为命令(带有前缀和所有这些)这是我的代码:

@client.command()
async def menu(ctx)
#here, well, goes what I want the command to do but it's not the issue

@client.event
async def on_message(message):
    if message.content.startswith("nothing"):
        #here idk how to execute the command up there. That's my question

I hope I am being clear with my issue.我希望我清楚我的问题。 Don't worry about what the command exectues, or why the message for the event is "nothing".不要担心命令执行什么,或者为什么事件的消息是“无”。 I just really want to know how to make this work.我真的很想知道如何进行这项工作。

Some friends suggested me to invoke the command, but I didn't really know how to do that, and everytime I would try it wouldn't work.一些朋友建议我调用命令,但我真的不知道该怎么做,每次尝试都不起作用。 Others suggested to call the function, but I also tried that and it wouldn't work.其他人建议调用该函数,但我也尝试过,但它不起作用。 I don't know if I typed it correctly or if it simply won't work.我不知道我是否输入正确或者它根本不起作用。 I hope someone helps me out here.我希望有人在这里帮助我。 Thanks in advance.提前致谢。

If your client is a Bot instance you can use Bot.get_context() to create your own context and invoke the command from there:如果您的客户端是Bot实例,您可以使用Bot.get_context()创建您自己的上下文并从那里调用命令:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command()
async def menu(ctx):
    await ctx.send('bar')

@bot.event
async def on_message(message):
    if message.content.startswith('foo'):
        ctx = await bot.get_context(message, cls=commands.Context)
        ctx.command = bot.get_command('menu')
        await bot.invoke(ctx)

    await bot.process_commands(message)

get_context , this takes a message object. get_context ,这需要一个消息对象。 Then invoke .然后invoke . Keep in mind, there are 3 downsides to using this method.请记住,使用此方法有 3 个缺点。

  1. The converters (the type hints) won't be triggered.转换器(类型提示)不会被触发。 You need to pass the correct types into the arguments.您需要将正确的类型传递给参数。
  2. Checks will be bypassed.检查将被绕过。 You could invoke an owner only command with a non-owner and it would still work.您可以使用非所有者调用仅所有者命令,它仍然可以工作。 If you still want to run all the checks, see can_run , which will run all the checks and raise an error if any checks fail.如果您仍想运行所有检查,请参阅can_run ,它将运行所有检查并在任何检查失败时引发错误。
  3. If ctx.invoke was invoked outside of a command (eg eval), the error handler won't fire.如果ctx.invoke在命令之外调用(例如 eval),错误处理程序将不会触发。
@client.command()
async def menu(ctx):
    await ctx.send("Hello")


@client.event
async def on_message(message):
    if message.content.startswith("nothing"):
        ctx = await client.get_context(message)
        await ctx.invoke(menu)
    await client.process_commands(message)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎样才能使它只有具有特定角色的成员才能执行命令? - How can I make it so only members with a specific role can execute a command? 我如何使它可以循环? - How do I make it so it can loop? 我将如何做到这一点,以便个人是唯一可以使用 discord 机器人运行命令的人? - How would I make it so that an individual person is the only one who can run a command with a discord bot? 我怎么做才能让你不能用 echo 命令@everyone - How do I make it so that you can't @everyone with an echo command 我怎么做才能让我的方格可以降落在我的另一个方格上并留在上面 - How do I make it so my square can land on my other square and stay on it 如果用户说“不”,我该如何做到这一点,以便我的代码可以返回一行 - How do I make it so my code can return to a line if the User says No 如何制作程序,以便用户可以指定文件名? - How do I make my program so that the user can specify a file name? 如何让鼠标点击子标签传递到父画布,以便我可以执行我的回调功能? - How to do I get mouse clicks on child labels to pass through to the parent canvas so I can execute my callback functionality? (discord.py)我怎样才能让我的机器人在当前命令完成之前不回复其他命令 - (discord.py) How can I make it so my bot that doesn't reply to other commands until the current command is finished 如何使 python 可以在命令提示符中检查 SystemError - How can I make it so that python can check a SystemError in the command prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM