This guide will tell you how to make a very simple NQN (Not Quite Nitro) for your discord.py bot with 3 simple steps.
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!
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 is below. Again, Please, please understand what this does first before putting this in your code.
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
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
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.
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...
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.