forked from notnotnotswipez/BonelabMultiplayerMockup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscordIntegration.cs
128 lines (103 loc) · 3.44 KB
/
DiscordIntegration.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Collections.Generic;
using System.Linq;
using Discord;
using MelonLoader;
namespace BonelabMultiplayerMockup
{
public class DiscordIntegration
{
private static Discord.Discord discord;
public static UserManager userManager;
public static ActivityManager activityManager;
public static LobbyManager lobbyManager;
public static User currentUser;
public static Activity activity;
public static Lobby lobby;
// Boilerplate connection code, thanks Entanglement.
public static Dictionary<byte, long> byteIds = new Dictionary<byte, long>();
public static byte localByteId = 0;
public static byte lastByteId = 1;
public static bool hasLobby => lobby.Id != 0;
public static bool isHost => hasLobby && lobby.OwnerId == currentUser.Id;
public static bool isConnected => hasLobby && lobby.OwnerId != currentUser.Id;
public static void Init()
{
discord = new Discord.Discord(1026695411144081518, (ulong)CreateFlags.Default);
userManager = discord.GetUserManager();
activityManager = discord.GetActivityManager();
lobbyManager = discord.GetLobbyManager();
userManager.OnCurrentUserUpdate += () =>
{
currentUser = userManager.GetCurrentUser();
MelonLogger.Msg($"Current Discord User: {currentUser.Username}");
};
DefaultRichPresence();
}
public static void Update()
{
discord.RunCallbacks();
}
public static void Flush()
{
lobbyManager.FlushNetwork();
}
public static void Tick()
{
Update();
Flush();
}
public static void DefaultRichPresence()
{
activity = new Activity
{
State = "Playing alone",
Details = "Not connected to a server",
Instance = true,
Assets =
{
LargeImage = "blmp"
}
};
activity.Instance = false;
UpdateActivity();
}
public static void RegisterUser(long userId, byte byteId)
{
if (byteIds.ContainsKey(byteId)) return;
MelonLogger.Msg("Registered " + userId + " to byte id: " + byteId);
byteIds.Add(byteId, userId);
}
public static byte CreateByteId()
{
return lastByteId++;
}
public static void RemoveUser(long userId)
{
byteIds.Remove(GetByteId(userId));
}
public static byte GetByteId(long longId)
{
if (longId == currentUser.Id) return localByteId;
return byteIds.FirstOrDefault(o => o.Value == longId).Key;
}
public static long GetLongId(byte shortId)
{
if (shortId == 0) return lobby.OwnerId;
if (byteIds.ContainsKey(shortId))
{
return byteIds[shortId];
}
return 1278;
}
public static byte RegisterUser(long userId)
{
var byteId = CreateByteId();
RegisterUser(userId, byteId);
return byteId;
}
public static void UpdateActivity()
{
activityManager.UpdateActivity(activity, result => { });
}
}
}