首页 > 解决方案 > Discord Bot 不响应命令

问题描述

我的 Discord Bot 不响应命令,但它会在有人写东西时记录用户 ID。为什么会这样?我做错了什么?on_message 监听器中是否缺少某些内容?我添加到我的代码中的最后一件事是我正在研究的等级系统,但即使我将其注释掉,机器人仍然不会响应像 +macarena 这样的命令。这是代码:

import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
from user import User

############## Init Variables #############

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
DATA = os.getcwd() + '\\data\\'

bot = commands.Bot(command_prefix='+')
bot.remove_command('help')

rank_list = []

############## Main Code #############
@bot.event
async def on_ready():
    for guild in bot.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{bot.user} is connected to the following guild:\n'
        f'{guild.name}(id: {guild.id})'
    )

    if(os.path.isfile(DATA + 'database.txt')):
        print('Database File for Rank System exists and is ready for processing!')
    else:
        print('Database File doesnt exist and will be created....')
        create_file()
        print('File is created and ready for use!')

@bot.event
async def on_message(message):
    #rank_system(message)
    pass


@bot.command(name='help')
async def help_command(ctx):
    embed = discord.Embed(
        title = "Bot Commands",
        description = "Hier werden alle Commands von diesem Bot aufgelistet. Alle nachfolgenden Commands müssen mit dem Präfix '+' aufgerufen werden.",
        color = discord.Color.blue()
    )
    embed.add_field(name="Algorithmen", value="Lexikon", inline=True)
    embed.add_field(name="Datenstrukturen", value="Lexikon", inline=True)
    embed.add_field(name="macarena", value="Fun Commands", inline=True)
    await ctx.send(embed=embed)

#Rank System
def rank_system(message):
    author = str(message.author)
    userid = str(message.author.id) 
    time = str(message.created_at)
    channel = str(message.channel)

    user = search_user(userid)
    if user == None:
        rank_list.append(User(author,userid,time,channel))
    else:
        user.add_xp()
    print(userid)

@bot.command(name='rank')
async def get_rank(message):
    print("I am Here")
    id = str(message.author.id)
    user = search_user(id)
    response = f"You are Rank {user.get_rank()} and you have {user.get_xp()}."
    await message.channel.send(response)
    

#Lexikon Commands
@bot.command(name='Algorithmen')
async def algo_command(message):
    response = "Es gibt viele verschiedene Algorithmen hier eine kurze Auflistung von den bekanntesten:\n\n- Bubble Sort\n- Quick Sort\n- Merge Sort"
    await message.channel.send(response)

@bot.command(name='Datenstrukturen')
async def datenstrukturen_command(message):
    response = "Es gibt viele verschiedene Datenstrukturen hier eine kurze Auflistung von den bekanntesten:\n\n- Stack\n- Queue\n- List"
    await message.channel.send(response)

#Vote Commands


#Fun Commands
@bot.command(name='macarena')
async def makarena_command(message):
    print("Funktioniert")
    response = "Hast du ernsthaft so viel Langeweile, dass du diesen Command ausprobierst....Schäm dich ;)"
    await message.channel.send(response)

#Sound Board


#Helper Class
def create_file():
    open(os.path.join(DATA, "database.txt"), "x")

def read_file():
    pass

def write_file(text):
    pass

def search_user(id):
    for x in rank_list:
        print("Loop User %s",x.get_userID())
        if x.get_userID() == id:
            return x
    return None


bot.run(TOKEN)

提前感谢您的帮助:)我真的很困惑,无法弄清楚我做错了什么

标签: pythonpython-3.xdiscordbotsdiscord.py

解决方案


尝试像这样更改您的命令名称

@bot.command()
async def macarena(message):
   #code

使用时会运行{prefix}macarena


推荐阅读