-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvenBetterDiscord.plugin.js
155 lines (125 loc) · 3.87 KB
/
EvenBetterDiscord.plugin.js
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
145
146
147
148
149
150
151
152
153
154
155
//META{"name":"EvenBetterDiscord"}*//
let request = require('request');
let fs = require('fs');
let EvenBetterDiscord = function ()
{
this.targetAPI = "https://api.github.com/repos/DeathStrikeV/BetterDiscordApp/commits/master";
this.targetURL = "https://raw.githubusercontent.com/DeathStrikeV/BetterDiscordApp/master/data/emotedata_ffz.json";
this.bdPath = (process.platform == "win32" ?
process.env.APPDATA : process.platform == "darwin" ?
process.env.HOME + "/Library/Preferences/" : "/var/local/") + "/BetterDiscord/";
this.preferencesFile = this.bdPath + "/EBD_Preferences.json";
this.emotesFile = this.bdPath + "/EBD_Emotes.json";
this.defaultEmotesFile = this.bdPath + "/emotes_ffz.json";
};
EvenBetterDiscord.prototype.start = function ()
{
this.loadEBDFiles();
};
EvenBetterDiscord.prototype.loadEBDFiles = function ()
{
let readPreferencesFile = function (err, data)
{
if (data)
this.currentHash = JSON.parse(data).currentHash;
else
this.currentHash = "";
if (this.currentHash === "ignore")
{
this.loadEmotes(this.emotesFile);
return;
}
request({ url: this.targetAPI, headers: { "User-Agent": "DeathStrikeV" } }, requestRepoHash.bind(this));
};
let requestRepoHash = function (error, response, body)
{
if (response.statusCode !== 200)
{
this.loadEmotes(this.emotesFile);
return;
}
this.latestHash = JSON.parse(body).sha;
if (this.currentHash === this.latestHash)
{
this.loadEmotes(this.emotesFile);
return;
}
request(this.targetURL, downloadEmotesFile.bind(this));
};
let downloadEmotesFile = function (error, response, body)
{
if (response.statusCode !== 200)
{
this.loadEmotes(this.emotesFile);
return;
}
fs.writeFile(this.emotesFile, body, saveEmotesFile.bind(this));
};
let saveEmotesFile = function (err)
{
if (!err)
{
//console.log("Retreived new emotes file!");
fs.writeFile(this.preferencesFile, JSON.stringify({ currentHash: this.latestHash }), null);
}
this.loadEmotes(this.emotesFile);
}
fs.readFile(this.preferencesFile, "utf8", readPreferencesFile.bind(this));
};
EvenBetterDiscord.prototype.loadEmotes = function (targetFile)
{
fs.readFile(targetFile, "utf8", (err, data) =>
{
if (data)
{
let emoteData = JSON.parse(data);
if (!emoteData)
{
//console.log("Failed to parse emotes file!");
return;
}
window.emotesFfz = emoteData;
//console.log("Loaded emotes file!");
}
});
};
EvenBetterDiscord.prototype.stop = function ()
{
this.removeEBDFiles();
this.loadEmotes(this.defaultEmotesFile);
};
EvenBetterDiscord.prototype.removeEBDFiles = function ()
{
fs.stat(this.preferencesFile, (err, stats) =>
{
if (err)
return;
fs.unlink(this.preferencesFile, (err) => { });
});
fs.stat(this.emotesFile, (err, stats) =>
{
if (err)
return;
fs.unlink(this.emotesFile, (err) => { });
});
};
EvenBetterDiscord.prototype.load = function () { };
EvenBetterDiscord.prototype.unload = function () { };
EvenBetterDiscord.prototype.onMessage = function () { };
EvenBetterDiscord.prototype.onSwitch = function () { };
EvenBetterDiscord.prototype.getName = function ()
{
return "EvenBetterDiscord";
};
EvenBetterDiscord.prototype.getDescription = function ()
{
return "Loads FFZ emotes from alternate repository";
};
EvenBetterDiscord.prototype.getVersion = function ()
{
return "1.0.0";
};
EvenBetterDiscord.prototype.getAuthor = function ()
{
return "DeathStrikeV";
};