Python Discord Bot:如何与用户互动?

时间:2020-08-30 08:50:10

标签: python discord

我正在尝试为我的服务器制作Discord机器人,但遇到了一些困难。我查看了其他人的问题,应用了所有类型的更改,但仍然遇到困难。作为参考,我是使用Python的新手,而100%是Discord机器人的初学者。所以,这是我的代码:

import discord
from discord.ext import commands


prefix = ">"
client = commands.Bot(command_prefix=prefix, case_insensitive=True)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>hello'):
        msg = 'Hello, {0.author.mention}!'.format(message)
        await message.channel.send(msg)
        

@client.command(name = "pomodoro")
async def Pomodoro(ctx):
    if ctx.content.startswith('>pomodoro'):
        await ctx.channel.send("Let's grab some tomatoes! For how many minutes?")

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and \
               type(msg.content)==int

    msg = await client.wait_for("message", check=check)

hello函数可完美运行。我的问题是番茄酱(当然还没有完成)。我使用此功能的目的是询问用户要学习多少分钟,然后要休息多少分钟,然后为这两个变量设置一个计时器。但是我什至不能让它发送第一个消息("Let's grab some tomatoes! For how many minutes?")。我不知道我在做什么错,特别是当第一个功能运行正常时。 预先感谢!

1 个答案:

答案 0 :(得分:1)

覆盖提供的默认on_message禁止运行任何其他命令。要解决此问题,请在client.process_commands(message)的末尾添加on_message行。

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('>hello'):
        msg = 'Hello, {0.author.mention}!'.format(message)
        await message.channel.send(msg)

    await client.process_commands(message)  # <----


@client.command(name="pomodoro")
async def _pomodoro(ctx):
    await ctx.channel.send("Let's grab some tomatoes! For how many minutes?")

    def check(msg):
        return msg.author == ctx.author and msg.channel == ctx.channel and \
               type(msg.content) == int

    msg = await client.wait_for("message", check=check)

Why does on_message make my commands stop working?