示例#1
0
 public static extern void Discord_Initialize([MarshalAs(UnmanagedType.LPUTF8Str)] string applicationId, ref DiscordEventHandlers handlers, bool autoRegister, [MarshalAs(UnmanagedType.LPUTF8Str)] string optionalSteamId = null, int optionalPipeNumber = 0);
        // ReSharper restore PrivateFieldCanBeConvertedToLocalVariable

        public unsafe DiscordPresence(string appId, int sleepTime = 15)
        {
            lock (PresenceLock)
            {
                _sleepTime     = sleepTime;
                _readyCallback = (request, _) =>
                {
                    if (request == null)
                    {
                        Log.Warning("Received null user info from discord!");
                    }
                    else
                    {
                        Log.Information($"Discord connected to {Marshal.PtrToStringUTF8(request->username)}#{Marshal.PtrToStringUTF8(request->discriminator)} with ID {Marshal.PtrToStringUTF8(request->userId)}");
                    }
                };
                _disconnectedCallback = (code, message, _) => Log.Information($"Discord disconnected with code {code} with message {Marshal.PtrToStringUTF8(message)}");
                _errorCallback        = (code, message, _) => Log.Information($"Discord errored with code {code} with message {Marshal.PtrToStringUTF8(message)}");
                _handlers             = new DiscordEventHandlers
                {
                    userData             = IntPtr.Zero,
                    readyCallback        = _readyCallback,
                    disconnectedCallback = _disconnectedCallback,
                    errorCallback        = _errorCallback,
                    joinCallback         = null,
                    spectateCallback     = null,
                    requestCallback      = null
                };
                lock (DisposeLock)
                {
                    DiscordNative.Discord_Initialize(appId, ref _handlers, false);

                    _updateThread = new Thread(RunCallbacks)
                    {
                        IsBackground = true, Priority = ThreadPriority.BelowNormal
                    };
                    _updateThread.Start();
                }

                _presence = new DiscordRichPresenceData
                {
                    state          = null,
                    details        = null,
                    startTimestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
                    endTimestamp   = 0,
                    largeImageKey  = null,
                    largeImageText = null,
                    smallImageKey  = null,
                    smallImageText = null,
                    partyId        = null,
                    partySize      = 0,
                    partyMax       = 0,
                    partyPrivacy   = DiscordPartyPrivacy.Private,
                    matchSecret    = null,
                    joinSecret     = null,
                    spectateSecret = null,
                    instance       = 0
                };
                DiscordNative.Discord_UpdatePresence(ref _presence);
            }
        }
示例#3
0
 public static extern void Discord_UpdateHandlers(ref DiscordEventHandlers handlers);