2023-11-07 09:04:22 +00:00
|
|
|
package funkin.api.discord;
|
2022-03-08 08:13:53 +00:00
|
|
|
|
|
|
|
import Sys.sleep;
|
2024-08-26 22:01:36 +00:00
|
|
|
#if FEATURE_DISCORD_RPC
|
2022-03-08 08:13:53 +00:00
|
|
|
import discord_rpc.DiscordRpc;
|
|
|
|
#end
|
|
|
|
|
|
|
|
class DiscordClient
|
|
|
|
{
|
2024-08-26 22:01:36 +00:00
|
|
|
#if FEATURE_DISCORD_RPC
|
2023-01-23 03:25:45 +00:00
|
|
|
public function new()
|
|
|
|
{
|
|
|
|
trace("Discord Client starting...");
|
|
|
|
DiscordRpc.start(
|
|
|
|
{
|
|
|
|
clientID: "814588678700924999",
|
|
|
|
onReady: onReady,
|
|
|
|
onError: onError,
|
|
|
|
onDisconnected: onDisconnected
|
|
|
|
});
|
|
|
|
trace("Discord Client started.");
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
DiscordRpc.process();
|
|
|
|
sleep(2);
|
|
|
|
// trace("Discord Client Update");
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
DiscordRpc.shutdown();
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
public static function shutdown()
|
|
|
|
{
|
|
|
|
DiscordRpc.shutdown();
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
static function onReady()
|
|
|
|
{
|
|
|
|
DiscordRpc.presence(
|
|
|
|
{
|
|
|
|
details: "In the Menus",
|
|
|
|
state: null,
|
|
|
|
largeImageKey: 'icon',
|
|
|
|
largeImageText: "Friday Night Funkin'"
|
|
|
|
});
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
static function onError(_code:Int, _message:String)
|
|
|
|
{
|
|
|
|
trace('Error! $_code : $_message');
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
static function onDisconnected(_code:Int, _message:String)
|
|
|
|
{
|
|
|
|
trace('Disconnected! $_code : $_message');
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
public static function initialize()
|
|
|
|
{
|
2023-06-08 20:30:45 +00:00
|
|
|
var DiscordDaemon = sys.thread.Thread.create(() -> {
|
2023-01-23 03:25:45 +00:00
|
|
|
new DiscordClient();
|
|
|
|
});
|
|
|
|
trace("Discord Client initialized");
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-08-28 19:03:29 +00:00
|
|
|
public static function changePresence(details:String, ?state:String, ?smallImageKey:String, ?hasStartTimestamp:Bool, ?endTimestamp:Float)
|
2023-01-23 03:25:45 +00:00
|
|
|
{
|
|
|
|
var startTimestamp:Float = if (hasStartTimestamp) Date.now().getTime() else 0;
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
if (endTimestamp > 0)
|
|
|
|
{
|
|
|
|
endTimestamp = startTimestamp + endTimestamp;
|
|
|
|
}
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
DiscordRpc.presence(
|
|
|
|
{
|
|
|
|
details: details,
|
|
|
|
state: state,
|
|
|
|
largeImageKey: 'icon',
|
|
|
|
largeImageText: "Friday Night Funkin'",
|
|
|
|
smallImageKey: smallImageKey,
|
|
|
|
// Obtained times are in milliseconds so they are divided so Discord can use it
|
|
|
|
startTimestamp: Std.int(startTimestamp / 1000),
|
|
|
|
endTimestamp: Std.int(endTimestamp / 1000)
|
|
|
|
});
|
2022-03-08 08:13:53 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
// trace('Discord RPC Updated. Arguments: $details, $state, $smallImageKey, $hasStartTimestamp, $endTimestamp');
|
|
|
|
}
|
|
|
|
#end
|
2022-03-08 08:13:53 +00:00
|
|
|
}
|