Skip to content

Commit

Permalink
Add log controller, start work on log levels #13, add ability to swit…
Browse files Browse the repository at this point in the history
…ch between pokes and messages #16, add stuff that needs documented #20
  • Loading branch information
Brandyn committed Feb 20, 2024
1 parent 507358d commit 87cf879
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 67 deletions.
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
});
}

Expand Down
16 changes: 11 additions & 5 deletions src/actions/teamspeakClientConnected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/database.js
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -148,7 +148,7 @@ class Database {
VALUES ('${discordID}','${tsID}')`,
(err) => {
if (err) {
console.log(err);
log.error(err);
reject(err);
}
resolve();
Expand Down
49 changes: 25 additions & 24 deletions src/discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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 }
);
}
}
Expand All @@ -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();
});
Expand All @@ -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);
});
});
}

Expand All @@ -103,7 +104,7 @@ class Discord {
{ body: devJsonCommands }
);

console.log(
log.system(
`Successfully reloaded ${data.length} dev application (/) commands.`
);

Expand All @@ -112,7 +113,7 @@ class Discord {
{ body: jsonCommands }
);

console.log(
log.debug(
`Successfully reloaded ${data2.length} public application (/) commands.`
);
}
Expand Down
66 changes: 37 additions & 29 deletions src/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`
);
});

Expand All @@ -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;
Expand All @@ -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: {
Expand All @@ -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}`);
})
);

Expand All @@ -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) {
Expand All @@ -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(
Expand All @@ -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}. <br /> If you have problems with synchronization send the line above to high command!`
Expand All @@ -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();
81 changes: 81 additions & 0 deletions src/log.js
Original file line number Diff line number Diff line change
@@ -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());
Loading

0 comments on commit 87cf879

Please sign in to comment.