Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - My bot wont run the on_message/on_ready event properly

When i tried to start up my bot, it wouldnt run the on_message event and it wouldnt run the on_ready event either.

Here is my code:

import os
from discord.ext import commands
from datetime import datetime

client = commands.Bot(command_prefix = ";", help_command = None)

dates = datetime.now()
date = datetime.today()
client.event
async def on_ready():
    print("bot is ready")


client.event
async def on_message(message):
    author = message.author
    text = message.content
    print(author + " said at " + dates + " : " + text)
    client.process_commands(message)

I have tried to get rid of the on_message event to see if that was the problem, and it didnt fix it. Does anyone have a fix for this?

question from:https://stackoverflow.com/questions/65858057/my-bot-wont-run-the-on-message-on-ready-event-properly

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

1 Reply

0 votes
by (71.8m points)

You are not actually using the correct decorators for the on_ready and on_message. Any decorator should start with a @, notice how you are only using client.event instead of @client.event.

Also make sure to activate some intents for your bot or you will not be able to access some specific information.

intents = discord.Intents.default()
client = commands.Bot(command_prefix = ";", help_command = None, intents = intents)

Your corrected code should be something like this:

import os
from discord.ext import commands
from datetime import datetime

intents = discord.Intents.default()
client = commands.Bot(command_prefix = ";", help_command = None, intents = intents)

dates = datetime.now()
date = datetime.today()
@client.event
async def on_ready():
    print("bot is ready")


@client.event
async def on_message(message):
    author = message.author
    text = message.content
    print(author + " said at " + dates + " : " + text)
    await client.process_commands(message)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...