Dblapi.js https request fail Discord Bot

https://usagix.glitch.me

So, I was trying to get vote updates from https://discordbots.org/ using webhooks so my code was trying with this

const app = express();

app.get("/", (request, response) => {
  console.log("Ping received!");
  response.sendStatus(200);
});

// listen for requests :)
const listener = app.listen(process.env.PORT, function() {
  console.log('Your app is listening on port ' + listener.address().port);
});
const DBL = require('dblapi.js');

const http = require('http');


const server = http.createServer(app);
const dbl = new DBL(process.env.DBLTOKEN, { webhookAuth: process.env.DBLAUTH, webhookServer: server });

dbl.webhook.on('ready', hook => {
  console.log(`Webhook running with path ${hook.path}`);
});
dbl.webhook.on('vote', vote => {
  console.log(`User with ID ${vote.user} just voted!`);
});



server.listen(5000, () => {
  console.log('Listening');
});```

Your project can only listen on a single port, but you’re trying to use two in your code. You could define an additional handler in your webhook code that whatever service you’re using to keep the bot awake can ping instead of the Express route you’ve created.

I am using uptimerobot. How would I do that?

Can you send me the exact error that you get? Also, does the console logging at the bottom work?

events.js:183

10:25 PM

throw er; // Unhandled ‘error’ event

10:25 PM

^

10:25 PM

10:25 PM

Error: listen EADDRINUSE :::5000

10:25 PM

at Server.setupListenHandle [as _listen2] (net.js:1360:14)

10:25 PM

at listenInCluster (net.js:1401:12)

10:25 PM

at Server.listen (net.js:1485:7)

10:25 PM

Jump to

at Object.<anonymous> (/app/server.js:27:8)

10:25 PM

at Module._compile (module.js:653:30)

10:25 PM

at Object.Module._extensions…js (module.js:664:10)

10:25 PM

at Module.load (module.js:566:32)

10:25 PM

at tryModuleLoad (module.js:506:12)

10:25 PM

at Function.Module._load (module.js:498:3)

10:25 PM

at Module.require (module.js:597:17)

10:25 PM

a few seconds ago

:couplekiss_man_man::crown: Your app is listening on port 3000

10:25 PM

Webhook running at http://0.0.0.0:5000/dblwebhook

10:25 PM

events.js:183

10:25 PM

throw er; // Unhandled ‘error’ event

10:25 PM

^

10:25 PM

10:25 PM

Error: listen EADDRINUSE :::5000

10:25 PM

at Server.setupListenHandle [as _listen2] (net.js:1360:14)

10:25 PM

at listenInCluster (net.js:1401:12)

10:25 PM

at Server.listen (net.js:1485:7)

10:25 PM

Jump to

at Object.<anonymous> (/app/server.js:27:8)

10:25 PM

at Module._compile (module.js:653:30)

10:25 PM

at Object.Module._extensions…js (module.js:664:10)

10:25 PM

at Module.load (module.js:566:32)

10:25 PM

at tryModuleLoad (module.js:506:12)

10:25 PM

at Function.Module._load (module.js:498:3)

10:25 PM

at Module.require (module.js:597:17)

10:25 PM

Remove this code and change webhookServer: server to webhookServer: listener

I know this is a little old thread, but I have followed when you have said and I keep getting this error HelpChat Paste
This is the code I use HelpChat Paste

It looks like the DBLAPI is making the ‘webhook’ listen on port 5000. Since Glitch projects only allow port 3000 to be exposed, it could be an issue. Also, it looks like you already have a server listening on port 3000. E.G an express/http server, etc.

I’d suggest removing whatever HTTP/express server is listening on port 3000 if not, then I’d suggest trying to use a reverse proxy.

Hey @GabyTM I think what’s probably going on here is that you have Express setting up a listener on the port represented by process.env.PORT and you’re also setting up a server that’s listening to the same port, so you’re basically saying “hey app, listen over here on this port, but also listen over here on the same port” which will result in the error you’re seeing.

I talked about this in some detail in Page cant be found, but in essences you should be able to use

const server = http.createServer(app);

and tell your DBL object to use that server, but you need to make sure that when you start up your Express app you also use server.listen() there instead of app.listen() and that should stop the “Address already in use” error.

This is my code but I don’t get the vote message in console.
I use http://gdt.glitch.me:0/dblwebhook as webhook url on DBL.

The URL should be https://gdt.glitch.me/dblwebhook

image
Simply changing webhookServer: server to webhookServer: listener should fix the problem

Tried your and @Callum-OKane 's suggestion but I still don’t get anything on console. =/

Hey @GabyTM I just took a quick peek and it seems like you’re getting the console messages you’re expecting now; is everything working as you’d like?

1 Like

Yea, I just took a look at the console and works, ty <3

1 Like