Skip to content

Fix Discord Rich Presence integration#1692

Merged
Zykem merged 3 commits into
esx-framework:devfrom
alexistb2904:main
Aug 8, 2025
Merged

Fix Discord Rich Presence integration#1692
Zykem merged 3 commits into
esx-framework:devfrom
alexistb2904:main

Conversation

@alexistb2904

@alexistb2904 alexistb2904 commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

Description

Fix the discord activity that was not working. The problem before was that some variable in for example {server_endpoint}, were never replaced so it created invalid URL and in assetText {server_name} was not replaced too.


Motivation

To fix the discord Presence


Implementation Details

New function Adjustments:ReplacePlaceholders(text) that replace correctly all the placeholder in a text
Adjustments:PresencePlaceholders() use the new ReplacePlaceholders function to create the presence text.
SetDiscordRichPresenceAssetText and buttonUrl use ReplacePlaceholders to replace the placeholders by the actuals values


Usage Example

function Adjustments:ReplacePlaceholders(text)
    for placeholder, cb in pairs(placeHolders) do
        local success, result = pcall(cb)

        if not success then
            error(("Failed to execute placeholder: ^5%s^7"):format(placeholder))
            error(result)
            result = "Unknown"
        end

        text = text:gsub(("{%s}"):format(placeholder), tostring(result))
    end
    return text
end

function Adjustments:PresencePlaceholders()
    local presence = self:ReplacePlaceholders(Config.DiscordActivity.presence)
    print(presence)
    return presence
end

function Adjustments:DiscordPresence()
    if Config.DiscordActivity.appId ~= 0 then
        CreateThread(function()
            while true do
                SetDiscordAppId(Config.DiscordActivity.appId)
                SetRichPresence(self:PresencePlaceholders())
                SetDiscordRichPresenceAsset(Config.DiscordActivity.assetName)
                SetDiscordRichPresenceAssetText(self:ReplacePlaceholders(Config.DiscordActivity.assetText))
                for i = 1, #Config.DiscordActivity.buttons do
                    local button = Config.DiscordActivity.buttons[i]
                    local buttonUrl = self:ReplacePlaceholders(button.url)
                    SetDiscordRichPresenceAction(i - 1, button.label, buttonUrl)
                end
                Wait(Config.DiscordActivity.refresh)
            end
        end)
    end
end

PR Checklist

  • My commit messages and PR title follow the Conventional Commits standard.
  • My changes have been tested locally and function as expected.
  • My PR does not introduce any breaking changes.
  • I have provided a clear explanation of what my PR does, including the reasoning behind the changes and any relevant context.

@Kr3mu Kr3mu changed the base branch from main to dev August 4, 2025 10:11

@Tiziukas Tiziukas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks fine, guidelines are followed and the code works.

Please review the comment, once this is fixed, we can merge.

Sorry for being picky :)

Comment thread [core]/es_extended/client/modules/adjustments.lua

@Tiziukas Tiziukas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine to me, thanks.

@Zykem Zykem left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your PR.

I don't think the PresencePlaceholders should exist, it seems like it's just a wrapper for the actual ReplacePlaceholders, it's not doing anything more than just calling this function and returning it's return value.

Please use ReplacePlaceholders instead.

If your concern is code readability, you can localize it and name it something readable before using it for SetRichPresence.

@alexistb2904 alexistb2904 requested a review from Zykem August 8, 2025 09:50

@Zykem Zykem left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

@Zykem Zykem merged commit 2c285e1 into esx-framework:dev Aug 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants