SuspiciousLookingOwl / saweria-api

Saweria API and WebSocket wrapper for Node.js
https://npmjs.com/package/saweria
41 stars 1 forks source link

Discord Integration #7

Closed FiqriAgwar closed 2 years ago

FiqriAgwar commented 2 years ago

Hello. I hope this is still usable especially in Discord, since I saw this package has a good potential.

I want to make a Discord Bot that can alert the guild whenever there is a donation (I know there is a webhook available in saweria.co, but I want to upgrade it with the existing system in the bot).

However, when I tried to log in there is an error like this:

node:events:518
    throw err; // Unhandled 'error' event
    ^

Error [ERR_UNHANDLED_ERROR]: Unhandled error. (Event { type: 'error', message: 'read ECONNRESET' })
    at new NodeError (node:internal/errors:372:5)
    at SaweriaClient.emit (node:events:516:17)
    at EventSource.<anonymous> (E:\DiscordBot\node_modules\saweria\dist\Client.js:120:35)
    at EventSource.emit (node:events:527:28)
    at _emit (E:\DiscordBot\node_modules\eventsource\lib\eventsource.js:257:17)
    at onConnectionClosed (E:\DiscordBot\node_modules\eventsource\lib\eventsource.js:53:5)
    at ClientRequest.<anonymous> (E:\DiscordBot\node_modules\eventsource\lib\eventsource.js:246:7)
    at ClientRequest.emit (node:events:527:28)
    at TLSSocket.socketErrorListener (node:_http_client:442:9)
    at TLSSocket.emit (node:events:527:28) {
  code: 'ERR_UNHANDLED_ERROR',
  context: Event { type: 'error', message: 'read ECONNRESET' }

This is the code from index.js

const { Client, Collection } = require("discord.js");
const { Token, Saweria } = require("./config.json");
const { promisify } = require("util");
const { glob } = require("glob");
const PG = promisify(glob);
const Ascii = require("ascii-table");
const SaweriaClient = require("saweria");

const client = new Client({ intents: 32767 });
client.commands = new Collection();

const saweriaClient = new SaweriaClient();
saweriaClient.on("login", (user) => {
  console.log("Logged in as: ", user.username);
});

saweriaClient.on("donations", (donations) => {
  console.log(donations);
});

saweriaClient.login(Saweria.Email, Saweria.Password);

["Events", "Commands"].forEach((handler) => {
  require(`./Handlers/${handler}`)(client, PG, Ascii);
});
client.login(Token);

So, I really want to know if there is a mistake in using your package. Thank you in advance.

SuspiciousLookingOwl commented 2 years ago

Unfortunately it's currently not possible to use this library on server side environment / node, as Saweria's API is protected with Cloudflare's Browser Integrity Check.

Read more here.