-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDiscord.cs
48 lines (40 loc) · 1.35 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
using System.Runtime.InteropServices;
using AltV.Net.CApi.ClientEvents;
using AltV.Net.Shared.Utils;
namespace AltV.Net.Client
{
public class Discord
{
private readonly ICore core;
public Discord(ICore core)
{
this.core = core;
}
public async Task<string> RequestOAuth2Token(string appId)
{
GCHandle handle;
bool resultSuccess = false;
string data = null;
var semaphore = new SemaphoreSlim(0, 1);
unsafe
{
void ResolveTask(byte success, string token)
{
resultSuccess = success == 1;
data = token;
semaphore.Release();
}
DiscordOAuth2TokenResultModuleDelegate resolveTask = ResolveTask;
handle = GCHandle.Alloc(resolveTask);
var appIdPtr = MemoryUtils.StringToHGlobalUtf8(appId);
core.Library.Client.Core_Discord_GetOAuth2Token(core.NativePointer, appIdPtr, resolveTask);
Marshal.FreeHGlobal(appIdPtr);
}
await semaphore.WaitAsync();
handle.Free();
semaphore.Dispose();
if (!resultSuccess) throw new Exception("Failed to request OAuth2 token");
return data;
}
}
}