Closed AdamAlNajar closed 2 months ago
my code could be dogshit just :rofl:
Maybe you should use this implementation instead
package backend;
#if DISCORD
import flixel.FlxG;
import hxdiscord_rpc.Discord as RichPresence;
import hxdiscord_rpc.Types;
import openfl.Lib;
import sys.thread.Thread;
class Discord
{
public static var initialized(default, null):Bool = false;
public static function load():Void
{
if (initialized)
return;
var handlers:DiscordEventHandlers = DiscordEventHandlers.create();
handlers.ready = cpp.Function.fromStaticFunction(onReady);
handlers.disconnected = cpp.Function.fromStaticFunction(onDisconnected);
handlers.errored = cpp.Function.fromStaticFunction(onError);
RichPresence.Initialize("1140307809167220836", cpp.RawPointer.addressOf(handlers), 1, null);
// Daemon Thread
Thread.create(function():Void
{
while (true)
{
#if DISCORD_DISABLE_IO_THREAD
RichPresence.UpdateConnection();
#end
RichPresence.RunCallbacks();
// Wait 2 seconds until the next loop...
Sys.sleep(2);
}
});
Lib.application.onExit.add((exitCode:Int) -> RichPresence.Shutdown());
initialized = true;
}
public static function changePresence(details:String, ?state:String):Void
{
var discordPresence:DiscordRichPresence = DiscordRichPresence.create();
discordPresence.details = details;
if (state != null)
discordPresence.state = state;
discordPresence.largeImageKey = "icon";
discordPresence.largeImageText = cast(Lib.application.meta['title'], String);
RichPresence.UpdatePresence(cpp.RawConstPointer.addressOf(discordPresence));
}
private static function onReady(request:cpp.RawConstPointer<DiscordUser>):Void
{
if (Std.parseInt(cast(request[0].discriminator, String)) != 0)
FlxG.log.notice('(Discord) Connected to User "${cast (request[0].username, String)}#${cast (request[0].discriminator, String)}"');
else
FlxG.log.notice('(Discord) Connected to User "${cast (request[0].username, String)}"');
Discord.changePresence('Just Started');
}
private static function onDisconnected(errorCode:Int, message:cpp.ConstCharStar):Void
{
FlxG.log.notice('(Discord) Disconnected ($errorCode: ${cast (message, String)})');
}
private static function onError(errorCode:Int, message:cpp.ConstCharStar):Void
{
FlxG.log.notice('(Discord) Error ($errorCode: ${cast (message, String)})');
}
}
#end
I used it in a project
im gonna test it and tell u how it works out
im gonna test it and tell u how it works out
I suggest you to also add <haxedef name="DISCORD_DISABLE_IO_THREAD" />
for better performance.
also, why is it wrapped in an if?
is it something that i should add to my project.xml?
is it something that i should add to my project.xml?
Yes
what also do u mean by initalized? what should i do with it? should i add anything in the entrypoint?
No no
there is a problem in this line :
RichPresence.UpdateConnection();
#end
i defined it in my xml
Remove the export and compile again
yep everything works! thx!
yep everything works! thx!
Np
hello, im currently writing some rpc code for discord using this library, and so far a few things work as i need but i get this problem whenever i load into the menustate its supposed to update the rpc, but it updates then shows the rpc ready message again. maybe im doing something wrong in flixel, but better to be safe than sorry Here are the 2 classes rpcmanager and the state
rpc code : https://pastebin.com/RSGvJnWM flixel code : https://pastebin.com/cnYC6gkx