【问题标题】:Discord Bot doesnt respond to CommandsDiscord Bot 不响应命令
【发布时间】:2020-12-15 12:15:01
【问题描述】:

我的 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)

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

【问题讨论】:

    标签: python python-3.x discord bots discord.py


    【解决方案1】:

    您需要在on_message 事件的末尾使用process_commands() 以使其他命令起作用。 Read more

    @bot.event
    async def on_message(message):
        #some code if you want, if you want to pass then don't make on_message event
        await bot.process_commands(message)
    

    【讨论】:

      【解决方案2】:

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

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

      使用{prefix}macarena时会运行

      【讨论】:

      • 已更改,但 Bot 仍无响应
      猜你喜欢
      • 2021-08-28
      • 2021-02-17
      • 2023-03-09
      • 2021-04-17
      • 2022-10-23
      • 2021-10-23
      • 2022-01-04
      相关资源
      最近更新 更多