HeavenStudio/Assets/Scripts/DiscordController.cs

110 lines
2.8 KiB
C#
Raw 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;
namespace RhythmHeavenMania.DiscordRPC
{
public class DiscordController : MonoBehaviour
{
public Discord.Discord discord;
public static DiscordController instance { get; set; }
private long lastStartTime;
private void Awake()
{
// instance = this;
}
private void Start()
{
DontDestroyOnLoad(this.gameObject);
}
private void OnApplicationQuit()
{
Disconnect();
}
public void Connect()
{
2022-01-31 02:49:49 +00:00
discord = new Discord.Discord(DiscordRPC.clientID, (System.UInt64)Discord.CreateFlags.NoRequireDiscord);
2022-01-30 23:40:12 +00:00
}
public void Disconnect()
{
if (discord != null)
{
discord.Dispose();
}
}
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",
LargeText = "Rhythm Heaven Mania is based on the Rhythm Heaven series that attempts to recreate every minigame with customizability!"
},
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 (discord != null)
{
discord.RunCallbacks();
}
}
}
}