Skip to content

Commit

Permalink
refactored chat msg into util for easy converting
Browse files Browse the repository at this point in the history
  • Loading branch information
zfbx committed Dec 9, 2021
1 parent f1a9fe0 commit 0fa1369
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
11 changes: 11 additions & 0 deletions docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
4 changes: 1 addition & 3 deletions server/commands/announcement.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ module.exports = {
],

run: async (client, interaction, args) => {
emitNet("chat:addMessage", -1, {
template: `<div class=chat-message server'><strong>${client.z.locale.announcement}:</strong> ${args.message}</div>`,
});
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 });
},
Expand Down
4 changes: 1 addition & 3 deletions server/commands/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<div class=chat-message server'><strong>${client.z.locale.directMessage}:</strong> ${args.message}</div>`,
});
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 });
},
Expand Down
5 changes: 1 addition & 4 deletions server/commands/qb-ban.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ module.exports = {
bantime,
interaction.member.id,
]);
emitNet("chat:addMessage", -1, {
template: "<div class=chat-message server'><strong>ANNOUNCEMENT | {0} has been banned:</strong> {1}</div>",
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}`);
Expand Down
8 changes: 2 additions & 6 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "<div class=chat-message server'><strong>[server]:</strong> Staff Chat Disabled</div>",
});
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: "<div class=chat-message server'><strong>[server]:</strong> Staff Chat Enabled</div>",
});
z.utils.chatMessage(source, z.locale.staffchat, "Staff chat enabled.", { color: [ 255, 255, 0 ] });
}
}
}, "zdiscord.staffchat");
Expand Down
19 changes: 16 additions & 3 deletions server/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<div class=chat-message server'><strong>[${z.locale.staffchat}] ${name}:</strong> ${msg}</div>`,
});
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;

0 comments on commit 0fa1369

Please sign in to comment.