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)
Related
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
I want to create a command that only works if a user is DMing their command. How do I do that? I am using discord.py 1.5.1, and I'm fairly new to discord.py.
Here is my code so far:
from discord.ext import commands, tasks
import discord
intents = discord.Intents.all
bot = commands.Bot(command_prefix='$', description='- shows this message', intents=intents)
---- snip ----
#commands.command(brief='a DM command')
async def dm_command(ctx):
# do stuff here
bot.run('TOKEN')
When I try DMing the command, the bot doesn't pick up the DM I sent. Should I specify the command in on_message()?
Here is my code for your query:
#bot.command()
async def dm_command(ctx):
if isinstance(ctx.channel, discord.channel.DMChannel):
#do stuff here
First of all, the decorator you use is not what I was taught when i first learned discord.py, so I changed the decorator from #commands.command(brief='a DM command') to #bot.command() (Feel free to change back if it works for you). Then, the rest is fairly simple. I just checked if the channel was a DM channel, and thats it! If you have any questions about my code or if you have unforseen errors, follow up!
#client.command()
async def dm(ctx):
await ctx.author.send('Hi im a discord bot!')#this is the message that will pop up in ur dms when u input the cmd
client.run('Token')
Done! Thanks to anyone who helped me out:) Still maybe you have better answers, so, feel free to answer!
I know, this may look like a stupid question, but if you consider that I'm a beginner in making bots for Discord in Python beyond all my other Python knowledge and Stack Overflow being probably a place just for that, I hope it wouldn't. (I'm so new that I literally woke everyone in the home up by my happiness when I saw my bot turned online lol)
As I saw in other posts, tutorial, etc.; (don't mind the usage of , and ; it could be wrong) we have to specify the ID of the channel, so how can we just reply in the channel the user has sent the command in? Maybe getting the ID of the current channel with some type of a command? I don't know.
import discord
TOKEN = 'XXXXX'
client = discord.Client()
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!hello'):
msg = 'Hi {0.author.mention}'.format(message)
await client.send_message(message.channel, msg)
#client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
(Since I'm lazy regenerating tokens, i made it xxxxx in this example but don't worry i put it in the normal code.)
As I saw, there is no same question even though there are similar ones (i couldnt see an answer or a question definitely because everybody knows how to do that)
The problem is in the send_message part. It gives an error.
You just have to get the channel object and send a message to it using message.channel.send() and not client.send_message()
if message.content.startswith('!hello'):
msg = 'Hi {0.author.mention}'.format(message)
await message.channel.send(msg)
In the future you may wanna try something like this or maybe come across it by any reason:
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.command()
#This is defining a '!hello' command
async def hello(ctx):
#In this case you have to use the ctx to send the message
#Good to remember that ctx is NOT a parameter wich the user will give
msg = f'Hi {ctx.author.mention}'
await ctx.send(msg)
There are two ways you can send messages. One is using the on_message but I would recommend against it because it might interfere with other commands in your code.
Example:
import discord
client = discord.Client(command_prefix='!')
#client.event
async def on_ready()
print('Bot is ready')
#example message
#client.command()
async def test(ctx):
await ctx.send('This message is a test')
client.run('YOUR TOKEN')
You can call the test command by doing "!test" in the bot channel.
import os
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='Your Prefered Prefix')
#client.command()
async def hi(ctx):
channel = ctx.message.channel
await channel.send(f"hi {ctx.author.name}!")
client.run(Token)
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.
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