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
Related
I am having the problem where if I run my bot, the first time I use a command, link !hello it send 'Hello (and the author)' back. But when I then try it again, it doesn't respond anymore. It does print in the console from the event_message. Then if I use another command like the !test, it gives response, but when I try that then again, it doesn't, but if I try !hello then, it works again, but only for that 1 time, until I used another command first.
Here's the code:
class Bot(commands.Bot):
def __init__(self):
super().__init__(token='MYTOKEN', prefix='!', initial_channels=['MYCHANNEL'])
async def event_ready(self):
print(f'Logged in as | {self.nick}')
print(f'User id is | {self.user_id}')
async def event_message(self, message):
if message.echo:
return
print(message.content)
await self.handle_commands(message)
#commands.command()
async def hello(self, ctx: commands.Context):
await ctx.send(f'Hello {ctx.author.name}!')
#commands.command()
async def test(self, ctx: commands.Context):
await ctx.send(f'test {ctx.author.name}!')
bot = Bot()
bot.run()```
I asked them in the discord. Giving the bot account moderator fixed the problem, since before it couldn't send the same message again too fast after each other
I want to make a simple say command in discord.py (ex.!say something - the bot says "something" and deletes the command message) but every code I found doesn't work for me. I'm new to python and discord.py and would really appreciate some help.
Thanks in advance.
You could also try something much easier like this:
#client.command()
async def say(ctx, message):
if message != None
ctx.channel.send(message)
elif message == None:
ctx.channel.send('Give me something to say')
This is the easiest way I've been able to do this, hope it helps!
#client.command()
async def say(ctx, *, text):
await ctx.message.delete()
await ctx.send(f"{text}")
You can find a lot of useful information on what you can make your discord bot do in the discord.py API reference. This is an example which should do what you want it to:
import discord
from discord.ext import commands
Set your bot's intents and command prefix as well as your bot token:
intents = discord.Intents().default()
bot = commands.Bot(command_prefix='!', intents=intents)
token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
Define a command '!say' with a parameter 'msg', which the bot should reply:
#bot.command(name='say')
async def audit(ctx, msg=None):
if msg is not None:
await ctx.send(msg)
Now you can delete the message which invoked the command (needs permissions!):
await ctx.message.delete()
Finally, run the bot:
bot.run(token)
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
Every I try to use the commands.Bot(command_prefix=''), the program reads it as an error. For example, with the code below, it comes out with
Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "-ping" is not
found Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "ping" is not
found
and is repeated a few times before what i wanted the bot to say (Pong!), is sent 2 or more times in the server...
\I think it might be looping? I'm not sure, but I got it to work once, but the longer I waited, every time used the command it sent more responses? -it sent 16 'Pong's last time I tried... Is there anything I can do about this?\
How can i fix this?
from discord.ext import commands
client = commands.Bot(command_prefix='-')
#client.event
async def on_ready():
print("Bot is ready for use...")
#client.command()
async def ping(ctx):
await ctx.send('Pong')
client.run('TOKEN')
The problem doesn't come from you prefix, you just forgot parentheses after your client.command decorator:
from discord.ext import commands
client = commands.Bot(command_prefix='-')
#client.event
async def on_ready():
print("Bot is ready for use...")
#client.command()
async def ping(ctx):
await ctx.send('Pong')
client.run('TOKEN')
The client.event decorator doesn't have any arguments so you don't need parentheses but client.command() can have arguments like name=, brief=, description=, aliases, ... so you need parentheses. ^^
I have searched around a lot for this answer, and I haven't found it. I want to use a suggestion command, that whenever someone uses it to suggest an idea, it DMs me, and me only.
You'll have to use the send_message method. Prior to that, you have to find which User correspond to yourself.
#client.event
async def on_message(message):
# we do not want the bot to reply to itself
if message.author == client.user:
return
# can be cached...
me = await client.get_user_info('MY_SNOWFLAKE_ID')
await client.send_message(me, "Hello!")
#client.event
async def on_message(message):
if message.content.startswith("#whatever you want it to be")
await client.send_message(message.author, "#The message")
Replace the hashtagged things with the words that you want it to be. eg.: #whatever you want it to be could be "!help". #The message could be "The commands are...".
discord.py v1.0.0+
Since v1.0.0 it's no longer client.send_message to send a message, instead you use send() of abc.Messageable which implements the following:
discord.TextChannel
discord.DMChannel
discord.GroupChannel
discord.User
discord.Member
commands.Context
Example
With bot.command() (recommended):
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
#bot.command()
async def suggest(ctx, *, text: str):
me = bot.get_user(YOUR_ID)
await me.send(text) # or whatever you want to send here
With on_message:
from discord.ext import commands
bot = commands.Bot()
#bot.event
async def on_message(message: discord.Message):
if message.content.startswith("!suggest"):
text = message.content.replace("!suggest", "")
me = bot.get_user(YOUR_ID)
await me.send(text) # or whatever you want to send here