-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DiscordManager.m
118 lines (104 loc) · 3.38 KB
/
DiscordManager.m
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
//
// DiscordManager.m
// Hachidori
//
// Created by 小鳥遊六花 on 1/31/18.
//
#import "DiscordManager.h"
#import <DiscordRPC/DiscordRPC.h>
static const char* APPLICATION_ID = "451384588405571585";
@implementation DiscordManager
- (instancetype)init {
if (self = [super init]) {
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(recieveNotification:) name:@"DiscordStateChanged" object:nil];
if ([NSUserDefaults.standardUserDefaults boolForKey:@"usediscordrichpresence"]) {
[self startDiscordRPC];
}
}
return self;
}
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
}
- (void)recieveNotification:(NSNotification *)notification {
if ([notification.name isEqualToString:@"DiscordStateChanged"]) {
if ([NSUserDefaults.standardUserDefaults boolForKey:@"usediscordrichpresence"]) {
[self startDiscordRPC];
}
else {
[self removePresence];
[self shutdownDiscordRPC];
}
}
}
void InitDiscord()
{
DiscordEventHandlers handlers;
memset(&handlers, 0, sizeof(handlers));
handlers.ready = handleDiscordReady;
handlers.errored = handleDiscordError;
handlers.disconnected = handleDiscordDisconnected;
Discord_Initialize(APPLICATION_ID, &handlers, 1, NULL);
}
static void handleDiscordReady(void)
{
printf("\nDiscord: ready\n");
}
static void handleDiscordDisconnected(int errcode, const char* message)
{
printf("\nDiscord: disconnected (%d: %s)\n", errcode, message);
}
static void handleDiscordError(int errcode, const char* message)
{
printf("\nDiscord: error (%d: %s)\n", errcode, message);
}
- (void)startDiscordRPC {
NSLog(@"Discord Rich Presence enabled");
InitDiscord();
_discordrpcrunning = true;
}
- (void)shutdownDiscordRPC {
NSLog(@"Discord Rich Presence disabled");
Discord_Shutdown();
_discordrpcrunning = false;
}
- (void)UpdatePresence:(NSString *)state withDetails:(NSString *)details {
if ([self checkDiscordRunning]) {
Discord_ClearPresence();
DiscordRichPresence discordPresence;
discordPresence.state = state.UTF8String;
discordPresence.details = details.UTF8String;
discordPresence.startTimestamp = [NSDate date].timeIntervalSince1970;
discordPresence.endTimestamp = [NSDate dateWithTimeIntervalSinceNow:86400].timeIntervalSince1970;
discordPresence.largeImageKey = "default";
discordPresence.smallImageKey = "default";
discordPresence.largeImageText = "";
discordPresence.smallImageText = "";
discordPresence.partyId = NULL;
discordPresence.partySize = 0;
discordPresence.joinSecret = NULL;
discordPresence.spectateSecret = NULL;
discordPresence.matchSecret = NULL;
discordPresence.spectateSecret = NULL;
Discord_UpdatePresence(&discordPresence);
Discord_RunCallbacks();
}
}
- (void)removePresence {
if ([self checkDiscordRunning]) {
Discord_ClearPresence();
Discord_RunCallbacks();
}
}
- (BOOL)checkDiscordRunning {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSArray *runningApps = ws.runningApplications;
NSRunningApplication *a;
for (a in runningApps) {
if ([a.bundleIdentifier isEqualToString:@"com.hnc.Discord"]) {
return true;
}
}
return false;
}
@end