简体   繁体   English

Discord bot in Python - 没有命令在工作

[英]Discord bot in Python - no commands are working

I want to create a bot that will respond to commands with arguments. I have gotten it to respond to strings in messages, but when i try to make commands working, the bot seems not to react at all.我想创建一个机器人来响应 arguments 的命令。我已经让它响应消息中的字符串,但是当我尝试使命令工作时,机器人似乎根本没有反应。 I have studied the documentation, put a lot of effor into trying to find the answer on the inte.net, but no help.我研究了文档,投入大量精力试图在 inte.net 上找到答案,但没有帮助。 My code can be the exact same as someone else's on the inte.net, but the bot never responds, while still responding to messages.我的代码可能与 inte.net 上其他人的代码完全相同,但机器人从不响应,但仍在响应消息。 I wonder if there's something really basic that I'm missing about how the commands are supposed to work.我想知道是否有一些关于命令应该如何工作的真正基本的东西我错过了。

I have imported the right library, I use the exact same syntax many people are sharing on the inte.net, yet the commands just don't do anything.我导入了正确的库,我使用许多人在 inte.net 上共享的完全相同的语法,但这些命令什么也没做。 I use replit.com which is a browser IDE.我使用 replit.com,这是一个浏览器 IDE。

This snippet of code is like half the code from the bot, he's quite dumb so far.这段代码就像机器人的一半代码,到目前为止他还很笨。 Everything i tried other than commands has worked so far.到目前为止,我尝试过的所有命令都有效。 I usually also don't work with Python, but i just followed the syntax i saw in tutorials to not make mistakes.我通常也不使用 Python,但我只是按照我在教程中看到的语法来避免出错。 When i run this, the bot logs in successfully, then if i send.tst the bot just doesn't do anything.当我运行它时,机器人成功登录,然后如果我 send.tst 机器人什么都不做。

from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(name='tst')
async def test(ctx):
  await ctx.send('testt')

I mainly used this tutorial: https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python Also the library documentation: https://discordpy.readthedocs.io/en/stable/index.html我主要使用了这个教程: https://realpython.com/how-to-make-a-discord-bot-python/#how-to-make-a-discord-bot-in-python还有库文档: https ://discordpy.readthedocs.io/en/stable/index.html

Based on the full code:基于完整代码:

You shouldn't use both discord.Client and commands.Bot , because you are defining two different bots, but run only one of them.您不应同时使用discord.Clientcommands.Bot ,因为您定义了两个不同的机器人,但只运行其中一个。 commands.Bot extends the discord.Client . commands.Bot扩展了discord.Client Think about commands.Bot as a subclass of discord.Client .commands.Bot视为discord.Client的子类。 Everywhere where you've used client before, now you can use bot .您以前在任何地方使用过client ,现在您可以使用bot

The code you posted would run correctly like this:您发布的代码将像这样正确运行:

import discord
import os
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(name='tst')
async def test(ctx):
  await ctx.send('testt')

@bot.event
async def on_ready():
  print ('Successful login as {0.user}'.format(bot)) 

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

Original answer:原答案:

(it might be a solution to someone else looking for it) (这可能是其他人寻找它的解决方案)

Are you using the on_message event?你在使用on_message事件吗? That prevents the command processor from getting the messages.这会阻止命令处理器获取消息。 You have to call the command processor manually, like so:您必须手动调用命令处理器,如下所示:

@bot.event
async def on_message(message):
    # Do your stuff
    await bot.process_commands(message)

According to the documentation you should call this function when you override the on_message event.根据文档,当您覆盖 on_message 事件时,您应该调用此 function。

By default, this coroutine is called inside the on_message() event.默认情况下,这个协程在 on_message() 事件中被调用。 If you choose to override the on_message() event, then you should invoke this coroutine as well.如果您选择覆盖 on_message() 事件,那么您也应该调用这个协程。

import discord
import os
from discord.ext import commands

bot = commands.Bot(command_prefix='!')

@bot.command(name='tst')
async def test(ctx):
  await ctx.send('testt')

client = discord.Client()

@client.event
async def on_ready():
  print ('Successful login as {0.user}'.format(client)) 

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

I didn't want to post the whole thing, but it's really nothing special so here it goes.我不想发布整个事情,但它真的没什么特别的,所以就这样吧。 for a moment i thought that might be the solution, but even if i clear it of all message related code, it still does nothing.有那么一刻我认为这可能是解决方案,但即使我清除了所有与消息相关的代码,它仍然无能为力。 Could it be conflicting with the on_ready message?它会不会与 on_ready 消息冲突? That one just goes to the console on the IDE website..那个只是转到 IDE 网站上的控制台..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM