Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

python - Discord bot command works some times

I am pretty new to discord.py thing and I am building a bot that can I shut down on command(but that doesn't turn off/make bot offline, it just disables it's functions). so I made this shutdown() command and sometimes works and other times it doesn't for some to me unknown reason. Some prints don't work either for some reason, I will mark them in the code:

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)

Thanks in forward!Any help is appreciated!

question from:https://stackoverflow.com/questions/65848915/discord-bot-command-works-some-times

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...