Discord.py ctx commands not responding - python

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

Related

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.

!help [command_name] discord.py

I’m creating a discord bot using python. I did a custom help command (don’t worry, for this part I’m ok), and I want to do a !help [command_name]. Let me explain. If we do !help, the bot will send the basic help command, but if I do !help [command_name], like !help ping, it will send informations a bout the ping command. I tried to do this
#bot.command()
async def help ping(ctx):
…
await ctx.send(embed=embed)
#bot.command()
async def help(ctx):
…
await ctx.send(embed=embed)
It didn’t worked, so I did this:
#bot.event
async def on_message(message):
if message.content.startswith(+help ping):
…
await message.channel.send(embed=embed)
#bot.command()
async def help(ctx):
…
await ctx.send(embed=embed)
There, the !help ping worked, but not the !help. And now, I’m there. Can you tell me a simple solution using #bot.command() or #bot.event?
You can also do Grouping on the commands.
Example :
#client.group(invoke_without_command = True) # for this main command (.help)
async def help(ctx):
await ctx.send("Help! Categories : Moderation, Utils, Fun")
#help.command() #For this command (.help Moderation)
async def Moderation(ctx):
await ctx.send("Moderation commands : kick, ban, mute, unban")
#help.command() #And for this command (.help Utils)
async def Uitls(ctx):
await ctx.send("Utils : ping, prefix")
#help.command() #And lastly this command (.help Fun)
async def Fun(ctx):
await ctx.send("Fun : 8ball, poll, headsortails")
Thank me later :D
...
from discord.ext.commands import Cog, Group, Command, HelpCommand
... # bot and bot commands
class MyCustomHelpCommand(HelpCommand):
async def send_bot_help(self, mapping): # Mapping[Optional[Cog], List[Command]]
... # write you'r help message here
self.get_destination().send(message)
async def send_cog_help(self, cog: Cog):
... # write you'r help message here
self.get_destination().send(message)
async def send_group_help(self, group: Group):
... # write you'r help message here
self.get_destination().send(message)
async def command_not_find(self):
... # write you'r help message here
self.get_destination().send(message)
bot.help_command = MyCustomHelpCommand()
docs: HelpCommand
also see: DefaultHelpCommand, MinimalHelpCommand
try it:
...
from discord.ext.commands import Command
... # bot and bot commands
# command example 1
#client.command(help='I am an example')
async def ex1(ctx):
await ctx.send('I am working')
# command example 2
#client.command()
async def ex2(ctx):
'''I am an example too'''
await ctx.send('I am working too')
#client.command(name='help')
async def my_help_command(ctx, command_name: str = 'help')
await ctx.send(client.get_command(command_name) or 'Command Not Find')
Output with '!' command_perfix:
me: !help ex1
bot: I am an example
me: !help ex2
bot: I am an example too
me: !help i_an_not_command
bot: Command Not Find
You can do this:
#bot.commands()
async def help(ctx, cmd=None):#if the user doesn't give any arguments for "cmd" then it will count it as None
if not cmd:#checks if the user gave arguments for "cmd"
await ctx.send("Help is here!")#the message the bot should send
elif cmd.lower()=="ping":#checks if the arguments given for "cmd" is ping and I set "cmd.lower()" so that the argument is not case sensitive
await ctx.send("Help for command `ping` is here!")
else:#if the arguments doesn't match any of the if statements it will show an error
await ctx.send(f"No command named `{cmd.lower()}`")#you can set anything for the error message
Hope you find it helpful
And sorry for answering this late

Why bot.commands won't work in discord.py?

This is my code:
import discord
import asyncio
from discord import Game
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.event
async def on_ready():
print(bot.user.id)
print("botstart")
game = discord.Game("abc")
await bot.change_presence(status=discord.Status.online, activity=game)
#bot.event
async def on_message(message):
if message.content.startswith("hi"):
await message.channel.send("Hello")
if message.content.startswith("ping"):
await message.channel.send("Pong")
#bot.command
async def ping(ctx):
ctx.send("Pong!")
bot.run("(my token)", bot=True)
For some reason, bot.event works but #bot.command(!ping) part doesn't. I tried to fix it but i couldn't, even watching discord.py guides.
What am I doing wrong?
Since you are overriding the on_message event, the bot no longer responds to any commands. To fix this add await bot.process_commands(message) to your on_message function for it to handle commands normally.
Source:
https://discordpy.readthedocs.io/en/latest/faq.html#why-does-on-message-make-my-commands-stop-working

Allow Discord Rewrite bot to respond to other bots

I have a Discord bot and a webhook setup for a Discord channel to send a command every hour on the dot. However it seems that Discord Rewrite by default ignores commands sent from other bots. How do I go about disabling this?
Do I need to modify something on a per-command function or the on_message function?
Current on_message:
#bot.event
async def on_message(message):
await bot.process_commands(message)
Try adding a check for it:
on_message:
if message.author.bot == True:
#Do something
command:
if ctx.author.bot == True:
#Do something
The solution is:
#bot.event
async def on_message(message):
ctx = await bot.get_context(message)
await bot.invoke(ctx)
as per OP #Cynthia Valdez's edit to the question.

Events/commands not working

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)

Categories