Events/commands not working - python

When I check for an event like on_message(message), every command stops working
If I remove the event, everything is back to normal.
With this code, commands are not working.
#bot.event
async def on_ready():
print("Connected.")
#bot.event
async def on_message(message):
print("test")
#bot.command(pass_context=True)
async def hello(ctx):
print("test")
While removing the event section, the command works normally.

As written here, you need to explicitly invoke the command when you override on_message
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)

Related

Trying to make a discord bot but on.message or message.content not working

I want the bot to use the gpt-3 API to answer questions but for some reason on.message is not working
import openai
import discord
openai.api_key = "apikey"
client = discord.Client()
#client.event
async def on_ready():
print('online')
async def on_message(message):
if message.content.startswith("!ask"):
print('I read the message')
question = message.content[5:]
response = openai.Completion.create(
engine="text-davinci-002",
prompt=f"{question}\n",
temperature=0.7,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
await message.channel.send(response.choices[0].text)
client.run('token')
Everything works fine and the 'online' appears but after that i don't know what's happening since i am not getting any errors (Sorry if its something obvious i am just now learning)
the
client.run('token')
open.api_key="apikey"
are obviously replaced with the real ones in my code
If you want to register an event, you have to put the client.event decorator on top of the event functions. Your on_message function certainly doesn't have one, so just put one on it.
#client.event
async def on_message(message):
...
You need to use #client.event for every event
#client.event
async def on_ready():
print('online')
#client.event # <- this is new
async def on_message(message):
# ...

Bot stopped working even though code is fine

This is my first time coding a discord bot and I have been following tutorials mainly but I wanted to make a unique game so I tried to make it myself. But after messing around with it for a while I realized that the only part of my code that works is the mcguccy is an idiot part none of the client. Command parts work.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='!')
#client.command()
async def ping1(ctx):
await ctx.send("pong!")
#client.event
async def on_ready():
await client.change_presence(status=discord.Status.online, activity=discord.Game("cranking 90s on shitters"))
print("bot is ready!")
#client.event
async def on_member_join(member):
print(f'{member} has joined :weary:.')
#client.event
async def on_member_remove(member):
print(f'{member} has left :weary:')
#client.command()
async def helb(ctx):
await ctx.send('#everyone look at this idiot')
#client.command()
async def ping(ctx):
await ctx.send(f'here you go: {round(client.latency * 1000)}ms')
#client.command()
async def commands(ctx):
await ctx.send('1. helb it will help you. 2. ping it will tell you the bots ping. ')
#client.command()
async def overlord(ctx):
await ctx.send("muah hah hah YOUR SERVER IS MINE")
keywords = ["mcguccy is an idiot", "kick mcguccy", "i want to nuke the server"]
#client.event
async def on_message(message):
for i in range(len(keywords)):
if keywords[i] in message.content:
for j in range(20):
await message.channel.send(f"#everyone someone has spammed :weary:")
There are a few things to look at here:
Your client variable is a Bot type, not a Client type.
client = commands.Bot(command_prefix='!')
This means you need to use #bot decorators instead of #client decorators:
#client.command()
async def helb(ctx):
...
# Becomes
#bot.command()
async def helb(ctx):
...
The .run() method is never called.
Because of this, the bot's event loop doesn't start. You need to initialize the Client event loop so the bot can listen for commands to pass:
if __name__=='__main__':
client.run(TOKEN)
That code needs to be the very last statement run in the file, as it's blocking.
The .run() command requires an API token.
You can acquire said token by creating a bot account if you don't have one already. If you do have one, you'll need to pass it as the first paramater to the .run() method.

Discord.py ctx commands not responding

None of my ctx commands work. Here I'll give an example. When I say p!test I want it to print hi and say test, but it's not responding. Can someone help?
#client.command()
async def test(ctx):
print('hi')
await ctx.channel.send('test')
If you're using a on_message event, you have to 'process commands' in the last line of the event. Otherwise, your commands won't work.
#client.event
async def on_message(message):
...
await client.process_commands(message)
Reference
discord.ext.commands.Bot.process_commands

Discord.py #bot.event

So I have a script that uses both #bot.event and #bot.command(). The problem is that when I have a #bot.event waiting the #bot.command() will not run.
Here is my code:
#bot.event
async def on_ready():
print("Bot Is Ready And Online!")
async def react(message):
if message.content == "Meeting":
await message.add_reaction("👍")
#bot.command()
async def info(ctx):
await ctx.send("Hello, thanks for testing out our bot. ~ techNOlogics")
#bot.command(pass_context=True)
async def meet(ctx,time):
if ctx.message.author.name == "techNOlogics":
await ctx.channel.purge(limit=1)
await ctx.send("**Meeting at " + time + " today!** React if you read.")
#bot.event ##THIS ONE HOLDS UP THE WHOLE SCRIPT
async def on_message(message):
await react(message)
When using a mixture of the on_message event with commands, you'll want to add await bot.process_commands(message), like so:
#bot.event
async def on_message(message):
await bot.process_commands(message)
# rest of code
As said in the docs:
This function processes the commands that have been registered to the bot and other groups. Without this coroutine, none of the commands will be triggered.
If you choose to override the on_message() event, you then you should invoke this coroutine as well.
References:
Bot.process_commands()
on_message()

Commands not being recognized by discord.py

I have a simple discord.py set up trying to use .ping, but in this current instance, the actual sending of ".ping" results in nothing being sent by the bot. Is there something I'm missing here?
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix = ".")
#bot.event
async def on_ready():
print("Everything's all ready to go~")
#bot.event
async def on_message(message):
author = message.author
content = message.content
print(content)
#bot.event
async def on_message_delete(message):
author = message.author
channel = message.channel
await bot.send_message(channel, message.content)
#bot.command()
async def ping():
await bot.say('Pong!')
bot.run('Token')
Ensure that you have await bot.process_commands(message) somewhere in your on_message event.
Why does on_message make my commands stop working?
Overriding the default provided on_message forbids any extra commands from running. To fix this, add a bot.process_commands(message) line at the end of your on_message. For example:
#bot.event
async def on_message(message):
# do some extra stuff here
await bot.process_commands(message)
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Categories