From 5fe0190d10eadda6c11829f288596db6b2c1d34a Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Thu, 9 May 2024 22:28:13 -0600 Subject: [PATCH] Just testing some stuff --- debug.js => testing/debug.js | 2 +- testing/noaawire.js | 109 +++++++++++++++++++++++ voicetest.js => testing/voicetest.js | 2 +- wfo.js => testing/wfo.js | 2 +- xml2jsontmp.js => testing/xml2jsontmp.js | 0 5 files changed, 112 insertions(+), 3 deletions(-) rename debug.js => testing/debug.js (92%) create mode 100644 testing/noaawire.js rename voicetest.js => testing/voicetest.js (98%) rename wfo.js => testing/wfo.js (95%) rename xml2jsontmp.js => testing/xml2jsontmp.js (100%) diff --git a/debug.js b/testing/debug.js similarity index 92% rename from debug.js rename to testing/debug.js index 7b958be..5df9b2f 100644 --- a/debug.js +++ b/testing/debug.js @@ -1,6 +1,6 @@ const Discord = require('discord.js'); const client = new Discord.Client({intents: ["Guilds"]}) -const config = require('./config.json'); +const config = require('../config.json'); categories = ["1237806700879417416", "1237806701722337451", "1237806702473121853", "1237806703010123857"] diff --git a/testing/noaawire.js b/testing/noaawire.js new file mode 100644 index 0000000..5f019f1 --- /dev/null +++ b/testing/noaawire.js @@ -0,0 +1,109 @@ +const { client, xml } = require("@xmpp/client"); + +const generateRandomString = function (options, length) { + let result = ''; + const characters = { + upper: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', + lower: 'abcdefghijklmnopqrstuvwxyz', + number: '0123456789', + special: '!@#$%^&*()_+' + }; + let chars = ''; + for (const key in options) { + if (options[key]) { + chars += characters[key]; + } + } + for (let i = 0; i < length; i++) { + result += chars.charAt(Math.floor(Math.random() * chars.length)); + } + return result; +} + +// Func to generate UUID +const generateUUID = function () { + return generateRandomString({ lower: true, upper: true, number: true }, 8) + "-" + generateRandomString({ lower: true, upper: true, number: true }, 4) + "-" + generateRandomString({ lower: true, upper: true, number: true }, 4) + "-" + generateRandomString({ lower: true, upper: true, number: true }, 4) + "-" + generateRandomString({ lower: true, upper: true, number: true }, 12); +} +const colors = require("colors"); +//nwws@nwws-oi.weather.gov/nwws-oi +const xmpp = client({ + service: "xmpp://nwws-oi.weather.gov", + domain: "weather.gov", + resource: `discord-weather-bot-${generateRandomString({ upper: true, lower: true, number: true }, 5)}`, // Weird fix to "Username already in use" +}); + +//debug(xmpp, true); + +xmpp.on("error", (err) => { + console.log(`${colors.red("[ERROR]")} XMPP Error: ${err}. Trying to reconnect...`); + setTimeout(() => { + xmpp.stop().then(() => { + start(); + }); + }, 5000); +}); + +xmpp.on("offline", () => { + console.log(`${colors.yellow("[WARN]")} XMPP offline, trying to reconnect...`); + setTimeout(() => { + xmpp.stop().then(() => { + start(); + }); + }, 5000); +}); + +xmpp.on("stanza", (stanza) => { + if (config.debug >= 2) console.log(`${colors.magenta("[DEBUG]")} Stanza: ${stanza.toString()}`); + // Stops spam from getting old messages + if (startup) return; + // Get new messages and log them, ignore old messages + if (stanza.is("message") && stanza.attrs.type === "groupchat") { + const message = stanza.getChildText("body"); + const from = stanza.attrs.from; + console.log(`${colors.green("[MESSAGE]")} ${from}: ${message}`); + } +}); + +xmpp.on("online", async (address) => { + + errCount = 0; + // Start listening on all channels, (dont ban me funny man) + // for (const channel in iem.channels) { + // console.log(`Joining ${channel.name}`) + // await xmpp.send(xml("presence", { to: `${channel.jud}/${channel.name}` })); + // } + /* sub format + + + + visitor + + + + */ + // Join all channels + xmpp.send(xml("presence", { to: `nwws@nwws-oi.weather.gov/nwws-oi/${generateUUID()}` }, xml("item", { role: "visitor" }))); + + console.log(`${colors.cyan("[INFO]")} Connected to XMPP server as ${address.toString()}`); + + setTimeout(() => { + startup = false; + console.log(`${colors.cyan("[INFO]")} Startup complete, listening for messages...`); + }, 1000) +}); + +const start = () => { + xmpp.start().catch((err) => { + errCount++; + if (errCount >= 5) { + console.log(`${colors.red("[ERROR]")} XMPP failed to start after 5 attempts, exiting...`); + process.exit(1); + } + console.log(`${colors.red("[ERROR]")} XMPP failed to start: ${err}. Trying again in 5 seconds...`); + setTimeout(() => { + start(); + }, 5000); + }); +} + +start() \ No newline at end of file diff --git a/voicetest.js b/testing/voicetest.js similarity index 98% rename from voicetest.js rename to testing/voicetest.js index 653ca07..ebbab8f 100644 --- a/voicetest.js +++ b/testing/voicetest.js @@ -2,7 +2,7 @@ const Discord = require('discord.js'); const { Client, Intents } = require('discord.js'); const client = new Client({ intents: ["GuildVoiceStates", "Guilds"] }); -const config = require('./config.json'); +const config = require('../config.json'); const dVC = require('@discordjs/voice'); const { join } = require('path'); diff --git a/wfo.js b/testing/wfo.js similarity index 95% rename from wfo.js rename to testing/wfo.js index 163df97..02682d9 100644 --- a/wfo.js +++ b/testing/wfo.js @@ -3,7 +3,7 @@ const fs = require('fs'); const path = require('path'); -const wfos = require('./wfos.json'); +const wfos = require('../wfos.json'); // wfos is an object of objects diff --git a/xml2jsontmp.js b/testing/xml2jsontmp.js similarity index 100% rename from xml2jsontmp.js rename to testing/xml2jsontmp.js