HeavenStudio/Assets/Scripts/DiscordController.cs

110 lines
2.7 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;
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 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",
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 (discord != null)
{
discord.RunCallbacks();
}
}
}
}