Discord bot 命令有时会起作用

时间:2021-01-22 16:20:52

标签: python discord discord.py dotenv

我对 discord.py 很陌生,我正在构建一个可以根据命令关闭的机器人(但这不会关闭/使机器人离线,它只是禁用了它的功能)。所以我做了这个 shutdown() 命令,有时会起作用,有时会因为某些我不知道的原因而不起作用。有些打印由于某种原因也不起作用,我会在代码中标记它们:

import discord
import os
from dotenv import load_dotenv
from discord.ext import commands
import random

load_dotenv()

token = 'bot token'

bot = commands.Bot(command_prefix = '$')

@bot.event
async def on_ready():
    print('-----------------------------------')
    print('we have logged in as {0.user}'.format(bot))
    print('-----------------------------------')

@bot.command()
async def ping(ctx):
    await ctx.channel.send("pong")

@bot.command()
async def say(ctx, *args):
    response = ""

    for arg in args:
        response = response + " " + arg

    await ctx.channel.send(response)

@bot.command()
async def shutdown(ctx):#this command doesn't work properly
    await bot.close()
    print("We have logged off as {0.user}".format(bot))#this print doesn't work most of the time

@bot.event
async def on_message(message):
    if message.content == "opinion":
        await message.channel.send("not an opinion")

bot.run(token)

感谢转发!感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

您的机器人没有处理命令(其他命令也应该不起作用),您应该在 bot.process_commands 事件的结束添加 on_message

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

    await bot.process_commands(message)

参考:

-Bot.process_commands