How to send a message in discord.py - discord.py

How do you send a normal message when a user types the command? I have tried using #client.command
async def ping
but it does not work D:

You can use await ctx.send("Your message here"), make sure that you have Context passed in your #client.command.
If you have anymore questions, feel free to ask me :)

Use this as a guide if it helps
#client.command
async def ping(ctx):
await ctx.send("Your message")

here ctx is the context and hi is command
Code:
#client.command
async def hi(ctx):
await ctx.send("Message which you want to send")

Related

Can't figure out how to use discord.py ctx

I have tried to convert my discord bot's commands to using ctx instead of interactions but the code I have tried below does not work.
#bot.command(description="Says test")
async def test(ctx):
await ctx.send("Test")
My issue is that the command never loads.
Updated code but still broken
import discord
from discord import ui
from discord.ext import commands
bot = commands.Bot(command_prefix="/", intents = discord.Intents.all())
# Run code
#bot.event
async def on_ready():
print('The bot has sucuessfully logged in: {0.user}'.format(bot))
await bot.change_presence(activity=discord.Activity(type = discord.ActivityType.listening, name = f"Chilling Music - {len(bot.guilds)}"))
# Commands
#bot.command(description="Says Hello")
async def hello(ctx):
await ctx.send(f"Hello {ctx.author.mention}!")
#bot.command(description="Plays music")
async def play(ctx, song_name : str):
await ctx.author.voice.channel.connect()
await ctx.send(f"Now playing: {song_name}")
#bot.command(description="The amount of servers the bot is in")
async def servercount(ctx):
await ctx.send(f"Chill Bot is in {len(bot.guilds)} discord servers!")
#bot.command(description="The bots ping")
async def ping(ctx):
await ctx.send(f"🏓Pong\nChill Bot's ping is\n{round(bot.latency * 1000)} ms")
#bot.command(description="Announcement command")
async def announce(ctx, message : str):
await ctx.send(message)
#bot.command(description="Support Invite")
async def support(ctx):
await ctx.send(f"{ctx.author.mention} Please join our server for support! [invite]", ephemeral=True)
#bot.command(description = "Syncs up commands")
async def sync(ctx):
await ctx.send("Synced commands!", ephemeral=True)
bot.run('')
Your commands don't work because you incorrectly created an on_message event handler.
By overwriting it and not calling Bot.process_commands(), the library no longer tries to parse incoming messages into commands.
The docs also show this in the Frequently Asked Questions (https://discordpy.readthedocs.io/en/stable/faq.html#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.
Also, don't auto-sync (you're doing it in on_ready). Make a message command and call it manually whenever you want to sync.

How do I make it so this message gets sent a specific channel? (Discord.py)

I want the message to get sent to a specific channel
async def on_message_edit(before, after):
await before.channel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
To send a message in a specific channel you need first to fetch the choosed channel by using his id by using get_channel() like this:
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
Then your event block should look like this:
#client.event
async def on_message_edit(before, after):
specificChannel = bot.get_channel(int(YOUR_CHANNEL_ID_HERE))
await specificChannel.send(
f'{before.author} edited a message.\n'
f'Before: {before.content}\n'
f'After: {after.content}'
)
Useful link:
get_channel()
Discord.py documentation
maybe this can help u :)
#client.event
async def on_member_join(member:Member):
if member.bot:
guild:discord.Guild = member.guild
role = guild.get_role(config.botRoleID)
channel:discord.TextChannel = guild.get_channel(YOUR_CHANNEL_ID)
await member.add_roles(role)
await channel.send(f'Set Bot role to "{member.display_name}" 😌')

How do i make a welcoming command in discord.py?

Im trying to make a welcome message for my bot but the message never works. I have all 3 intents enabled and i dont know whats wrong. Can someone please help?
#bot.event
async def on_member_join(member):
channel = bot.get_channel(906928559602421830)
embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
await channel.send(embed=embed)
What does the alfred stands for?
And for the code:
#bot.event
async def on_member_join(member):
guild = member.guild
channel = guild.get_channel(channel_id)
embed=discord.Embed(title="Welcome!",description=f"{member.mention} Just Joined")
await channel.send(embed=embed)

I can't make the bot write that you don't have rights to use the command

I created a lot of moderation commands, and only allowed admins to use them.I also really want to make such a scheme:If a person who does not have the right to use the command writes it,the bot sends a message that you do not have the right to use this command.Please help me figure this out!
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
You can use a error handler.
#client.command()
#commands.has_permissions(view_audit_log=True)
async def ban(ctx,member:discord.Member,reason):
emb = discord.Embed(title="ban",color=0xff0000)
emb.add_field(name='Модератор',value=ctx.message.author.mention,inline=False)
emb.add_field(name='Нарушитель',value=member.mention,inline=False)
emb.add_field(name='Причина',value=reason,inline=False)
await member.ban()
await channel.send(embed = emb)
#ban.error
async def ban_error(ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send('You do not have the required permissions to use this command.')

Making say command bot owner only

I am trying to make my !say command only work for the bot owner. This is what I currently have
#bot.command(pass_context = True)
async def say(ctx, *args):
mesg = ' '.join(args)
await bot.delete_message(ctx.message)
return await bot.say(mesg)
The code works but I want to make it so only I (the bot owner) can run the command.
You can also use the decorator #is_owner().
#bot.command(pass_context = True)
#commands.is_owner()
async def say(ctx):
your code...
Try doing it this way by adding an if statement
#bot.command(pass_context=True)
async def say(ctx):
if ctx.message.author.id =='bot owner id':
then execute the following code
This is how I did it - do comment if this does not work as intended.
#client.command()
#commands.is_owner()
async def say(ctx, *, message):
await ctx.send(f"{message}")
Have fun with your coding! Sorry for the late reply, I was reading old questions and seeked for unsolved issues
(remade)
The documentation for check in the 1.0 docs has the below example (slightly modified.)
def user_is_me(ctx):
return ctx.message.author.id == "Your ID"
#bot.command(pass_context = True)
#commands.check(user_is_me)
async def say(ctx, *args):
mesg = ' '.join(args)
await bot.delete_message(ctx.message)
return await bot.say(mesg)
How to find your ID

Resources