2017-09-28 16:15:15 +01:00
|
|
|
|
using UnityEngine;
|
2017-08-02 19:31:35 +01:00
|
|
|
|
|
2017-10-17 21:33:12 +01:00
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class DiscordJoinEvent : UnityEngine.Events.UnityEvent<string> { }
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
|
|
|
|
public class DiscordSpectateEvent : UnityEngine.Events.UnityEvent<string> { }
|
|
|
|
|
|
|
|
|
|
[System.Serializable]
|
2018-04-16 18:25:44 +01:00
|
|
|
|
public class DiscordJoinRequestEvent : UnityEngine.Events.UnityEvent<DiscordRpc.DiscordUser> { }
|
2017-10-17 21:33:12 +01:00
|
|
|
|
|
2017-10-14 11:21:35 +01:00
|
|
|
|
public class DiscordController : MonoBehaviour
|
|
|
|
|
{
|
2018-02-22 19:47:18 +00:00
|
|
|
|
public DiscordRpc.RichPresence presence = new DiscordRpc.RichPresence();
|
2017-08-02 19:31:35 +01:00
|
|
|
|
public string applicationId;
|
2017-08-30 23:17:47 +01:00
|
|
|
|
public string optionalSteamId;
|
2017-08-02 19:31:35 +01:00
|
|
|
|
public int callbackCalls;
|
|
|
|
|
public int clickCounter;
|
2018-04-16 18:25:44 +01:00
|
|
|
|
public DiscordRpc.DiscordUser joinRequest;
|
2017-08-03 17:52:43 +01:00
|
|
|
|
public UnityEngine.Events.UnityEvent onConnect;
|
|
|
|
|
public UnityEngine.Events.UnityEvent onDisconnect;
|
2017-12-04 23:16:59 +00:00
|
|
|
|
public UnityEngine.Events.UnityEvent hasResponded;
|
2017-10-17 21:33:12 +01:00
|
|
|
|
public DiscordJoinEvent onJoin;
|
|
|
|
|
public DiscordJoinEvent onSpectate;
|
|
|
|
|
public DiscordJoinRequestEvent onJoinRequest;
|
2017-08-02 19:31:35 +01:00
|
|
|
|
|
2017-08-03 01:52:20 +01:00
|
|
|
|
DiscordRpc.EventHandlers handlers;
|
|
|
|
|
|
2017-08-02 19:31:35 +01:00
|
|
|
|
public void OnClick()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Discord: on click!");
|
|
|
|
|
clickCounter++;
|
|
|
|
|
|
|
|
|
|
presence.details = string.Format("Button clicked {0} times", clickCounter);
|
|
|
|
|
|
2018-02-22 19:47:18 +00:00
|
|
|
|
DiscordRpc.UpdatePresence(presence);
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-04 23:16:59 +00:00
|
|
|
|
public void RequestRespondYes()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Discord: responding yes to Ask to Join request");
|
|
|
|
|
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.Yes);
|
|
|
|
|
hasResponded.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RequestRespondNo()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Discord: responding no to Ask to Join request");
|
|
|
|
|
DiscordRpc.Respond(joinRequest.userId, DiscordRpc.Reply.No);
|
|
|
|
|
hasResponded.Invoke();
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-16 18:25:44 +01:00
|
|
|
|
public void ReadyCallback(ref DiscordRpc.DiscordUser connectedUser)
|
2017-08-02 19:31:35 +01:00
|
|
|
|
{
|
|
|
|
|
++callbackCalls;
|
2018-04-16 18:25:44 +01:00
|
|
|
|
Debug.Log(string.Format("Discord: connected to {0}#{1}: {2}", connectedUser.username, connectedUser.discriminator, connectedUser.userId));
|
2017-08-03 17:52:43 +01:00
|
|
|
|
onConnect.Invoke();
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void DisconnectedCallback(int errorCode, string message)
|
|
|
|
|
{
|
|
|
|
|
++callbackCalls;
|
|
|
|
|
Debug.Log(string.Format("Discord: disconnect {0}: {1}", errorCode, message));
|
2017-08-03 17:52:43 +01:00
|
|
|
|
onDisconnect.Invoke();
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ErrorCallback(int errorCode, string message)
|
|
|
|
|
{
|
|
|
|
|
++callbackCalls;
|
|
|
|
|
Debug.Log(string.Format("Discord: error {0}: {1}", errorCode, message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void JoinCallback(string secret)
|
|
|
|
|
{
|
|
|
|
|
++callbackCalls;
|
|
|
|
|
Debug.Log(string.Format("Discord: join ({0})", secret));
|
2017-10-17 21:33:12 +01:00
|
|
|
|
onJoin.Invoke(secret);
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SpectateCallback(string secret)
|
|
|
|
|
{
|
|
|
|
|
++callbackCalls;
|
|
|
|
|
Debug.Log(string.Format("Discord: spectate ({0})", secret));
|
2017-10-17 21:33:12 +01:00
|
|
|
|
onSpectate.Invoke(secret);
|
2017-10-14 11:21:35 +01:00
|
|
|
|
}
|
2017-10-14 11:12:03 +01:00
|
|
|
|
|
2018-04-16 18:25:44 +01:00
|
|
|
|
public void RequestCallback(ref DiscordRpc.DiscordUser request)
|
2017-10-14 11:21:35 +01:00
|
|
|
|
{
|
|
|
|
|
++callbackCalls;
|
2017-11-30 19:24:12 +00:00
|
|
|
|
Debug.Log(string.Format("Discord: join request {0}#{1}: {2}", request.username, request.discriminator, request.userId));
|
2017-12-04 23:16:59 +00:00
|
|
|
|
joinRequest = request;
|
2017-10-17 21:33:12 +01:00
|
|
|
|
onJoinRequest.Invoke(request);
|
2017-10-14 11:21:35 +01:00
|
|
|
|
}
|
2017-08-02 19:31:35 +01:00
|
|
|
|
|
2017-10-14 11:21:35 +01:00
|
|
|
|
void Start()
|
|
|
|
|
{
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
2017-10-14 11:21:35 +01:00
|
|
|
|
|
|
|
|
|
void Update()
|
|
|
|
|
{
|
2017-08-03 01:52:20 +01:00
|
|
|
|
DiscordRpc.RunCallbacks();
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
2017-10-14 11:21:35 +01:00
|
|
|
|
|
2017-08-02 19:31:35 +01:00
|
|
|
|
void OnEnable()
|
|
|
|
|
{
|
2017-08-03 01:52:20 +01:00
|
|
|
|
Debug.Log("Discord: init");
|
2017-08-02 19:31:35 +01:00
|
|
|
|
callbackCalls = 0;
|
2017-08-03 01:52:20 +01:00
|
|
|
|
|
|
|
|
|
handlers = new DiscordRpc.EventHandlers();
|
|
|
|
|
handlers.readyCallback = ReadyCallback;
|
|
|
|
|
handlers.disconnectedCallback += DisconnectedCallback;
|
|
|
|
|
handlers.errorCallback += ErrorCallback;
|
|
|
|
|
handlers.joinCallback += JoinCallback;
|
|
|
|
|
handlers.spectateCallback += SpectateCallback;
|
2017-10-14 11:21:35 +01:00
|
|
|
|
handlers.requestCallback += RequestCallback;
|
2017-08-30 23:17:47 +01:00
|
|
|
|
DiscordRpc.Initialize(applicationId, ref handlers, true, optionalSteamId);
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Discord: shutdown");
|
|
|
|
|
DiscordRpc.Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OnDestroy()
|
|
|
|
|
{
|
2017-10-14 11:21:35 +01:00
|
|
|
|
|
2017-08-02 19:31:35 +01:00
|
|
|
|
}
|
|
|
|
|
}
|