forked from WolvenKit/WolvenKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord.cs
144 lines (120 loc) · 4.37 KB
/
discord.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace SharpPresence
{
class Discord
{
public struct EventHandlers
{
public IntPtr ready;
public IntPtr disconnected;
public IntPtr errored;
public IntPtr joinGame;
public IntPtr spectateGame;
public IntPtr joinRequest;
}
//--------------------------------------------------------------------------------
#pragma warning disable CS0649 // unused variables
public struct RichPresence
{
public string state;
public string details;
public Int64 startTimestamp;
public Int64 endTimestamp;
public string largeImageKey;
public string largeImageText;
public string smallImageKey;
public string smallImageText;
public string partyId;
public int partySize;
public int partyMax;
public string matchSecret;
public string joinSecret;
public string spectateSecret;
public sbyte instance;
}
//--------------------------------------------------------------------------------
public struct JoinRequest
{
public string userId;
public string username;
public string avatar;
}
#pragma warning restore CS0649 // unused variables
//--------------------------------------------------------------------------------
public enum Reply : int
{
No = 0,
Yes = 1,
Ignore = 2
}
//--------------------------------------------------------------------------------
/// <summary>
/// Initializes discord rich presence
/// </summary>
/// <param name="applicationID"></param>
/// <param name="handlers"></param>
/// <param name="autoRegister"></param>
/// <param name="optionalSteamId"></param>
[DllImport("discord-rpc.dll")]
private static extern void Discord_Initialize([MarshalAs(UnmanagedType.LPStr)]string applicationID,
ref EventHandlers handlers,
int autoRegister,
[MarshalAs(UnmanagedType.LPStr)]string optionalSteamId);
public static void Initialize(string appID, EventHandlers handlers)
{
try
{
Discord_Initialize(appID, ref handlers, 1, String.Empty);
}
catch
{ }
}
//--------------------------------------------------------------------------------
[DllImport("discord-rpc.dll")]
private static extern void Discord_UpdatePresence(IntPtr presence);
public static void UpdatePresence(RichPresence presence)
{
try
{
IntPtr ptrPresence = Marshal.AllocHGlobal(Marshal.SizeOf(presence));
Marshal.StructureToPtr(presence, ptrPresence, false);
Discord_UpdatePresence(ptrPresence);
}
catch
{ }
}
//--------------------------------------------------------------------------------
[DllImport("discord-rpc.dll")]
private static extern void Discord_Shutdown();
public static void Shutdown()
{
Discord_Shutdown();
}
//--------------------------------------------------------------------------------
[DllImport("discord-rpc.dll")]
private static extern void Discord_UpdateConnection();
public static void UpdateConnection()
{
Discord_UpdateConnection();
}
//--------------------------------------------------------------------------------
[DllImport("discord-rpc.dll")]
private static extern void Discord_RunCallbacks();
public static void RunCallbacks()
{
Discord_RunCallbacks();
}
//--------------------------------------------------------------------------------
[DllImport("discord-rpc.dll")]
private static extern void Discord_Respond(string userId, int reply);
public static void Respond(string userID, Reply reply)
{
Discord_Respond(userID, (int)reply);
}
}
}