Skip to content

Instantly share code, notes, and snippets.

@proguy914629bot
Last active August 26, 2023 15:17
Show Gist options
  • Save proguy914629bot/96e37ac71d3a35d754fa07c87d65adde to your computer and use it in GitHub Desktop.
Save proguy914629bot/96e37ac71d3a35d754fa07c87d65adde to your computer and use it in GitHub Desktop.

This guide tells you how to make a simple NQN system for your discord.py bot.

This guide will tell you how to make a very simple NQN (Not Quite Nitro) for your discord.py bot with 3 simple steps.

What is Not Quite Nitro a.k.a NQN?

NQN stands for Not Quite Nitro. It is simply where a bot (which has powers to use external emojis in any server it's in) replaces a message for you where you have emojis that you want to use, but you can't, cause you don't have nitro! If you want to make one very simple NQN for your own discord.py bot, then you have came in the right place!

Please, please understand what this does first before putting this in your code.

Guide:

We can combine NQN with utils.find/utils.get to get emojis, then send them either by webhooks or the bot response itself.

Methods/Attributes Documentation Link
discord.utils.get discord.utils.get(bot.emojis, name = Emoji-Name)
discord.utils.find discord.utils.find(lambda e: Emoji-Name.lower() in e.name.lower(), bot.emojis)

utils.find is to get case insensitive emojis if utils.get fails.

Optionally, you may use EmojiConverter to get the emoji.

Code:

Code is below. Again, Please, please understand what this does first before putting this in your code.

First step:

First, we are going to make something like a get_emoji function to get the emoji, this is where we use the utils.get and utils.find.

# "emoji" param will contain the emoji name provided by the user e.g `:myemoji:`.
# The function below may return None (`?tag get none`), PartialEmoji, or Emoji.
def get_emoji(emoji: str) -> Union[discord.Emoji, discord.PartialEmoji, None]:
    if (emoji.startswith(":")) and (emoji.endswith(":")) and (len(emoji) != 2):
        emoji = emoji[1:-1] # Deletes the ":" in ":myemoji:" to "myemoji" so utils.get/find can work well. 
        
    emoji = discord.utils.get(bot.emojis, name = emoji)
    if not emoji:
        emoji = discord.utils.find(lambda e: emoji.lower() in e.name.lower(), bot.emojis)
        
    return emoji

Second Step:

After we have made our get_emoji function, we need to parse the emojis in the message content and call get_emoji, lets call this function, parse_message.

# "string" param will contain the whole message provided by the user e.g `At least :youtried:`.
# The function below will return a string with the parsed emoji.
# We will use `get_emoji`, check if it is None. If it isn't, turn it in a string to get the unicode of the emoji.
def parse_message(content: str) -> str:
    emojis = re.findall(r":(?P<name>[a-zA-Z0-9_]{2,32}):", content) # Gets all the `:myemoji:` for example and puts them in a list.
    if len(emojis) == 0:
        return content # No emojis found.
    
    for emoji in emojis:
        if emoji:
            result = get_emoji(emoji)
            if result is not None:
                content = content.replace(f":{emoji}:", str(result))
            
    return content

Third Step:

Finally, combining this with an on_message event/listener.

async def on_message(message: discord.Message):
    if message.author.bot:
        return # Prevents bots from using the NQN.
        
    if ":" in message.content:
        new_content = parse_message(message.content) # Parse the message content
        if new_content == message.content:
            return # It didn't parse, so we don't need to duplicate the message content itself.
            
        try:
            if not message.guild.default_role.permissions.use_external_emojis: # Check if webhooks have perms to use external emojis (webhooks follow the `@everyone` role permissions)
                return await message.channel.send(new_content)
                
            webhooks = await message.channel.webhooks() # Gets the channel webhooks.
            webhook = discord.utils.get(webhooks, name = f"NQN") # Gets the webhook in that channel named "NQN" that the bot uses.
            if webhook is None:
                webhook = await message.channel.create_webhook(name = f"NQN") # Creates that webhook because it doesn't exist in that channel.
                
            await webhook.send(new_content, username = message.author.name, avatar_url = message.author.avatar_url) # Send the message with the emojis.
            
            try:
                await message.delete() # Try to deletes the message since the webhook replaces it.
            except (discord.Forbidden, discord.HTTPException, discord.NotFound):
                pass
        except (discord.Forbidden, discord.HTTPException):
            await message.channel.send(new_content) # Send the message cause something bad happend when doing the webhook stuff.

And that finishes our NQN. If you do think this is a good gist and this helps you, please go give it a star (I am definitely not begging for a star.)

You can make this more complex if you want, such as a blacklisting system, toggle system, or an emoji finder, etc.

Documentation:

Method/Attributes Documentation Link
discord.utils.get Click Here
discord.utils.find Click Here
TextChannel.webhooks Click Here
re.findall Click Here
on_message Click Here
str.startswith Click Here
str.endswith Click Here
str.replace Click Here
discord.Forbidden Click Here
discord.HTTPException Click Here
discord.NotFound Click Here
discord.Message Click Here
discord.Emoji Click Here
discord.PartialEmoji Click Here
typing.Union Click Here

I really don't know why I had gone too detail on this...

Feedback/Issues:

I am always open for feedbacks and issues. If you have some, feel free to comment below and I will try to reply/fix things.

@proguy914629bot
Copy link
Author

if result is not None:
#you can change this to
if result is not None and result.animated==True:
#it only needs to change nitro emojis ,other emotes should remain the same

@sireeshdevaraj,

  1. Users that dont have access to that emoji and want to use it (does not have nitro to do emojis in other server or the user isnt in the server) can't use it.
  2. This code is mostly outdated. If you want, just fork this gist and do what you want to do with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment