From 94207e693ba23a64229363f40ac42c4bb4a46ae9 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Sat, 7 Oct 2023 19:48:39 +1100 Subject: [PATCH] Add webhook for multifarm chat --- src/config.json | 3 ++- src/funcs/HookManager.ts | 5 +++- src/funcs/MPModule.ts | 50 ++++++++++++++++++++++++++++++++++++- src/typings/interfaces.d.ts | 3 ++- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/config.json b/src/config.json index 5ce8107..e4ead20 100644 --- a/src/config.json +++ b/src/config.json @@ -90,7 +90,8 @@ "bankick_log": "1048341961901363352", "fs_server_log": "1104632399771488317", "punishment_log": "1102751034754998302", - "dcmod_chat": "742324777934520350" + "dcmod_chat": "742324777934520350", + "mf_chat": "1149238561934151690" } } } diff --git a/src/funcs/HookManager.ts b/src/funcs/HookManager.ts index 998f280..2dc7659 100644 --- a/src/funcs/HookManager.ts +++ b/src/funcs/HookManager.ts @@ -5,8 +5,11 @@ import {readFileSync} from 'node:fs'; const config:Config = JSON.parse(readFileSync('src/config.json', 'utf-8')); type ChannelList = keyof typeof config.mainServer.channels; export default class HookMgr { + protected static async channelFetch(client:TClient, channel:ChannelList) { + return await client.channels.fetch(config.mainServer.channels[channel]) as Discord.TextChannel; + } protected static async fetch(client:TClient, channel:ChannelList, webhookId:Discord.Snowflake) { - const hookInstance = await (await client.channels.fetch(config.mainServer.channels[channel]) as Discord.TextChannel).fetchWebhooks().then(x=>x.find(y=>y.id===webhookId)); + const hookInstance = await (await this.channelFetch(client, channel)).fetchWebhooks().then(x=>x.find(y=>y.id===webhookId)); if (!hookInstance) throw new Error('[HookManager] Webhook not found.'); return hookInstance; } diff --git a/src/funcs/MPModule.ts b/src/funcs/MPModule.ts index ac24350..9fcb288 100644 --- a/src/funcs/MPModule.ts +++ b/src/funcs/MPModule.ts @@ -6,6 +6,7 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import FormatPlayer from '../helpers/FormatPlayer.js'; import Logger from '../helpers/Logger.js'; +import HookMgr from './HookManager.js'; import {writeFileSync, readFileSync} from 'node:fs'; import {FSPlayer, FSData, FSCareerSavegame} from '../typings/interfaces'; @@ -14,6 +15,7 @@ export default async(client:TClient, Channel:string, Message:string, Server:TSer const msg = await (client.channels.resolve(Channel) as Discord.TextChannel).messages.fetch(Message); const serverErrorEmbed = new client.embed().setColor(client.config.embedColorRed).setTitle('Host did not respond back in time'); const genericEmbed = new client.embed(); + const refreshIntervalText = 'Refreshes every 35 seconds.'; let sessionInit = {signal: AbortSignal.timeout(8500),headers:{'User-Agent':'Daggerbot - HITALL/undici'}}; try { @@ -81,8 +83,54 @@ export default async(client:TClient, Channel:string, Message:string, Server:TSer {name: 'Timescale', value: isNaN(Number(hitCSG?.settings?.timeScale._text)) === true ? 'Unavailable' : formatTimescale(Number(hitCSG.settings?.timeScale._text), 0, 'x'), inline: true} ); const playersEmbed = new client.embed().setColor(client.config.embedColor).setTitle(hitDSS.server.name).setDescription(hitDSS.slots.used < 1 ? '*No players online*' : playerData.join('\n\n')).setAuthor({name:`${hitDSS.slots.used}/${hitDSS.slots.capacity}`}); - msg.edit({content:'This embed updates every 35 seconds.',embeds:[serverDetails, playersEmbed]}); + msg.edit({content:refreshIntervalText,embeds:[serverDetails, playersEmbed]}); } + + // #multifarm_chat webhook + let dataUnavailable = 'Unavailable'; + const growthModeTextMap = { + '1': 'Yes', + '2': 'No', + '3': 'Growth paused' + } + const growthModeText = growthModeTextMap[hitCSG.settings.growthMode._text] ?? dataUnavailable; + + function genericMapping(map: Record, key: string, defaultValue: T): T { + return map[key] ?? defaultValue; + } + const genericTextMap = { + 'false': 'Off', + 'true': 'On' + } + + const fuelUsageTextMap = { + '1': 'Low', + '2': 'Normal', + '3': 'High' + } + const fuelUsageText = fuelUsageTextMap[hitCSG.settings.fuelUsage._text] ?? dataUnavailable; + + const dirtIntervalTextMap = { + '1': 'Off', + '2': 'Slow', + '3': 'Normal', + '4': 'Fast' + } + const dirtIntervalText = dirtIntervalTextMap[hitCSG.settings.dirtInterval._text] ?? dataUnavailable; + // Edit the embed in #multifarm_chat + HookMgr.edit(client, 'mf_chat', '1159998634604109897', '1160098458997370941', { + content: refreshIntervalText, + embeds: [new client.embed().setColor(client.config.embedColor).setTitle('Savegame Settings').addFields( + {name: 'Seasonal Growth', value: growthModeText, inline: true}, + {name: 'Crop Destruction', value: genericMapping(genericTextMap, hitCSG.settings.fruitDestruction._text, dataUnavailable), inline: true}, + {name: 'Periodic Plowing', value: genericMapping(genericTextMap, hitCSG.settings.plowingRequiredEnabled._text, dataUnavailable), inline: true}, + {name: 'Stones', value: genericMapping(genericTextMap, hitCSG.settings.stonesEnabled._text, dataUnavailable), inline: true}, + {name: 'Lime', value: genericMapping(genericTextMap, hitCSG.settings.limeRequired._text, dataUnavailable), inline: true}, + {name: 'Weeds', value: genericMapping(genericTextMap, hitCSG.settings.weedsEnabled._text, dataUnavailable), inline: true}, + {name: 'Fuel Usage', value: fuelUsageText, inline: true}, + {name: 'Dirt Interval', value: dirtIntervalText, inline: true}, + ).setFooter({text: 'Last updated'}).setTimestamp()] + }); } catch(err) { msg.edit({content: err.message, embeds: [serverErrorEmbed]}); Logger.forwardToConsole('log', 'MPModule', `Failed to make a request for ${ServerName}: ${err.message}`); diff --git a/src/typings/interfaces.d.ts b/src/typings/interfaces.d.ts index 56c4c82..42ec60e 100644 --- a/src/typings/interfaces.d.ts +++ b/src/typings/interfaces.d.ts @@ -174,7 +174,8 @@ export interface Config { bankick_log: string, fs_server_log: string, punishment_log: string, - dcmod_chat: string + dcmod_chat: string, + mf_chat: string } } }