将它们移至齿轮后,Discord Bot机器人不响应命令

时间:2019-10-27 01:50:53

标签: python bots discord

几天前,我决定将Discord Bot中的所有命令移至扩展名/齿轮文件中。之后,Discord完全忽略任何命令(即使是在主文件中声明的命令)。日志很好,没有错误或崩溃。我尝试了许多在youtube,github等上找到的方法

这是主要代码:

import discord
from discord.ext import commands
import asyncio
import random
import string
import requests
import json
import os


bot = commands.Bot(command_prefix = '?')
extensions = ['cogs.com']

if __name__ == '__main__':
    for extension in extensions:
        try:
            bot.load_extension(extension)
        except Exception as error:
            print('{} cannot load the file. [{}]'.format(extension, error))

@bot.event
async def on_ready():
    await bot.change_presence(game=discord.Game(name="type `?help` for help"))
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')


@bot.event
async def on_message(message):

# here I've got a lot of bot events (responding, sending funny memes etc). Surprisingly this part works absolutely fine

bot.run("TOKEN", bot=True, reconnect=True)

和齿轮文件(com.py)。这里我有很多命令,但是我决定只留下基本的命令:

import discord
from discord.ext import commands

class Com(): 
    def __init__(self, bot):
            self.bot = bot


    @commands.command()
    async def test(self):
        print('TEST') # Logs don't show anything after my command. It looks like bot doesn't even read it... or read it and doesn't send anything back?
        await self.bot.send('test')


def setup(bot):
    bot.add_cog(Com(bot))

discord.py ver = 0.16.12

如果有人可以帮助,那就太好了。谢谢

1 个答案:

答案 0 :(得分:0)

短暂休息后,我回到了这个问题,并通过添加以下内容解决了该问题:

await bot.process_commands(message)

@bot.event
async def on_message(message):

我完全忘记了这条线。这就是为什么bot忽略了我的命令。

谢谢大家,祝你愉快