0

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

感谢转发!任何帮助表示赞赏!

4

1 回答 1

1

Your bot is not processing the commands (other commands should also not work), you should add bot.process_commands at the end of the on_message event.

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

    await bot.process_commands(message)

Reference:

-Bot.process_commands

于 2021-01-22T16:25:54.607 回答