forked from MelonEnjoyers/MelonLoaderDiscordStatus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordStatusPlugin.cs
107 lines (86 loc) · 2.99 KB
/
DiscordStatusPlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using MelonLoader;
using System;
using Discord;
using System.Threading;
[assembly: MelonInfo(typeof(DiscordStatus.DiscordStatusPlugin), "Discord Status", "1.0.0", "SlidyDev")]
[assembly: MelonColor(ConsoleColor.DarkCyan)]
namespace DiscordStatus
{
public class DiscordStatusPlugin : MelonPlugin
{
public const long AppId = 977473789854089226;
public Discord.Discord discordClient;
public ActivityManager activityManager;
private bool gameClosing;
public bool GameStarted { get; private set; }
public long gameStartedTime;
public override void OnPreInitialization()
{
DiscordLibraryLoader.LoadLibrary();
InitializeDiscord();
UpdateActivity();
new Thread(DiscordLoopThread).Start();
}
public override void OnApplicationLateStart()
{
GameStarted = true;
gameStartedTime = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
UpdateActivity();
}
public override void OnApplicationQuit()
{
gameClosing = true;
}
public void DiscordLoopThread()
{
for (; ; )
{
if (gameClosing)
break;
discordClient.RunCallbacks();
Thread.Sleep(200);
}
}
public void InitializeDiscord()
{
discordClient = new Discord.Discord(AppId, (ulong)CreateFlags.NoRequireDiscord);
discordClient.SetLogHook(LogLevel.Debug, DiscordLogHandler);
activityManager = discordClient.GetActivityManager();
}
private void DiscordLogHandler(LogLevel level, string message)
{
switch (level)
{
case LogLevel.Info:
case LogLevel.Debug:
LoggerInstance.Msg(message);
break;
case LogLevel.Warn:
LoggerInstance.Warning(message);
break;
case LogLevel.Error:
LoggerInstance.Error(message);
break;
}
}
public void UpdateActivity()
{
var activity = new Activity
{
Details = $"Playing {MelonUtils.CurrentGameAttribute.Name}"
};
activity.Assets.LargeImage = "ml_icon";
activity.Name = $"MelonLoader {BuildInfo.Version}";
activity.Instance = true;
activity.Assets.LargeText = activity.Name;
var modsCount = MelonHandler.Mods.Count;
activity.State = GameStarted ? $"{modsCount} {(modsCount == 1 ? "Mod" : "Mods")} Loaded" : "Loading MelonLoader";
if (GameStarted)
activity.Timestamps.Start = gameStartedTime;
activityManager.UpdateActivity(activity, ResultHandler);
}
public void ResultHandler(Result result)
{
}
}
}