Connect Discord Bot on discord.js to site on Glitch

Hi, guys! Please, help me connecting my Discord bot to site on Glitch.

I don’t know what you mean, can you please elaborate?

How to display bot statistics on a site created on Glitch?

If you mean amount of Servers your bot is in you can have a API with endpoints like
/api/getamountofguilds and use express and discord.js and return the amount of guilds.

Edit: And then use AJAX on your website to get that amount.

Can I get an example code of this miracle?

I need about the same as on this site: https://discordyui.net/

Node application:

const Discord = require("discord.js")
const Express = require("express")
const App = Express()
const Client = new Discord.Client()

App.get("/api/getguilds", (req, res) => {
  res.send(Client.guilds.cache.array().length.toString())
})

App.listen(process.env.PORT)

Client.login("token")

HTML File:

<!DOCTYPE HTML>
<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <p id="Guilds"></p>
    <script>
       const Request = new XMLHttpRequest()
       Request.onreadystatechange = function() {
         if (this.readyState == 4 && this.status == 200) {
           document.getElementById("Guilds").innerHTML = this.responseText;
         }
       };
       Request.open("GET", "/api/getguilds", true)
       Request.send()
    </script>
  </body>
</html>
2 Likes

Thanks for the code. And if the site and the bot on different projects will work?

The Node application must be on the bots project and when the URL you GET to must be a full url (e.g https://bot.glitch.me/api/getguilds)

Edit: AJAX requires same origin

That is, it works, but instead of writing the path to the link to the project, right?

Request.open(“GET”, “https://url.bot/api/getguilds”, true)
Request.send()

I forgot you need same origin for AJAX you must create a API on the websites node application to request from the bots project

Edit: Why don’t u have them both in the same project?

Is it possible to put both a bot and a site in one project?

Edit: I’m used hello-sqlite