From dfa36f2e4bfffef8319da0b1d3be36e08957d0ec Mon Sep 17 00:00:00 2001 From: SowinskiBraeden Date: Sun, 17 Nov 2024 12:09:46 -0800 Subject: [PATCH] fix/convert alarms to webhooks --- package.json | 2 +- src/DayzRBot.js | 19 ++++++++++++++----- util/AlarmsHandler.js | 36 ++++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 5fac438..e533bf6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dayzr-bot", - "version": "13.3.17", + "version": "13.3.18", "description": "A General Purpose Discord Bot for DayZ Nitrado Servers.", "main": "index.js", "nodemonConfig": { diff --git a/src/DayzRBot.js b/src/DayzRBot.js index ef81467..6524dfe 100644 --- a/src/DayzRBot.js +++ b/src/DayzRBot.js @@ -11,6 +11,7 @@ const { HandlePlayerLogs, HandleActivePlayersList } = require('../util/LogsHandl const { HandleKillfeed, UpdateLastDeathDate } = require('../util/KillfeedHandler'); const { HandleExpiredUAVs, HandleEvents, PlaceFireplaceInAlarm } = require('../util/AlarmsHandler'); const { decrypt } = require('../util/Cryptic'); +const { GetWebhook, WebhookSend } = require("../util/WebhookHandler"); // Data structures imports const { getDefaultPlayer, UpdatePlayer } = require('../database/player'); @@ -213,17 +214,25 @@ class DayzRBot extends Client { const maxEmbed = 10; this.alarmPingQueue.forEach(queue => { - queue.forEach((data, channel_id) => { + queue.forEach(async (data, channel_id) => { const channel = this.GetChannel(channel_id); if (!channel) return; - data.forEach((embeds, role) => { + + const NAME = "DayZ.R Zone Alert"; + const webhook = await GetWebhook(this, NAME, channel_id); + + data.forEach(async (embeds, role) => { let embedArrays = []; while (embeds.length > 0); embedArrays.push(embeds.splice(0, maxEmbed)); - for (let i = 0; i < embedArrays.length; i++) { - if (role == '-no-role-ping-') channel.send({ embeds: embedArrays[i] }); - else channel.send({ content: `<@&${role}>`, embeds: embedArrays[i] }); + for (let i = 0; i < embedArrays.length; i++) { + let content = { embeds: embedArrays[i] }; + if (role != '-no-role-ping-') content.content = `<@&${role}>`; + WebhookSend(this, webhook, content); + + // if (role == '-no-role-ping-') channel.send({ embeds: embedArrays[i] }); + // else channel.send({ content: `<@&${role}>`, embeds: embedArrays[i] }); } }); }); diff --git a/util/AlarmsHandler.js b/util/AlarmsHandler.js index 5ca8911..d9ed183 100644 --- a/util/AlarmsHandler.js +++ b/util/AlarmsHandler.js @@ -2,6 +2,7 @@ const { BanPlayer, UnbanPlayer } = require('./NitradoAPI'); const { EmbedBuilder } = require('discord.js'); const { nearest } = require('../database/destinations'); const { GetGuild } = require('../database/guild'); +const { GetWebhook, WebhookSend } = require("../util/WebhookHandler"); // Private functions (only called locally) @@ -20,6 +21,10 @@ const ExpireEvent = async(client, guild, e) => { } const HandlePlayerTrackEvent = async (client, guild, e) => { + if (!client.exists(e.channel)) return ExpireEvent(client, guild, e); // Expire event since it has invalid channel. + const channel = client.GetChannel(e.channel); + if (!channel) return; + let player = await client.dbo.collection("players").findOne({"gamertag": e.gamertag}); let newDt = await client.getDateEST(player.time); @@ -31,10 +36,15 @@ const HandlePlayerTrackEvent = async (client, guild, e) => { .setColor(client.config.Colors.Default) .setDescription(`**${e.name} Event**\n${e.gamertag} was located at **[${player.pos[0]}, ${player.pos[1]}](https://www.izurvive.com/chernarusplussatmap/#location=${player.pos[0]};${player.pos[1]})** at \n${destination}`); - if (!client.exists(e.channel)) return ExpireEvent(client, guild, e); // Expire event since it has invalid channel. - const channel = client.GetChannel(e.channel); - if (e.role) channel.send({ content: `<@&${e.role}>`, embeds: [trackEvent] }); - else channel.send({ embeds: [trackEvent] }); + const NAME = "DayZ.R Player Tracker"; + const webhook = await GetWebhook(client, NAME, e.channel); + + let content = { embeds: [trackEvent] }; + if (client.exists(guild.adminRole)) content.content = `<@&${e.role}>`; + WebhookSend(client, webhook, content); + + // if (e.role) channel.send({ content: `<@&${e.role}>`, embeds: [trackEvent] }); + // else channel.send({ embeds: [trackEvent] }); let now = new Date(); let diff = ((now - e.creationDate) / 1000) / 60; @@ -157,6 +167,7 @@ module.exports = { if (distance < alarm.radius) { const channel = client.GetChannel(alarm.channel); + if (!channel) continue; let newDt = await client.getDateEST(data.time); let unixTime = Math.floor(newDt.getTime()/1000); @@ -166,7 +177,13 @@ module.exports = { .setDescription(`**Zone Ping - **\n**${data.killer}** was located within **${distance} meters** of the Zone **${alarm.name}** __and has been banned for killing **${data.victim}**.__`) .addFields({ name: '**Location**', value: `**[${data.killerPOS[0]}, ${data.killerPOS[1]}](https://www.izurvive.com/chernarusplussatmap/#location=${data.killerPOS[0]};${data.killerPOS[1]})**`, inline: false }) - channel.send({ content: `<@&${alarm.role}>`, embeds: [alarmEmbed] }); + const NAME = "DayZ.R Zone Alert"; + const webhook = await GetWebhook(client, NAME, alarm.channel); + + let content = { content: `<@&${alarm.role}>`, embeds: [alarmEmbed] }; + WebhookSend(client, webhook, content); + + // channel.send({ content: `<@&${alarm.role}>`, embeds: [alarmEmbed] }); BanPlayer(client, data.killer); break; @@ -198,6 +215,7 @@ module.exports = { if (distance < alarm.radius) { const channel = client.GetChannel(alarm.channel); + if (!channel) return; let newDt = await client.getDateEST(info.time); let unixTime = Math.floor(newDt.getTime()/1000); @@ -207,7 +225,13 @@ module.exports = { .setDescription(`**Zone Ping - **\n**${info.player}** was located within **${distance} meters** of the Zone **${alarm.name}** __and has been banned for **placing a fireplace**.__`) .addFields({ name: '**Location**', value: `**[${info.playerPOS[0]}, ${info.playerPOS[1]}](https://www.izurvive.com/chernarusplussatmap/#location=${info.playerPOS[0]};${info.playerPOS[1]})**`, inline: false }) - channel.send({ content: `<@&${alarm.role}>`, embeds: [alarmEmbed] }); + const NAME = "DayZ.R Zone Alert"; + const webhook = await GetWebhook(client, NAME, alarm.channel); + + let content = { content: `<@&${alarm.role}>`, embeds: [alarmEmbed] }; + WebhookSend(client, webhook, content); + + // channel.send({ content: `<@&${alarm.role}>`, embeds: [alarmEmbed] }); BanPlayer(client, info.player); break;