【问题标题】:How to add spaces in async function discord.py如何在异步函数 discord.py 中添加空格
【发布时间】:2021-12-06 15:54:09
【问题描述】:

我正在制作一个不和谐的聊天机器人。我想设置命令“你好吗?”但我的程序只选择 'How' 。我需要一些建议。TIA

import discord
from discord.ext import commands
client = commands.Bot(command_prefix= '=')

@client.event
async def on_ready():
    print('Bot is online')
@client.command()
async def hello(ctx):
    await ctx.send('hello')
@client.command()
async def how_are_you(ctx):
    await ctx.send('I am good')
client.run('token')

【问题讨论】:

    标签: python discord pycharm discord.py bots


    【解决方案1】:

    如果你想使用一个命令,我认为你不能注册一个带有空格的命令,但是有几个解决方法:

    首先您可以使用字符串的第一个单词作为命令名称,然后检查消息的内容:

    @client.command()
    async def how(ctx):
        if ctx.message.content.lower() == "=how are you":
            await ctx.send("I'm fine thanks")
        else:
            pass
    

    消息中需要=前缀,当你想改变你的前缀时替换它。

    或者,第二个选项是设置一个on_message 监听器。

    @client.event
    async def on_message(message):
        if message.content.lower().startswith("=how are you"):
            await message.channel.send("I'm fine thanks.")
        await client.process_commands(message)
    
    

    【讨论】:

      猜你喜欢
      • 2021-03-05
      • 2022-08-21
      • 2021-08-05
      • 2020-10-28
      • 2019-03-02
      • 2021-03-25
      • 1970-01-01
      • 2022-06-21
      • 1970-01-01
      相关资源
      最近更新 更多