From 0fa136969ae84462894b0183b9ff2ee2e27541de Mon Sep 17 00:00:00 2001 From: Tony <1414927+zfbx@users.noreply.github.com> Date: Wed, 8 Dec 2021 19:30:30 -0500 Subject: [PATCH] refactored chat msg into util for easy converting --- docs/faq.md | 11 +++++++++++ server/commands/announcement.js | 4 +--- server/commands/message.js | 4 +--- server/commands/qb-ban.js | 5 +---- server/server.js | 8 ++------ server/utils.js | 19 ++++++++++++++++--- 6 files changed, 32 insertions(+), 19 deletions(-) diff --git a/docs/faq.md b/docs/faq.md index a72b184..8151414 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -13,6 +13,8 @@ Here's a list of common problems people have run into and how to solve them. If - [Buffer Deprecation Warning](#buffer-deprecation-warning) - [Could not find dependency /server:4890](#could-not-find-dependency-server4890) - [QBCore commands aren't showing](#qbcore-commands-not-showing) +- [New chat messages / announcements aren't popping up when recieved](#new-chat-messages--announcements-arent-popping-up-when-recieved) +- [How to use custom chat](#how-to-use-custom-chat) ### No Slash Commands? @@ -90,3 +92,12 @@ if (client.QBCore) utils.log.info("QBCore found! Supported QB commands will be l ``` **Note:** Not all commands are backwards compatible with the previous version of QBCore as it would be very hard to support many versions of backwards compatibility so I recommend updating :) + + +### New chat messages / announcements aren't popping up when recieved + +cfx chat by default has modes you can toggle between with a keybind (default: L) and if you change it to "WHEN ACTIVE" it'll show when new messages arrive + +### How to use custom chat + +You really shouldn't use a custom chat but rather theme the default but IF you did.. you can change the functionality of the chat in zdiscord by modifying the function `chatMessage` in `utils.js` as all messages in game are forwarded through therre diff --git a/server/commands/announcement.js b/server/commands/announcement.js index a2b2ab8..068fc10 100644 --- a/server/commands/announcement.js +++ b/server/commands/announcement.js @@ -25,9 +25,7 @@ module.exports = { ], run: async (client, interaction, args) => { - emitNet("chat:addMessage", -1, { - template: `
${client.z.locale.announcement}: ${args.message}
`, - }); + client.utils.chatMessage(-1, client.z.locale.announcement, args.message, { color: [ 255, 0, 0 ] }); client.utils.log.info(`[${interaction.member.displayName}] Announcement: ${args.message}`); interaction.reply({ content: "Announcement Sent", ephemeral: false }); }, diff --git a/server/commands/message.js b/server/commands/message.js index 0d8ce9c..6482aa0 100644 --- a/server/commands/message.js +++ b/server/commands/message.js @@ -32,9 +32,7 @@ module.exports = { run: async (client, interaction, args) => { if (!GetPlayerName(args.id)) return interaction.reply({ content: "This ID seems invalid.", ephemeral: true }); - emitNet("chat:addMessage", args.id, { - template: `
${client.z.locale.directMessage}: ${args.message}
`, - }); + client.utils.chatMessage(args.id, client.z.locale.directMessage, args.message); client.utils.log.info(`[${interaction.member.displayName}] sent a DM to ${GetPlayerName(args.id)} (${args.id}): ${args.message}`); return interaction.reply({ content: `Message sent to ${GetPlayerName(args.id)} (${args.id}).`, ephemeral: false }); }, diff --git a/server/commands/qb-ban.js b/server/commands/qb-ban.js index 3df9b72..563c2b9 100644 --- a/server/commands/qb-ban.js +++ b/server/commands/qb-ban.js @@ -53,10 +53,7 @@ module.exports = { bantime, interaction.member.id, ]); - emitNet("chat:addMessage", -1, { - template: "
ANNOUNCEMENT | {0} has been banned: {1}
", - args: [ GetPlayerName(args.id), args.reason ], - }); + client.utils.chatMessage(-1, client.z.locale.announcement, `${GetPlayerName(args.id)} has been banned for breaking the rules.`, { color: [ 155, 0, 0 ] }); emit("qb-log:server:CreateLog", "bans", "Player Banned", "red", `${GetPlayerName(args.id)} was banned by ${interaction.member.displayName} for ${args.reason}`, true); if (bantime >= 2147483647) { DropPlayer(args.id, `You have been banned:\n${args.reason}\n\nYour ban is permanent.\n🔸 Check our Discord for more information: ${client.QBCore.Config.Server.discord}`); diff --git a/server/server.js b/server/server.js index e75e53d..d6cea04 100644 --- a/server/server.js +++ b/server/server.js @@ -99,16 +99,12 @@ if (z.config.EnableStaffChatForwarding) { RegisterCommand("stafftoggle", (source, args, raw) => { if (IsPlayerAceAllowed(source, "zdiscord.staffchat")) { ExecuteCommand(`remove_principal "player.${source}" group.zdiscordstaff`); - emitNet("chat:addMessage", source, { - template: "
[server]: Staff Chat Disabled
", - }); + z.utils.chatMessage(source, z.locale.staffchat, "Staff chat disabled.", { color: [ 255, 255, 0 ] }); } else { const member = z.bot.getMemberFromSource(source); if (z.bot.isRolePresent(member, [ z.config.DiscordModRoleId, z.config.DiscordAdminRoleId, z.config.DiscordGodRoleId ])) { ExecuteCommand(`add_principal "player.${source}" group.zdiscordstaff`); - emitNet("chat:addMessage", source, { - template: "
[server]: Staff Chat Enabled
", - }); + z.utils.chatMessage(source, z.locale.staffchat, "Staff chat enabled.", { color: [ 255, 255, 0 ] }); } } }, "zdiscord.staffchat"); diff --git a/server/utils.js b/server/utils.js index c09b497..4fdbfa1 100644 --- a/server/utils.js +++ b/server/utils.js @@ -183,10 +183,23 @@ const sendStaffChatMessage = (z, name, msg) => { if (!msg) return; getPlayers().forEach(async function(player, index, array) { if (IsPlayerAceAllowed(player, "zdiscord.staffchat")) { - emitNet("chat:addMessage", player, { - template: `
[${z.locale.staffchat}] ${name}: ${msg}
`, - }); + chatMessage(player, `[${z.locale.staffchat}] ${name}`, msg, { multiline: false, color: [ 255, 100, 0 ] }); } }); }; exports.sendStaffChatMessage = sendStaffChatMessage; + + +/** send chat message + * @param {number} destination - source id or -1 for all + * @param {string} label - Name the message is from + * @param {string} msg - message to send + * @param {object} options - options: array color[r, g, b], bool multiline */ +const chatMessage = (destination, label, msg, options) => { + TriggerClientEvent("chat:addMessage", destination, { + color: (options.color || [ 255, 255, 255 ]), + multiline: options.multiline || false, + args: [ label, msg ], + }); +}; +exports.chatMessage = chatMessage;