mirror of
https://github.com/toast-ts/Daggerbot-TS.git
synced 2024-11-17 12:21:00 -05:00
Add webhook for multifarm chat
This commit is contained in:
parent
c31bda2fbc
commit
94207e693b
@ -90,7 +90,8 @@
|
||||
"bankick_log": "1048341961901363352",
|
||||
"fs_server_log": "1104632399771488317",
|
||||
"punishment_log": "1102751034754998302",
|
||||
"dcmod_chat": "742324777934520350"
|
||||
"dcmod_chat": "742324777934520350",
|
||||
"mf_chat": "1149238561934151690"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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<T>(map: Record<string, T>, 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}`);
|
||||
|
3
src/typings/interfaces.d.ts
vendored
3
src/typings/interfaces.d.ts
vendored
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user