'Making a say command in discord.py

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.



Solution 1:[1]

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')

Solution 2:[2]

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}")

Solution 3:[3]

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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vihaan Patil
Solution 2 ShinyDragon96
Solution 3 Fugi