HeavenStudioPlus/Assets/Scripts/DiscordController.cs

114 lines
2.8 KiB
C#
Raw Permalink Normal View History

2022-01-30 23:40:12 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Text;
using Discord;
using System;
2022-03-14 14:21:05 +00:00
namespace HeavenStudio.DiscordRPC
2022-01-30 23:40:12 +00:00
{
public class DiscordController : MonoBehaviour
{
public Discord.Discord discord;
public static DiscordController instance { get; set; }
private long lastStartTime;
private bool quitting;
2022-01-30 23:40:12 +00:00
private void Awake()
{
}
private void Start()
{
DontDestroyOnLoad(this.gameObject);
instance = this;
2022-01-30 23:40:12 +00:00
}
// private void OnApplicationQuit()
// {
// quitting = true;
// }
2022-01-30 23:40:12 +00:00
public void Connect()
{
2022-01-31 02:49:49 +00:00
discord = new Discord.Discord(DiscordRPC.clientID, (System.UInt64)Discord.CreateFlags.NoRequireDiscord);
quitting = false;
2022-01-30 23:40:12 +00:00
}
public void Disconnect()
{
if (discord != null)
{
discord.Dispose();
quitting = true;
2022-01-30 23:40:12 +00:00
}
}
public void UpdateActivity(string stateText, string stateDetails, bool updateTime = false)
{
var activityManager = discord.GetActivityManager();
var activity = new Activity { };
activity = new Activity
{
State = stateText,
Details = stateDetails,
Assets =
{
LargeImage = "logo",
2022-02-23 03:23:56 +00:00
LargeText = "Together now!"
2022-01-30 23:40:12 +00:00
},
Instance = true,
};
if (updateTime == true)
{
lastStartTime = DateTimeOffset.Now.ToUnixTimeSeconds();
activity.Timestamps.Start = lastStartTime;
}
else
{
activity.Timestamps.Start = lastStartTime;
}
activityManager.UpdateActivity(activity, (result) => {
if (result == Discord.Result.Ok)
{
Debug.Log("Update Success!");
}
else
{
Debug.Log("Update Failed");
}
});
}
public void ClearActivity()
{
var activityManager = discord.GetActivityManager();
activityManager.ClearActivity((result) => {
if (result == Discord.Result.Ok)
{
Debug.Log("Clear Success!");
}
else
{
Debug.Log("Clear Failed");
}
});
}
void Update()
{
if ((!quitting) && discord != null)
2022-01-30 23:40:12 +00:00
{
discord.RunCallbacks();
}
}
}
}