【问题标题】:I have problems with command that gets what user typed after command我在获取用户在命令后输入的内容时遇到问题
【发布时间】:2019-07-09 09:58:16
【问题描述】:

我正在为一个沙盒页面制作 Discord Bot。 我已经制作了一个程序,可以从您想要的任何用户那里删除数据。 (它是输入,然后它会废弃数据) 现在有一个新问题。我想做一个命令“rr2.info.UserHere” rr2。是前缀,但我想创建一个命令来获取您在 rr2.info 之后键入的内容。并将其保存到变量中。

我尝试了一些我在网上找到的代码,但它不起作用。之后我找不到其他任何东西了。

@client.event 
async def on_message():
    if message.content.startswith('rr2.info.'):
        #This is the part I need help with! :D 

我需要一种在 rr2.info 之后输入任何内容的方法。该命令完成!

【问题讨论】:

    标签: discord.py discord.py-rewrite


    【解决方案1】:

    on_message() 用作命令不是一个好习惯。

    最好使用command()discord.ext.commands

    显然您正在寻找一种将用户输入存储到变量中的方法,您可以这样做:

    使用命令

    您已将rr2 定义为您的prefix。假设您使用的是 cog 系统:

    @commands.command()
    async def info(self, ctx, *, input: str):
       await ctx.send(input)
       return
    

    await ctx.send(input) 行将向使用该命令的通道发送一条消息,其中包含用户作为input 传递的内容。

    所以我们有:

    >>> rr2 info This is my input.
    

    输出:

    'This is my input.'
    

    使用 on_message 事件

    如果您绝对想使用事件来存储用户输入,您可以使用:

    @client.event 
    async def on_message(message):
        if message.content.startswith('rr2.info.'):
            input = message.content  # this will send the message with the 'rr2.info.' prefix
    
            channel = message.channel
            await channel.send(input)
    

    这将具有与command() 解决方案完全相同的行为。

    【讨论】:

    • 我都试过了,命令一不起作用,但事件一,它发送我所说的所有内容 10 次或其他东西。我如何让它只在 rr2.info 之后发送东西。并且只发送一次?
    • 另外,当我执行命令一时,我收到错误“忽略命令无异常:discord.ext.commands.errors.CommandNotFound:找不到命令“info.KOMKO190””。我输入了“rr2.info.KOMKO190”
    • 对于该命令,如果您不使用 cogs,它将无法工作。您可以改用装饰器@client.command()。对于event,也可以考虑使用process_command 方法来允许您使用其他命令。尝试添加此if 语句以避免机器人无限期重复:if message.author != client.user.id:
    • 我尝试使用 @client.command() 并得到相同的错误。另外,我应该在哪里添加它?在第一个if下?
    • 不幸的是,您的两个代码都是错误的。第一个代码不是OP想要的格式,为什么return?第二个代码更糟糕,bot 只会发送成员发送的完全相同的消息,并且由于没有检查消息是否来自 bot,bot 将在无限循环中回复 iteslf。
    【解决方案2】:
    @client.event
    async def on_message(message):
        if message.content.startswith('rr2.info.'):
            # Split method returns a list of strings after breaking the given string by the specified separator
            # Example input is 'rr2.info.uwu' then after calling input.split('rr2.info.')
            # we will get a list of ['', 'uwu'] , that's why we need to call second index to get the string after
            # 'rr2.info.' with [1]
            suffix = message.content.split('rr2.info.')[1]
            # Don't send it if it's empty, it will be empty if the user for example sent just 'rr2.info.'
            if suffix:
                await message.channel.send(suffix)
        else:
            # Overriding the default provided on_message forbids any extra commands from running.
            # To fix this, we add client.process_commands(message) line at the end of our on_message
            await client.process_commands(message)
    

    我还建议把名字client改成bot更好看。

    如果您想用commands 做同样的事情,您将不得不更改命令ext 函数的源代码,主要是因为space 用作命令/参数之间的分隔符,并且您希望将其传递为一个字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-20
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      • 2021-03-12
      • 2012-04-14
      相关资源
      最近更新 更多