fivem icon indicating copy to clipboard operation
fivem copied to clipboard

feat(discord): user toggle for rich presence

Open tens0rfl0w opened this issue 2 years ago • 13 comments

Requested in https://forum.cfx.re/t/5018677

Allow users to either completely disable Discords Rich Presence status or only show the current game version without displaying any game status.

tens0rfl0w avatar Nov 01 '23 23:11 tens0rfl0w

Shouldn't the flag for the ConVar be ConVar_UserPref

AvarianKnight avatar Nov 01 '23 23:11 AvarianKnight

Shouldn't the flag for the ConVar be ConVar_UserPref

Yes, correct! I missed this one, thank you. Should be fixed.

tens0rfl0w avatar Nov 02 '23 00:11 tens0rfl0w

You could probably handle all of this in OnGameFrame and not have to modify the natives to have a bunch of g_richPresenceEnabled->GetValue() calls everywhere

static std::shared_ptr<ConVar<bool>> g_richPresenceEnabled = std::make_shared<ConVar<bool>>("game_enableDiscordRichPresence", ConVar_Archive | ConVar_UserPref, true);

static bool g_discordRichPresenceEnabled;

static bool g_wasRichPresencePreviouslyEnabled = true;

OnGameFrame.Connect([&handlers]()
{
	if (!g_discordRichPresenceEnabled)
	{
		if (g_wasRichPresencePreviouslyEnabled)
		{
			g_wasRichPresencePreviouslyEnabled = false;
			Discord_Shutdown();
		}
		return;
	}
	
	if (!g_wasRichPresencePreviouslyEnabled)
	{
		g_wasRichPresencePreviouslyEnabled = true;
		Discord_Initialize(g_discordAppId.c_str(), &handlers, 1, nullptr);
	}
	
	Discord_RunCallbacks();

	UpdatePresence();
});

AvarianKnight avatar Nov 02 '23 15:11 AvarianKnight

You could probably handle all of this in OnGameFrame and not have to modify the natives to have a bunch of g_richPresenceEnabled->GetValue() calls everywhere

Decided to go with your approach: If disabled, Rich Presence should be completely killed. Implemented like you proposed.

tens0rfl0w avatar Nov 02 '23 17:11 tens0rfl0w

It would be cool to have a getter to check if the player has enabled Rich Presence (for example, to offer rewards if the player enables this option).

spacevx avatar Nov 05 '23 14:11 spacevx

Servers have notoriously abused stuff like that

AvarianKnight avatar Nov 05 '23 15:11 AvarianKnight

You can already get the current ConVar value with GetConvar("game_enableDiscordRichPresence", "true"). I already wanted to block this ConVar from ScRTs reads to avoid a situation like this one, seems like I forgot about this while implementing. Any objections to blocking this?

tens0rfl0w avatar Nov 05 '23 15:11 tens0rfl0w

I've discussed the mentioned improvements in the forum post and discussed this feature internally, we'd like to see it offer at least 3 options:

  1. Off
  2. On - only mentioning FiveM/RedM
  3. On - with server information

I'm more drawn to naming it cl_discordRichPresence for these options.

thorium-cfx avatar Nov 07 '23 22:11 thorium-cfx

Will implement like proposed, thank you for the feedback.

tens0rfl0w avatar Nov 08 '23 01:11 tens0rfl0w

This should now include all desired changes requested by @thorium-cfx.

The current commit has two implementation aspects I'm not sure about, but wasn't able to accomplish without changing other things first: -~~ConVar change handlers won't get called if the ConVar value is unset and the default value is returned, so on a "fresh" install, Discord Rich Presence will not work till the user restarts their game at least once to get the ConVar set.~~ Applied a small workaround to properly set this on first startup and correct wrong values set by a user executing set cl_discordRichPresence -cfx-ui doesn't expose any ConVar setters for int values, this is why I went with string here.

Also, regarding possible "unwanted" side-effects: Should this ConVar be readable by ScRTs to avoid servers kicking users if they decide to disable their rich presence?

tens0rfl0w avatar Nov 08 '23 09:11 tens0rfl0w

Should this ConVar be readable by ScRTs to avoid servers kicking users if they decide to disable their rich presence?

Probably out of scope, but can't this be achieved with a discord bot? Most servers already force you to join their discord server.

Moozdzn avatar Nov 08 '23 12:11 Moozdzn

Thank you @tens0rfl0w, I've been quite busy and will review the changes soon, but wanted to address this question:

Also, regarding possible "unwanted" side-effects: Should this ConVar be readable by ScRTs to avoid servers kicking users if they decide to disable their rich presence?

Seeing that the player wants to hide this for personal or privacy reasons, there's reason enough not to expose this to anyone, so no it shouldn't be readable.

thorium-cfx avatar Nov 09 '23 09:11 thorium-cfx

Returning default_ now if read by GET_CONVAR.

(CI checks seem hung, repush didn't help)

tens0rfl0w avatar Nov 09 '23 19:11 tens0rfl0w

~~Seems stale~~

Wrong assumption by me, apologies.

Merge conflicts resolved.

tens0rfl0w avatar Jun 11 '24 14:06 tens0rfl0w