diff --git a/index.js b/index.js index e5142c8..a027e2e 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ import discord from "./src/discord.js"; import _ from "lodash"; import teamspeakClientConnected from "./src/actions/teamspeakClientConnected.js"; import synchroniseUser from "./src/actions/synchronizeUser.js"; +import log from "./src/log.js"; // Initialize Connections to Servers async function initialize() { @@ -17,8 +18,12 @@ async function initialize() { }); discord.client.on("guildMemberUpdate", (member) => { - console.log("Guild Member Update"); - synchroniseUser(member.guild.members.cache.get(member.id)); + log.info("Discord User Updated - $discordName - $discordId", { + discordName: member.user.username, + discordId: member.id, + }); + r; + synchroniseUser(member); }); } diff --git a/src/actions/teamspeakClientConnected.js b/src/actions/teamspeakClientConnected.js index 7fae9a0..1591186 100644 --- a/src/actions/teamspeakClientConnected.js +++ b/src/actions/teamspeakClientConnected.js @@ -5,6 +5,12 @@ import db from "../database.js"; import synchronizeUser from "./synchronizeUser.js"; import discord from "../discord.js"; +function contactUser(tsid, message) { + config.teamspeak.usePokes + ? teamspeak.sendPokeToClient(tsid, message) + : teamspeak.sendMessageToClient(tsid, message); +} + export default async function (event) { return new Promise(async (resolve, reject) => { var tsid = event.client.propcache.clientUniqueIdentifier; @@ -18,25 +24,25 @@ export default async function (event) { } } else { if (!config.discord.useOAuth) { - await teamspeak.sendMessageToClient( + await contactUser( event.client, config.teamspeak.welcomeMessageText ? config.teamspeak.welcomeMessageText : "Hello! You seem to be new here. Please connect your discord account by logging in with the link below." ); - await teamspeak.sendMessageToClient( + await contactUser( event.client, `Please use the /register command in the ${config.discord.commandChannelName} channel in discord with the following teamspeak-id` ); - await teamspeak.sendMessageToClient(event.client, `${tsid}`); + await contactUser(event.client, `${tsid}`); } else { - await teamspeak.sendMessageToClient( + await contactUser( event.client, config.teamspeak.welcomeMessageText ? config.teamspeak.welcomeMessageText : "Hello! You seem to be new here. Please connect your discord account by logging in with the link below." ); - await teamspeak.sendMessageToClient( + await contactUser( event.client, `${ config.web.clientBaseUrl diff --git a/src/database.js b/src/database.js index 3b180b7..b83509f 100644 --- a/src/database.js +++ b/src/database.js @@ -1,6 +1,6 @@ import config from "./config.js"; import sqlite3 from "sqlite3"; - +import log from "./log.js"; class Database { constructor() { this.db = new sqlite3.Database( @@ -148,7 +148,7 @@ class Database { VALUES ('${discordID}','${tsID}')`, (err) => { if (err) { - console.log(err); + log.error(err); reject(err); } resolve(); diff --git a/src/discord.js b/src/discord.js index 72169ed..c91f438 100644 --- a/src/discord.js +++ b/src/discord.js @@ -8,7 +8,7 @@ import { Routes, } from "discord.js"; import commands from "./actions/discordComamnds/index.js"; - +import log from "./log.js"; class Discord { constructor() { this.client = new Client({ @@ -25,8 +25,9 @@ class Discord { if ("data" in command && "execute" in command) { this.client.commands.set(command.data.name, command); } else { - console.log( - `[WARNING] The command ${command} is missing a required "data" or "execute" property.` + log.debug( + `[WARNING] The command $command is missing a required "data" or "execute" property.`, + { command: command } ); } } @@ -37,7 +38,7 @@ class Discord { this.client.login(config.discord.token); this.client.on("ready", () => { this.client.intents; - console.log("Discord Bot Connected"); + log.system("Discord Bot Connected"); this.regsiterCommands(); resolve(); }); @@ -58,29 +59,29 @@ class Discord { } }); - // this.client.on(Events.Debug, (message) => { - // console.log(message); - // }); + this.client.on(Events.Debug, (message) => { + log.debug(message); + }); - // this.client.on(Events.Error, (message) => { - // console.log(message); - // }); + this.client.on(Events.Error, (message) => { + log.error(message); + }); - // this.client.on(Events.Warn, (message) => { - // console.log(message); - // }); + this.client.on(Events.Warn, (message) => { + log.debug(message); + }); - // this.client.on(Events.GuildUnavailable, (message) => { - // console.log(message); - // }); + this.client.on(Events.GuildUnavailable, (message) => { + log.error(message); + }); - // this.client.on(Events.GuildRoleUpdate, (message) => { - // console.log(message); - // }); + this.client.on(Events.GuildRoleUpdate, (message) => { + log.verbose(message); + }); - // this.client.on(Events.Raw, (message) => { - // console.log(message); - // }); + this.client.on(Events.Raw, (message) => { + log.verbose(message); + }); }); } @@ -103,7 +104,7 @@ class Discord { { body: devJsonCommands } ); - console.log( + log.system( `Successfully reloaded ${data.length} dev application (/) commands.` ); @@ -112,7 +113,7 @@ class Discord { { body: jsonCommands } ); - console.log( + log.debug( `Successfully reloaded ${data2.length} public application (/) commands.` ); } diff --git a/src/express.js b/src/express.js index 3c4899f..e0dc569 100644 --- a/src/express.js +++ b/src/express.js @@ -8,8 +8,15 @@ import db from "./database.js"; import discord from "./discord.js"; import synchronizeUser from "./actions/synchronizeUser.js"; import config from "./config.js"; +import log from "./log.js"; class Web { + catchAsync = (fn) => (req, res, next) => { + const routePromise = fn(req, res, next); + if (routePromise.catch) { + routePromise.catch((err) => next(err)); + } + }; constructor() { this.app = express(); this.port = config.web.port; @@ -23,8 +30,8 @@ class Web { if (!req.query.tsid) throw new Error("NoTSIDProvided"); const tsid = req.query.tsid; res.cookie("tsid", tsid, { maxage: 3000 }); - res.this.redirect( - `https://discordthis.app.com/api/oauth2/authorize?this.CLIENT_ID=${this.CLIENT_ID}&scope=identify&response_type=code&this.redirect_uri=${this.redirect}` + res.redirect( + `https://discordapp.com/api/oauth2/authorize?client_id=${this.CLIENT_ID}&scope=identify&response_type=code&redirect_uri=${this.redirect}` ); }); @@ -33,7 +40,7 @@ class Web { }); this.app.get( - "/this.redirect", + "/redirect", this.catchAsync(async (req, res) => { if (!req.query.code) throw new Error("NoCodeProvided"); const code = req.query.code; @@ -45,7 +52,7 @@ class Web { }); // debugger; const response = await fetch( - `https://discordthis.app.com/api/oauth2/token`, + `https://discordapp.com/api/oauth2/token`, { method: "POST", headers: { @@ -56,7 +63,7 @@ class Web { } ); const json = await response.json(); - res.this.redirect(`/token?token=${json.access_token}`); + res.redirect(`/token?token=${json.access_token}`); }) ); @@ -65,15 +72,12 @@ class Web { this.catchAsync(async (req, res) => { if (!req.query.token) throw new Error("NoTokenProvided"); const token = req.query.token; - const response = await fetch( - "https://discordthis.app.com/api/users/@me", - { - method: "GET", - headers: { - Authorization: `Bearer ${token}`, - }, - } - ); + const response = await fetch("https://discordapp.com/api/users/@me", { + method: "GET", + headers: { + Authorization: `Bearer ${token}`, + }, + }); const json = await response.json(); if (req.cookies.tsid && json.id) { @@ -84,9 +88,9 @@ class Web { guild.members .fetch({ withPresences: true }) .then((fetchedMembers) => { - const totalOnline = fetchedMembers.filter( - (member) => member.presence?.status === "online" - ); + // const totalOnline = fetchedMembers.filter( + // (member) => member.presence?.status === "online" + // ); member = fetchedMembers.get(json.id); if (member) { synchronizeUser( @@ -95,15 +99,26 @@ class Web { .members.cache.get(json.id), tsid ); - } else console.log("The user didn't exist>!?!?"); - // Now you have a collection with all online member objects in the totalOnline variable + } else + log.error( + "Could not register from OAuth discord ID did not exist in the discord server - $discordId - $tsid", + { + tsid: req.cookies.tsid, + discordId: json.id, + } + ); }); } else { - console.log("Missing an ID, ", req.cookies.tsid, json.id); + log.error( + "Could not register from OAuth did not recieve tsid - $discordId", + { + discordId: json.id, + } + ); } if (config.web.clientBaseUrl) { - res.this.redirect(config.web.clientBaseUrl); + res.redirect(config.web.clientBaseUrl); } else { res.send( `Hello ${json.username} your Discord ID is ${json.id}, you Teamspeak ID is ${req.cookies.tsid}.
If you have problems with synchronization send the line above to high command!` @@ -120,16 +135,9 @@ class Web { ); this.app.listen(this.port, () => { - console.log(`OAuth Login listening at http://localhost:${this.port}`); + log.system(`OAuth Login listening at http://localhost:${this.port}`); }); } - - catchAsync = (fn) => (req, res, next) => { - const routePromise = fn(req, res, next); - if (routePromise.catch) { - routePromise.catch((err) => next(err)); - } - }; } export default new Web(); diff --git a/src/log.js b/src/log.js new file mode 100644 index 0000000..c7d75a9 --- /dev/null +++ b/src/log.js @@ -0,0 +1,81 @@ +import config from "./config.js"; +import _ from "lodash"; +import fs from "fs"; + +class log { + LOG_LEVEL = { + SYSTEM: 0, + ERROR: 1, + WARN: 3, + INFO: 7, + DEBUG: 15, + VERBOSE: 31, + }; + + LOG_STR = _.invert(this.LOG_LEVEL); + + constructor() { + this.log = console.log; + } + + addLog(level, message, data) { + if (config.logLevel & level) return; + this.logConsole(level, message, data); + } + + getTimeStamp() { + return new Date().toISOString(); + } + + compileMessage(message, data) { + if (data) { + _.keysIn(data).forEach((key) => { + let data = + data[key] instanceof Object ? JSON.stringify(data[key]) : data[key]; + message = message.replace(`\$${key}`, `${key}: ${data}`); + }); + } + return message; + } + + createLogString(level, message, data) { + return `${this.getTimeStamp()} - [${ + this.LOG_STR[level] + }] - ${this.compileMessage(message, data)}`; + } + + logConsole(level, message, data) { + console.log(this.createLogString(level, message, data)); + } + + logFile(level, message, data) { + if (config.logging.file === undefined) return; + fs.appendFileSync( + config.logging.file, + this.createLogString(level, message, data), + data + ); + } + + system(message, data) { + this.addLog(this.LOG_LEVEL.SYSTEM, message, data); + } + + error(message, data) { + this.addLog(this.LOG_LEVEL.ERROR, message, data); + } + + info(message, data) { + this.addLog(this.LOG_LEVEL.INFO, message, data); + } + + debug(message, data) { + this.addLog(this.LOG_LEVEL.DEBUG, message, data); + } + + verbose(message, data) { + this.addLog(this.LOG_LEVEL.VERBOSE, message, data); + } +} + +export default Object.freeze(new log()); diff --git a/src/teamspeak.js b/src/teamspeak.js index ed1b4a8..8f0c7ef 100644 --- a/src/teamspeak.js +++ b/src/teamspeak.js @@ -1,5 +1,6 @@ import { TeamSpeak, TextMessageTargetMode } from "ts3-nodejs-library"; import config from "./config.js"; +import log from "./log.js"; /* TODO: FIX THIS ON SERVER EDIT @@ -27,16 +28,16 @@ class TS { }); this.client.on("error", (e) => { - console.log("error", e); + log.error("error", e); }); this.client.on("ready", () => { - console.log("Teamspeak Bot Connected"); + log.system("Teamspeak Bot Connected"); if (config.teamspeak.virtualServerId === undefined) { - console.log( + log.error( "No virtual server id specified, please choose one in the config file from the list below" ); - console.log(this.client.serverList()); + log.error(this.client.serverList()); } this.client.useBySid(config.teamspeak.virtualServerId); @@ -65,13 +66,26 @@ class TS { }); } + async sendPokeToClient(client, message) { + return new Promise((resolve) => { + if (client.propcache.clientType === 0) { + this.client.sendPokeToClient( + client, + TextMessageTargetMode.CLIENT, + message + ); + } + resolve(); + }); + } + async getGroupByName(name) { return new Promise(async (resolve, reject) => { try { let group = await this.client.getServerGroupByName(name); resolve(group); } catch (ex) { - console.log("Could not lookup group!", ex); + log.error("Could not lookup group!", ex); reject(ex); } });