Node.js Discord bot works in localhost, but not when deployed to firebase hosting

21 views Asked by At

The exact project works in localhost, but not when deployed to Firebase.

With postman, when I send post request to /new-sub in localhost with a body, the bot messages to a specific channel in a specific server. When I send the same request to the live url, I get 404 instead.

Why does it behave differently when deployed?

my code:

const { Client, GatewayIntentBits } = require('discord.js');
const express = require("express");
const app = express();
app.use(express.json());

const token = "xxx";

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages
    ]
});
client.login(token); 

const port = process.env.POST || 443;
app.listen(port, () => {
    console.log(`Project is running. Listening on port ${port}`);
});

app.get("/", (req, res) => {
    res.send(`hello world. Port: ${port}}`);
});

app.post("/new-sub", (req, res) => {
    const [body, query] = [req.body, req.query];


    const guildId = query.guildId || "1234";// server id
    const channelId = query.channelId || "5678";
    const guild = client.guilds.cache.get(guildId);
    if (!guild) {
        const errMsg = `<p>Guild ${guildId} not found</p>`;
        console.log(errMsg);
        res.send(errMsg);
        return;
    }

    const channel = guild.channels.cache.get(channelId);
    if (!channel) {
        const errMsg = `<p>Channel ${channelId} not found in ${guild.name} (${guildId})</p>`;
        console.log(errMsg);
        res.send(errMsg);
        return;
    }

    const newMessage = `New sub! Title: ${body.title}, time: ${new Date(Date.now())}`;
    console.log(newMessage);
    channel.send(newMessage);

    console.log(`message sent to ${channel.name} channel in ${guild.name}`);
    res.send(newMessage);
});

app.use(express.static('public'));
0

There are 0 answers