Search code examples
pythondiscorddiscord.py

How do I make an announcement command in Discord.py?


No matter how hard I look I cannot find a tutorial on how to do this in Discord.py, It's all Discord.js. Here is my current code;

@bot.command(brief='announce [message]')
    async def announce(ctx, message : str):
            print(str(message))
            if(str(ctx.message.author) == user):
                    await ctx.send('User Authentication Successful')
                    try:
                            for chan in channels:
                                    try:
                                            channel = bot.get_channel(chan)
                                            info = discord.Embed(title='New Announcement!', description=str(message), color=0xFFFFFF)
                                            await channel.send(embed=info)
                                    except Exception as e:
                                            await ctx.send(e)
                                            await ctx.send("Error: " + str(chan))
                    except Exception as e:
                            await ctx.send(e)

And this is the error I am getting.

Ignoring exception in command announce
Traceback (most recent call last):
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 367, in invoke
    yield from self.prepare(ctx)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 345, in prepare
    yield from self._parse_arguments(ctx)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 304, in _parse_arguments
    transformed = yield from self.transform(ctx, param)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 212, in transform
    raise MissingRequiredArgument('{0.name} is a required argument that is missing.'.format(param))
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

Please Help Me :(

EDIT: I'm sorry if anything was confusing. I'm trying to make an -announce command. Where I type -announce and it announces (embedded) whatever was typed.


Solution

  • You can make an announcement command like this:

    Option 1: Prefix Command

    @bot.command(pass_context=True)
    async def announce(ctx, *, announcement: str):
        channel = bot.get_channel(CHANNEL_ID) # channel ID here (int)
        await channel.send(announcement)
        return
    

    Option 2: Slash Command

    @bot.tree.command(name='announce', description='send an announcement.')
    async def announce(interaction: discord.Interaction, *, announcement: str):
        channel = bot.get_channel(CHANNEL_ID) # channel ID here (int)
        await channel.send(announcement)
        return
    

    Optionally... you can mention a role by using the role ping format in the message. ex: await channel.send(f'<@&ROLE> :: {announcement}')

    It's important for * to not be the first argument, but announcement: str always has to be following..