【问题标题】:My discord bot is not responding to any commands in replit (Python)我的不和谐机器人没有响应 replit (Python) 中的任何命令
【发布时间】:2021-08-18 04:26:53
【问题描述】:

我是编程初学者,最近才开始使用 python。我正在尝试在名为 replit 的 IDE 中创建一个不和谐的机器人。 Replit 似乎没有检测到错误,因为它允许我运行代码而无需下划线或指出任何行。但是,当我运行机器人并将命令输入 discord 时,机器人没有响应。当我回到 replit 时,它仍然没有检测到任何错误或在控制台中指出任何内容。我尝试以两种不同的格式制作机器人命令。第一个(用户打招呼并且机器人响应)有效,但之后的其他两个命令有多个响应,其中机器人应该从该列表中选择一个随机响应。这两个命令不起作用。代码如下:

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

  if message.content.startswith ('^hello'):
   await message.channel.send('Hello...( ・_・)ノ')

client.run(os.getenv('TOKEN'))

import os
import random
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
#help commands
bot = commands.Bot(command_prefix='^')
@bot.command(name='help')
async def help(ctx):
    omori_responses = [
        'OMORI’s main command categories are: FUN, CURRENCY, OMORI, SUNNY, BASIL, MARI, HERO, KEL, AUBREY, WHITESPACE, DREAMWORLD, and BLACKSPACE. Please use the command omo help <insert command category>',
        
            'OMORI’s main command categories are: FUN, CURRENCY, OMORI, SUNNY, BASIL, MARI, HERO, KEL, AUBREY, WHITESPACE, DREAMWORLD, and BLACKSPACE. Please use the command omo help <insert command category>' 
          ]
    response = random.choice(omori_responses)
    await ctx.send(response)
client.run(os.getenv('TOKEN'))
#whitespace commands
bot = commands.Bot(command_prefix='^')
@bot.command(name='ws')
async def white_space(ctx):
    omori_responses = [
        'Whitespace. A single black lightbulb hangs from the ceiling above...wherever above is',
        'Whitespace. You have been here since as far back as you can remember...whenever that is. Everything is bright white. There are no walls. There is a floor...it is always cold.',
        
            'Whitespace. Everything you need is here. A laptop, sketchbook, tissue box, blanket, and MEWO, your cat.',
            'Whitespace. There is a door here. Beyond this door is a colorful world, unlike here...' 
          ]
    response = random.choice(omori_responses)
    await ctx.send(response)
client.run(os.getenv('TOKEN'))

【问题讨论】:

  • 为什么要运行3次代码?您的订购方式也很可疑。如果您真的是新手,我建议您先阅读docs 或观看一些教程。
  • 谢谢你的建议,多米尼克。我实际上只使用了 2-3 个月的 python 并且是自学成才的。我想我已经跳过了一些步骤,所以感谢您提供文档链接,非常感谢。

标签: python discord chatbot


【解决方案1】:

每当您编写自己的on_message() event reference(覆盖默认的discord.py on_message())时,您都需要调用Bot.process_commands() method

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

    if message.content.startswith ('^hello'):
       await message.channel.send('Hello...( ・_・)ノ')

    await bot.process_commands(message)

【讨论】:

    猜你喜欢
    • 2021-11-21
    • 2021-06-17
    • 2019-04-13
    • 2021-06-27
    • 1970-01-01
    • 2022-10-13
    • 2021-07-07
    • 2022-10-24
    • 2021-12-03
    相关资源
    最近更新 更多