Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V5 PR #21

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add log controller, start work on log levels #13, add ability to swit…
…ch between pokes and messages #16, add stuff that needs documented #20
  • Loading branch information
Brandyn committed Feb 25, 2024
commit 90461b915ff81776ae9bb997d26a9d2a897e0826
9 changes: 7 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -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);
});
}

6 changes: 5 additions & 1 deletion src/actions/discordComamnds/sync.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ import db from "../../database.js";
import synchroniseUser from "../synchronizeUser.js";
import ts from "../../teamspeak.js";
import { SlashCommandBuilder, PermissionsBitField } from "discord.js";
import log from "../../log.js";

class SyncCommand {
constructor() {
@@ -38,7 +39,10 @@ class SyncCommand {
try {
client = await ts.client.clientGetDbidFromUid(tsid);
} catch (ex) {
console.log(ex);
log.error("Error executing sync command $exception", {
discordName: member.username,
exception: ex,
});
}

if (client) {
2 changes: 0 additions & 2 deletions src/actions/discordComamnds/syncRole.js
Original file line number Diff line number Diff line change
@@ -35,7 +35,6 @@ class SyncRoleCommand {
}

let synced = await db.getSynchronizedRoles();
console.log(synced);
if (synced === undefined) {
synced = [];
}
@@ -44,7 +43,6 @@ class SyncRoleCommand {
await db.addSynchronizedRoles(role.name, tsGroup);

synced = await db.getSynchronizedRoles();
console.log(synced);
let syncedString = "Syncing: ";
synced.forEach((r, index) => {
syncedString += r.discord + " -> " + r.teamspeak;
87 changes: 73 additions & 14 deletions src/actions/synchronizeUser.js
Original file line number Diff line number Diff line change
@@ -2,11 +2,15 @@ import ts from "../teamspeak.js";
import db from "../database.js";
import _ from "lodash";
import config from "../config.js";
import log from "../log.js";

// TODO: MOVE TS CLIENT CODE TO TEAMSPEAK.JS
async function getTeamspeakRoles(cldbID, roles) {
let tsRoles = await ts.client.serverGroupsByClientId(cldbID).catch((ex) => {
console.log("cldbid ", cldbID, ex);
log.error("Could not get TS client information $tsClientId $exception", {
tsClientId: cldbid,
exception: ex,
});
});
let roleNames = [];
let rolesKeyedByTeamspeak = _.keyBy(roles, "teamspeak");
@@ -49,7 +53,6 @@ async function getMissingRoles(discordRoles, teamspeakRoles) {
discordRoles,
teamspeakRoles
);
console.log("missing roles executed");
let missing = _.difference(masterRoles, secondaryRoles);
return missing;
}
@@ -67,39 +70,88 @@ async function addTeamspeakRole(cldbID, role) {
let group = await ts.client
.getServerGroupByName(role.teamspeak)
.catch((ex) => {
console.log("cldbid ", cldbID, "role ", role, ex);
log.error(
"Could get group to update user TS roles! $clientId $role $exception",
{
clientId: cldbID,
role: role,
exception: ex,
}
);
});
if (!group) return console.log("Group ", role, "not found!");
if (!group)
return log.error(
"Failed to add client to group! Group not found! $clientId $role",
{
clientId: cldbID,
role: role,
exception: ex,
}
);
await ts.client.serverGroupAddClient(cldbID, group).catch((ex) => {
console.log("cldbID ", cldbID, "role ", role, ex);
log.error(
"Could not add user to teamspeak group $clientId $role $exception",
{
clientId: cldbID,
role: role,
exception: ex,
}
);
});
}

async function removeTeamspeakRole(cldbId, role) {
let group = await ts.client
.getServerGroupByName(role.teamspeak)
.catch((ex) => {
console.log("cldbid ", cldbID, "role ", role, ex);
log.error(
"Could get group to update user TS roles! $clientId $role $exception",
{
clientId: cldbID,
role: role,
exception: ex,
}
);
});
if (!group) return console.log("Group ", role, "not found!");
if (!group)
return log.error(
"Failed to add client to group! Group not found! $clientId $role",
{
clientId: cldbID,
role: role,
exception: ex,
}
);
await ts.client.serverGroupDelClient(cldbId, group).catch((ex) => {
console.log("cldbId ", cldbId, "role ", role, ex);
log.error(
"Could not remove user from teamspeak group $clientId $role $exception",
{
clientId: cldbID,
role: role,
exception: ex,
}
);
});
}

// TODO: IMPLMENT DISCORD CLIENT CODE IN DISCORD.JS & THIS FUNCTION
async function addDiscordRole(member, role) {
log.error("Discord Role Management Not Implemented");
throw new Error("Not implemented");
}

// TODO: IMPLMENT DISCORD CLIENT CODE IN DISCORD.JS & THIS FUNCTION
async function removeDiscordRole(member, role) {
log.error("Discord Role Management Not Implemented");
throw new Error("Not implemented");
}

async function sync(tsID, member) {
try {
console.log("syncing", tsID, member.user.name);
log.info("Beginning Sychronisation of User $discordName $tsID", {
discordName: member.user.username,
tsID: tsID,
});
let teamspeakId = (await ts.client.clientGetDbidFromUid(tsID)).cldbid;
let syncedRoles = await db.getSynchronizedRoles();
let RolesUserHasDiscord = await getDiscordRoles(member, syncedRoles);
@@ -117,24 +169,31 @@ async function sync(tsID, member) {
RolesUserHasTeamspeak
);

console.log("Roles User Has Discord: ", RolesUserHasDiscord);
console.log("Roles User Has Teamspeak: ", RolesUserHasTeamspeak);
log.debug("Roles User Has Discord: $RolesUserHasDiscord", {
RolesUserHasDiscord,
});
log.debug("Roles User Has Teamspeak: $RolesUserHasTeamspeak", {
RolesUserHasTeamspeak,
});

console.log("Missing Roles: ", missingRoles);
log.debug("Missing Roles: $missingRoles", { missingRoles });
for (const role of missingRoles) {
config.bot.master === "discord"
? addTeamspeakRole(teamspeakId, role)
: addDiscordRole(member, role);
}

console.log("Extra Roles: ", extraRoles);
log.debug("Extra Roles: $extraRoles", { extraRoles });
for (let role of extraRoles) {
config.bot.master === "discord"
? removeTeamspeakRole(teamspeakId, role)
: removeDiscordRole(member, role);
}
} catch (ex) {
console.log("Error synchronising user: " + member.username, ex);
log.error("Error synchronising user: $discordName $exception", {
discordName: member.username,
exception: ex,
});
}
}

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