From df1e2bcaa38b248de1bef1ff37a99e914565c9d5 Mon Sep 17 00:00:00 2001 From: ItzDabbzz <45545996+ItzDabbzz@users.noreply.github.com> Date: Sat, 14 Dec 2024 04:00:42 -0600 Subject: [PATCH 1/2] Create status_message.js A simple live status message that updates every time a player joins or leaves city :) --- optional addons/status_message.js | 119 ++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 optional addons/status_message.js diff --git a/optional addons/status_message.js b/optional addons/status_message.js new file mode 100644 index 0000000..c429dc4 --- /dev/null +++ b/optional addons/status_message.js @@ -0,0 +1,119 @@ +/** + * This file is part of zdiscord. + * Copyright (C) 2021 Tony/zfbx + * source: + * + * This work is licensed under the Creative Commons + * Attribution-NonCommercial-ShareAlike 4.0 International License. + * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ + * or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. + * + * This Addon was created by [ItzDabbzz](https://github.com/ItzDabbzz) + * + * This addon sends a message with status updates every x number of minutes in a specified channel + * copy this into your `server/addons` folder and edit the channelId to the channel id you want messages sent. + */ + +const { MessageEmbed, MessageActionRow, MessageButton } = require("discord.js"); + + +class StatusMessage { + constructor(z) { + // Minutes + this.timerDelay = 5; + // Channel id to send server status updates + this.channelId = "000000000000000000"; + // City join ip + this.cityJoinURL = 'cfx.re/join/'; + // store message id so we can edit it later + this.messageId = null; + // object to store players in the city + this.inCity = {} + + + this.z = z; + on("zdiscord:ready", async () => { + this.post(); + this.start(); + }); + } + + async start() { + setInterval(() => { + this.post(); + }, 1000 * 60 * this.timerDelay); + + on("playerJoining", async (oldId) => { + const source = global.source; + const member = this.z.bot.getMemberFromSource(source); + if (!member) return; + const playerName = GetPlayerName(source); + this.inCity[member.id] = { + name: playerName, + id: source, + } + this.post(); + }) + + on("playerDropped", async (reason) => { + const source = global.source; + const member = this.z.bot.getMemberFromSource(source); + delete this.inCity[member.id]; + this.post(); + }) + } + + async post() { + try { + const channel = await this.z.bot.channels.fetch(this.channelId); + let playerMessage = `**Current Connected Player(s):**\n\n`; + + for (const [key, value] of Object.entries(this.inCity)) { + playerMessage += `[${value.id}] ${value.name} - <@${key}>\n`; + } + + if (Object.keys(this.inCity).length === 0) playerMessage = "No Players Online"; + + const embed = new MessageEmbed() + .setTitle(`${this.z.config.FiveMServerName}`) + .setDescription(`${playerMessage}\n`) + .addFields( + { + name: "Online Players", + value: `\`\`\`${GetNumPlayerIndices()}/${GetConvar("sv_maxClients", "48")}\`\`\``, + inline: true + }, + { + name: "Uptime", + value: `\`\`\`${z.utils.timeformat((GetGameTimer() / 1000))}\`\`\``, + inline: true + } + ) + .setColor("#00ff00") + .setTimestamp() + const join = new MessageButton() + .setLabel('Join City') + .setURL(this.cityJoinURL) + .setStyle('LINK') + const row = new MessageActionRow() + .addComponents(join); + + if (this.messageId) { + const message = await channel.messages.fetch(this.messageId); + message.edit({ embeds: [embed] }); + } else { + const message = await channel.send({ + embeds: [embed], + components: [row], + }); + this.messageId = message.id; + } + } + catch (e) { + console.error(e); + } + } + +} + +module.exports = StatusMessage; From 40aec4a6f4d85b2bc6ef8eec0a8affbd8c1b7d0e Mon Sep 17 00:00:00 2001 From: ItzDabbzz <45545996+ItzDabbzz@users.noreply.github.com> Date: Sat, 14 Dec 2024 04:12:10 -0600 Subject: [PATCH 2/2] Update status_message.js - Added https:// to join url - updated console.error w/ log,error --- optional addons/status_message.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optional addons/status_message.js b/optional addons/status_message.js index c429dc4..c9a9a23 100644 --- a/optional addons/status_message.js +++ b/optional addons/status_message.js @@ -24,7 +24,7 @@ class StatusMessage { // Channel id to send server status updates this.channelId = "000000000000000000"; // City join ip - this.cityJoinURL = 'cfx.re/join/'; + this.cityJoinURL = 'https://cfx.re/join/'; // store message id so we can edit it later this.messageId = null; // object to store players in the city @@ -110,7 +110,7 @@ class StatusMessage { } } catch (e) { - console.error(e); + this.z.utils.log.error(e); } }