我的Discord Bot不响应命令,但是当有人写东西时它会记录用户ID。为什么是这种情况?我做错了什么? on_message侦听器中缺少某些内容吗?我添加到代码中的最后一件事是我正在使用的排名系统,但是即使我注释掉了,Bot仍然不会响应+ 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)
首先感谢您的帮助:)我真的很困惑,无法弄清楚我在做什么错
答案 0 :(得分:0)
尝试像这样更改命令名称
@bot.command()
async def macarena(message):
#code
它将在使用{prefix}macarena
答案 1 :(得分:0)
您需要在0x40223D
事件的末尾使用process_commands()
才能使其他命令起作用。 Read more
on_message