Skip to content

Commit

Permalink
Fix Console Webhook Live Update
Browse files Browse the repository at this point in the history
  • Loading branch information
TecEash1 committed May 19, 2024
1 parent d92b1ab commit e8cb489
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
.vscode/
.idea/
.node-version
/functions/other/settings.sqlite
/personality.txt

# Logs
logs
Expand Down Expand Up @@ -108,4 +110,4 @@ dist
.dynamodb/

# TernJS port file
.tern-port
.tern-port
31 changes: 22 additions & 9 deletions functions/logConsole.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module.exports = (client) => {
const { WebhookClient, EmbedBuilder } = require("discord.js");
const { QuickDB } = require("quick.db");
const {
webhookUpdateEvent,
} = require("../interactions/modals/Settings/webhooks");
const path = require("path");
const db = new QuickDB({
filePath: path.join(__dirname, "./other/settings.sqlite"),
Expand All @@ -11,15 +14,25 @@ module.exports = (client) => {
webhookURL = webhookUrlConsoleLogs;

let webhookClient;
try {
webhookClient = new WebhookClient({ url: webhookURL });
} catch (error) {
console.log(
"\x1b[31m\x1b[1m%s\x1b[0m",
"CONSOLE LOGGING IN DISCORD DISABLED. SET WEBHOOK URL WITH /SETTINGS.",
);
return;
}
const setupWebhookClient = async () => {
const webhooks = await db.get("webhooks");
webhookURL = webhooks.console;

try {
webhookClient = new WebhookClient({ url: webhookURL });
} catch (error) {
console.log(
"\x1b[31m\x1b[1m%s\x1b[0m",
"CONSOLE LOGGING IN DISCORD DISABLED. SET WEBHOOK URL WITH /SETTINGS.",
);
}
};
setupWebhookClient();

webhookUpdateEvent.on("update", (newWebhookUrl) => {
webhookURL = newWebhookUrl;
setupWebhookClient();
});

function customLogger(type, ...messages) {
const combinedMessage = messages
Expand Down
4 changes: 4 additions & 0 deletions interactions/modals/Settings/webhooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
const fs = require("fs").promises;
const path = require("path");
const { EventEmitter } = require("events");
const webhookUpdateEvent = new EventEmitter();
const { EmbedBuilder, WebhookClient } = require("discord.js");
const { checkWebhook } = require("../../../functions/other/utils");
const { emojis } = require("../../../config.json");
Expand All @@ -18,6 +20,7 @@ const db = new QuickDB({

module.exports = {
id: ["personality", "console"],
webhookUpdateEvent,

async execute(interaction) {
const webhookName =
Expand Down Expand Up @@ -99,6 +102,7 @@ module.exports = {
webhooks.console = webhook;
embedData.data.fields[webhookFieldIndex].value =
`${personalityWebhookStatus}\n${isValidWebhook ? emojis.working : emojis.failed} **Console**`;
webhookUpdateEvent.emit("update", webhooks.console);
}

await db.set("webhooks", webhooks);
Expand Down

0 comments on commit e8cb489

Please sign in to comment.