Adding a reminder feature to Discord

As many of you know, I released my Discord Bot Framework the other week. This has prompted me to put sometime into developing new features for the bot’s I manage.

I’ve always been a fan of the “remind” functionality that many Reddit communities have. In my Discord, we are often sending media content that I want to watch but don’t want to forget about. Hence the need for the command “!remind”. Surprisingly this was pretty simple code. Take a look below for the functionality I created.

@client.command(name='remind')
async def remind(ctx, duration: str):
    try:
        # Parse the duration (e.g., "10s" for 10 seconds, "5m" for 5 minutes, "1h" for 1 hour)
        time_units = {'s': 1, 'm': 60, 'h': 3600}
        time_value = int(duration[:-1])
        time_unit = duration[-1]

        if time_unit not in time_units:
            await ctx.send("Invalid time format! Use s (seconds), m (minutes), or h (hours).")
            return

        delay = time_value * time_units[time_unit]

        # Check if the command is a reply to another message
        if ctx.message.reference and ctx.message.reference.resolved:
            # Get the replied message
            replied_message = ctx.message.reference.resolved
            message_link = f"https://discord.com/channels/{ctx.guild.id}/{ctx.channel.id}/{replied_message.id}"
        else:
            await ctx.send("Please reply to a message you want to be reminded about.")
            return
        await ctx.send(f"Okay, I will remind you in {duration} to check out the message you referenced: {message_link}")

        # Wait for the specified duration
        await asyncio.sleep(delay)

        # Send a reminder message with the link to the original message
        await ctx.send(f"{ctx.author.mention}, it's time to check out your message! Here is the link: {message_link}")

    except ValueError:
        await ctx.send("Invalid time format! Use a number followed by s (seconds), m (minutes), or h (hours).")

Feel free to utilize this code and add it to your own Discord bot! As always, if this code is helpful please share it with your friends or on your own social media!


Posted

in

by

Comments

Leave a Reply