为什么我的 discord bot 命令(在程序中的 #NOT WORKING 中说明)没有给我任何错误并且没有给我回应?

时间:2021-06-21 15:27:29

标签: python discord discord.py bots

bot 命令以前可以运行,当我运行代码时,我没有收到任何错误或任何问题迹象。我正在使用 VS 代码。终端中唯一的就是 on_ready 函数打印“Bot is ready!”

import discord, random
from discord.ext import commands

bot = commands.Bot(command_prefix = '$')
keywords = ["bike", "tron", "disk"]
keyword_responses = [
    "Greetings, programs!", 
    "**Tron!**", 
    "Now this I can do.", 
    "Behold! The son of our maker!",
    "You'll never make it."]

@bot.event #presence
async def on_ready():
    await bot.change_presence(status = discord.Status.online, activity = discord.Game("Online"))
    print('Bot is ready!')

@bot.event #keyword response
async def on_message(message):
    for i in range(len(keywords)):
        if keywords [i] in message.content:
            for f in range(1):
                await message.channel.send(random.choice(keyword_responses))

#NOT WORKING
@bot.command() #clear command
async def clear(ctx, amount = 3):
    await ctx.channel.purge(limit = amount)
    await ctx.channel.send(f'**{amount} messages have been deleted.**')
#NOT WORKING

#NOT WORKING
@bot.command(name = "quotes") #quotes command
async def quotes(ctx):
    _quotes = [
     "Fortune favors the bold.",
     "Time is money.",
     "If you want to be happy, be.",
     "When life gives you lemons, make lemonade.",
     "Your time is limited, so don't waste it living someone else's life.",
     "The way to get started is to quit talking and begin doing.",
     "Life has a way of moving you past wants and hopes.",
     "You can't steal something that's designed to be given away free.",
     "Out there is a new world! Out there is our victory! Out there is our destiny!",
     "Some things are worth the risk",
     "The art of selflessness... it's about, 'Removing' ones self from the equation",
     "I... fight for the users!"]
    response = random.choice(_quotes)
    await ctx.send(response)
#NOT WORKING

token = "xxx"
bot.run(token)

1 个答案:

答案 0 :(得分:0)

正如 @Łukasz Kwieciński 在他的评论中已经指出的那样:

覆盖 on_message 会破坏 discord.ext.commands 框架。 为防止出现这种情况,请将 await bot.process_commands(message) 添加到 on_message 函数的末尾。

像这样:

@bot.event #keyword response
async def on_message(message):
    for i in range(len(keywords)):
        if keywords [i] in message.content:
            for f in range(1):
                await message.channel.send(random.choice(keyword_responses))
    await bot.process_commands(message)