Learn to Code via Tutorials on Repl.it

← Back to all posts
How to Make a Discord Bot That Isn't a Bot
minx28 (144)

Hello there!
I just posted to the Share board, explaining my concept of a "discord bot that isn't a bot". Essentially, it's a website which exploits the way discord shows a sort-of preview of some websites when a link to them is posted.

The website I made is blank when viewed in a browser, but when a link is posted in discord, it can display whatever content I want, responding to any text after the base URL. For example, if you posted https://discord.minx28.repl.co/calculate/1+3, a box would appear underneath showing 4. The best thing about this is that it can be used by anyone on any server, without inviting any bot. Here's a tutorial on how to make your own. We're going to be doing this in a Python3 repl, using flask to create a server and website, and WolframAlpha to make an advanced scientific calculator.

Firstly, we're going to create our flask server.

import flask



This code is fairly self-explanatory. When you run it, a new window will appear in the top-right corner, with a 404 Not Found error. This is because we haven't yet defined our homepage.

After line 2, add the following code:

def home():
    return "<h1>Hello World!</h1>"

If you run your code now, a large "Hello World" will display in that top-right panel. However, our finished website will not display any content at all.

A key part of this project is OGP, Open Graph Protocol. This is a protocol allowing websites to be displayed as the boxes discord shows, and is also in use on many other sites, like Facebook.
Create a folder called templates. Inside it, make a file called index.html, with the following code:

<html prefix="og: http://ogp.me/ns#">
    <meta property="og:title" content="">
    <meta property="og:type" content="">
    <meta property="og:url" content="">
    <meta property="og:image" content="">

The four meta tags in head are required properties for any page using OGP. However, since this isn't a normal website preview, we're not going to use any of these four. Underneath them, add <meta property="og:description" content="{{output}}">. The og:description property is the text we're going to be seeing, and {{output}} tells flask that this is a variable we're going to assign a value to.

Back in main.py, change return "Hello World!" to return flask.render_template("index.html",output="Hello World!") and run the code. The top-right panel should now be a blank page. In discord (or any other platform that uses OGP) send a message containing the URL of your server. You should see a box containing the words "Hello World!". flask.render_template is a function that allows us to take a file from the templates folder, and render the HTML with variables inside, and we then assign the variable output.

The next step it to create a WolframAlpha app and get an appid. First, you need to create an account here. When you're signed in, open the dropdown menu in the top-right corner and click My Apps (API). Click "Get an AppID".

Enter the name of your app and a description, and click Get AppID. You will then be shown your AppID. In your repl, make a file called .env and put appid=YOUR_APPID. Naming the file .env means that nobody else can see your AppID (the app in the photo below has been deactivated).

In main.py, add import os and import wolframalpha at the top. Next, after app=flask.Flask(""), add client=wolframalpha.Client(os.getenv("appid")). os.getenv("appid") is how we retrieve the AppID which we stored in .env.

Underneath the index() function, add the following code:

def calculate(calculation):
    return render_template("index.html",output=result)

The first line of that tells flask that we're creating a new route, which in this case is /<anything>. In <path:calculation>, the <> mean that we're passing on a variable called calculation, and path tells flask that calculation is a path rather than a string, meaning it can include /. We need this for division. If we just had <calculation>, this function would not be called for /9/3 for example.
Lines 3 and 4 are the code for WolframAlpha. result ends up storing the result of our calculation, as a string. Line 5 returns the template index.html with the variable output assigned.
You can now run this code and test it in discord by going to https://<replname>.<username>.repl.co/1+2, and after a second or two, a box should appear saying 3.
You can also change "Hello World" in the index() function to `"/[calculation]\nCalculates a mathematical expression" to tell people how to use your bot.

When you're creating your own bot, there is one major problem you might run into. If one of your functions doesn't require any variables, for example a timchen() function activated when you link to /timchen that gives you a random picture of timchen, you may find yourself a victim of discord caching. This means that if you post the same link, discord simply uses the previous preview and doesn't bother to check for a new one. Getting round this is easy: simply add a query (for example, ?discordcachingisannoying) at the end of the URL each time. As long as the query is different every time, discord won't recognise it as the same URL and will re-request the preview.

I hope you enjoyed this tutorial and found it useful! If you did, please upvote, and I'll send you some vee freebucks!

Oh and one final thing... this bot-but-not-a-bot works on Whatsapp!!

TheDrone7 (547)

Work pretty much everywhere except twitter. Use Twitter meta tags and works even there too.

minx28 (144)

@TheDrone7 I know OGP is uses on a lot of sites. I just thought whatsapp was a good one to mention since basically everyone uses it