Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better Discord Rich Presence Support. #67

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mods/deathmatch/mtaserver.conf.bak
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@
<resource src="dynamic_lighting" startup="1" />
<resource src="dynamic_lighting_flash" startup="1" />
<resource src="dynamic_lighting_vehicles" startup="1" />
<resource src="discord" startup="1" />
<!--<resource src="snow" startup="1"/>
<resource src="shader_snow_ground" startup="1"/>
<resource src="xmas" startup="1"/>-->
Expand Down
3 changes: 3 additions & 0 deletions mods/deathmatch/resources/account/s_characters.lua
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ function spawnCharacter(characterID, remoteAccountID, theAdmin, targetAccountNam
if freshSpawn then
triggerEvent( "social:look", client, client, ":edit" )
end

-- update discord
triggerClientEvent(client, "discord:updateRPC", client)
end
end
addEventHandler("accounts:characters:spawn", getRootElement(), spawnCharacter)
Expand Down
37 changes: 37 additions & 0 deletions mods/deathmatch/resources/discord/c_discord.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Update function.
function updateRPC()
if isDiscordRichPresenceConnected() then
local name = getPlayerName(localPlayer)

local currentPlayers = getElementsByType("player")
local maxPlayers = getElementData(root, "server:Slots") or 1024

if getElementData(localPlayer, "loggedin") == 0 then
setDiscordRichPresenceDetails("Logging into OwlGaming...")
else
setDiscordRichPresenceDetails("Playing as: "..name:gsub("_", " "))
end

setDiscordRichPresenceState("Currently " .. #currentPlayers .. "/" .. maxPlayers .. " playing!")
end
end
addEvent("discord:updateRPC", true)
addEventHandler("discord:updateRPC", root, updateRPC)

function connectRPC(appId)
-- reset
resetDiscordRichPresenceData()

-- set discord ApplicationID
setDiscordApplicationID(appId)

-- force an update.
updateRPC()
end
addEvent("discord:connectRPC", true)
addEventHandler("discord:connectRPC", root, connectRPC)

-- prevents the rich presence bugging.
addEventHandler("onClientResourceStop", resourceRoot, function()
resetDiscordRichPresenceData()
end)
6 changes: 6 additions & 0 deletions mods/deathmatch/resources/discord/meta.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<meta>
<info author="OwlGaming" type="script"/>

<script type="client" src="c_discord.lua" cache="false"/>
<script type="server" src="s_discord.lua"/>
</meta>
20 changes: 20 additions & 0 deletions mods/deathmatch/resources/discord/s_discord.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- The below function will handle if the resource is restarted while players are on the server... let's sort out a few things...
local appKey = get("application_id")

function startUp()
for _, player in ipairs(getElementsByType("player")) do
triggerClientEvent(player, "discord:connectRPC", player, appKey)
end
end

-- we add a delay here so that the client resource loads in.
function delayStartup()
setTimer(startUp, 2000, 1)
end
addEventHandler("onResourceStart", root, delayStartup)

-- sends the application key to the player on join.
function playerJoin()
triggerClientEvent(source, "discord:connectRPC", source, appKey)
end
addEventHandler("onPlayerJoin", root, playerJoin)
3 changes: 3 additions & 0 deletions mods/deathmatch/settings.xml.bak
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<!-- Imgur Client ID used to get dimensions of skins and item-texture stuff -->
<setting name="imgurClient" value="IMGUR_API_KEY" />

<!-- Discord Application ID for integration -->
<setting name="@discord.application_id" value="DISCORD_APP_KEY"/>

<!-- Scanner -->
<setting name="@payday.inactivityscanner_vehicles" value="1" />
<setting name="@payday.inactivityscanner_interiors" value="1" />
Expand Down