【问题标题】:Python Discord Bot: how to interact with the user?Python Discord Bot:如何与用户交互?
【发布时间】:2020-12-18 16:16:42
【问题描述】:

我正在尝试为我的服务器制作 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?")。我不知道我做错了什么,特别是当第一个功能工作正常时。 提前致谢!

【问题讨论】:

    标签: python discord


    【解决方案1】:

    覆盖默认提供的on_message 禁止运行任何额外的命令。要解决此问题,请在 on_message 末尾添加 client.process_commands(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?

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 2021-10-16
      • 2022-01-21
      • 1970-01-01
      • 2022-12-01
      • 2022-11-06
      • 2019-07-21
      • 2021-10-24
      • 2020-08-25
      相关资源
      最近更新 更多