Skip to content

Commit

Permalink
fix(menu): menu announcements not triggering event
Browse files Browse the repository at this point in the history
  • Loading branch information
tabarra committed Mar 14, 2023
1 parent 14bb402 commit 0716fe7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
42 changes: 42 additions & 0 deletions core/components/FxRunner/outputHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ export default class OutputHandler {
this.#txAdmin.resourcesManager.handleServerEvents(data.payload, mutex);
} else if (data.payload.type === 'txAdminPlayerlistEvent') {
this.#txAdmin.playerlistManager.handleServerEvents(data.payload, mutex);
} else if (data.payload.type === 'txAdminCommandBridge') {
this.bridgeCommand(data.payload);
}
}
} catch (error) {
Expand All @@ -109,6 +111,46 @@ export default class OutputHandler {
}
}


/**
* handles stdout and stderr from child fxserver and send to be processed by the logger
* TODO: use zod for type safety
*/
bridgeCommand(payload: any) {
if (payload.command === 'announcement') {
try {
// FIXME: discordBot.sendAnnouncement podia gerar um embed com cor variável e emoji
//Validate input
if (typeof payload.author !== 'string') throw new Error(`invalid author`);
if (typeof payload.message !== 'string') throw new Error(`invalid message`);
const message = (payload.message ?? '').trim();
if (!message.length) throw new Error(`empty message`);

//Resolve admin
const author = payload.author;
this.#txAdmin.logger.admin.write(author, `Sending announcement: ${message}`);

// Dispatch `txAdmin:events:announcement`
this.#txAdmin.fxRunner.sendEvent('announcement', { message, author });

// Sending discord announcement
const discMessage = message.replace(/\`/g, '\\`').replace(/\n/g, '\n> ');
const discMsgTitle = this.#txAdmin.translator.t(
'nui_menu.misc.announcement_title',
{ author }
);
this.#txAdmin.discordBot.sendAnnouncement(`${discMsgTitle}\n> ${discMessage}`);
} catch (error) {
console.verbose.warn(`bridgeCommand handler error:`);
console.verbose.dir(error);
}
} else {
console.warn(`Command bridge received invalid command:`);
console.dir(payload);
}
}


/**
* handles stdout and stderr from child fxserver and send to be processed by the logger
*/
Expand Down
1 change: 1 addition & 0 deletions core/components/PlayerlistManager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export default class PlayerlistManager {

/**
* Handler for all txAdminPlayerlistEvent structured trace events
* TODO: use zod for type safety
*/
async handleServerEvents(payload: any, mutex: string) {
if (payload.event === 'playerJoining') {
Expand Down
7 changes: 2 additions & 5 deletions docs/dev_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
- [x] merge translations
- [x] whitelist "discord no id" message should tell the user to open discord desktop
- [x] update most packages
- [ ] fix: menu > send announcement does not trigger discord msg nor custom event
- [ ] ConfigVault.saveProfile should probably throw the error up
- [x] ConfigVault.saveProfile should probably throw the error up
- [x] fix: menu > send announcement does not trigger discord msg nor custom event


(function() {
Expand Down Expand Up @@ -159,7 +159,6 @@ Whitelist Page/routes:
- [x] in-core playerlist state tracking
- [x] new proxy console util
- [ ] global socket.io connection for playerlist + async responses
- [ ] in-core resource state tracking
- [ ] new config (prepared for multiserver)
- [ ] multiserver tx instance (backend only)

Expand Down Expand Up @@ -247,8 +246,6 @@ Up next-ish:


### Randoms:
- BUG: nui menu triggered announcements are not sent to the discord

-- Why both have the same debug data? https://i.imgur.com/WGawiyr.png

FIXME: sendMenuMessage('setServerCtx', ServerCtx)
Expand Down
13 changes: 8 additions & 5 deletions resource/menu/server/sv_main_page.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ RegisterNetEvent('txAdmin:menu:tpToWaypoint', function()
end)

RegisterNetEvent('txAdmin:menu:sendAnnouncement', function(message)
--FIXME: this is not being relayed to discord
local src = source
if type(message) ~= 'string' then
return
end
local allow = PlayerHasTxPermission(src, 'players.message')
TriggerEvent("txaLogger:menuEvent", src, "announcement", allow, message)
if allow and not TX_HIDE_ANNOUNCEMENT then
local author = TX_ADMINS[tostring(src)].tag
TriggerClientEvent("txAdmin:receiveAnnounce", -1, message, author)
if allow then
PrintStructuredTrace(json.encode({
type = 'txAdminCommandBridge',
command = 'announcement',
author = TX_ADMINS[tostring(src)].username,
message = message,
}))
end
end)

Expand Down Expand Up @@ -130,7 +133,7 @@ RegisterNetEvent('txAdmin:menu:spawnVehicle', function(model, modelType)
end)

--- Deletes the vehicle the player is currently in
--- @param netId int
--- @param netId number
RegisterNetEvent("txAdmin:menu:deleteVehicle", function(netId)
local src = source
local allow = PlayerHasTxPermission(src, 'menu.vehicle')
Expand Down
1 change: 1 addition & 0 deletions resource/sv_admins.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ RegisterNetEvent('txsv:checkAdminStatus', function()
))
TX_ADMINS[srcString] = {
tag = adminTag,
username = resp.username,
perms = resp.permissions,
bucket = 0
}
Expand Down

0 comments on commit 0716fe7

Please sign in to comment.