forked from swishs-client-mod-plugins/grammar-nazi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
220 lines (185 loc) · 8.11 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const { Plugin } = require("powercord/entities");
const { inject, uninject } = require("powercord/injector");
const { getModule, messages, getModuleByDisplayName, React } = require("powercord/webpack");
const { findInReactTree } = require("powercord/util");
const { receiveMessage } = messages;
const HeaderBarButton = require("./components/HeaderBarButton");
const TextContainerButton = require("./components/TextContainerButton");
module.exports = class GrammarNazi extends Plugin {
async import(module, key = module) {
this[key] = (await getModule([module]))[key];
}
get regex() {
const dictionary = this.settings.get("customDictionary", {});
const keywords = Object.keys(dictionary).map(k => k.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
if (!keywords.length) return null;
return new RegExp(`(^|(?<=[^A-Z0-9]+))(${keywords.join("|")})((?=[^A-Z0-9]+)|$)`, "gi");
}
async startPlugin() {
await this.import("BOT_AVATARS");
await this.import("createBotMessage");
await this.import("getLastSelectedChannelId", "getChannelId");
powercord.api.settings.registerSettings("GrammarNazi", {
category: this.entityID,
label: "GrammarNazi",
render: require("./components/Settings")
});
powercord.api.commands.registerCommand({
command: "addword",
aliases: ["aw", "aword"],
description: "Add a key/value pair to the custom dictionary.",
usage: '{c} "key" "value"',
executor: this.addWord.bind(this)
});
powercord.api.commands.registerCommand({
command: "removeword",
aliases: ["rm", "rmword"],
description: "Remove a key/value pair from the custom dictionary.",
usage: '{c} "key"',
executor: this.removeWord.bind(this),
autocomplete: args => {
return {
header: "Please specify which keyword you would like to remove",
commands: Object.keys(this.settings.get("customDictionary"))
.filter(k => k.includes(args.join(" ").toLowerCase()))
.map(k => ({ command: k }))
};
}
});
powercord.api.commands.registerCommand({
command: "listwords",
aliases: ["lw", "dictionary", "dict"],
description: "View the current custom dictionary.",
usage: "{c}",
executor: this.listWords.bind(this)
});
this.loadStylesheet("style.css");
this.settings.set("customDictionary", this.settings.get("customDictionary", {}));
["nazify", "punctuation", "capitalization", "dictionary", "location"].forEach(s => this.settings.set(s, this.settings.get(s, false)));
inject(
"message-send",
messages,
"sendMessage",
args => {
let { content } = args[1];
const dict = this.settings.get("customDictionary", {});
if (!content.includes("```") && this.settings.get("nazify") && /\w/.test(content.charAt(0))) {
if (this.settings.get("dictionary")) {
const re = this.regex;
if (re !== null)
content = content.replace(this.regex, m => {
return dict[m.toLowerCase()] ?? m;
});
}
if (this.settings.get("punctuation") && /[A-Z0-9]/i.test(content.charAt(content.length - 1))) {
if (!content.startsWith("http", content.lastIndexOf(" ") + 1)) content += ".";
}
if (this.settings.get("capitalization") && !content.startsWith("http")) content = content.charAt(0).toUpperCase() + content.slice(1);
}
args[1].content = content;
return args;
},
true
);
const ChannelTextAreaContainer = await getModule(m => m.type && m.type.render && m.type.render.displayName === "ChannelTextAreaContainer");
inject("chat-button", ChannelTextAreaContainer.type, "render", (_, res) => {
if (this.settings.get("location") !== "channel-text-area-container") return res;
const props = findInReactTree(res, r => r && r.className && r.className.indexOf("buttons-") == 0);
props.children.unshift(
React.createElement(TextContainerButton, {
settings: this.settings
})
);
return res;
});
ChannelTextAreaContainer.type.render.displayName = "ChannelTextAreaContainer";
const HeaderBarContainer = await getModuleByDisplayName("HeaderBarContainer");
inject("header-bar", HeaderBarContainer.prototype, "render", (_, res) => {
if (this.settings.get("location") === "header-bar-container") {
res.props.toolbar.props.children.unshift(
React.createElement(HeaderBarButton, {
settings: this.settings
})
);
}
return res;
});
}
pluginWillUnload() {
powercord.api.commands.unregisterCommand("addword");
powercord.api.commands.unregisterCommand("removeword");
powercord.api.commands.unregisterCommand("listwords");
powercord.api.settings.unregisterSettings("GrammarNazi");
uninject("message-send");
uninject("chat-button");
uninject("header-bar");
document.querySelectorAll(".toggle-button").forEach(e => (e.style.display = "none"));
}
async addWord(args) {
const receivedMessage = this.createBotMessage(this.getChannelId(), {});
this.BOT_AVATARS.GrammarNaziAvatar = "https://i.imgur.com/wUcHvh0.png";
receivedMessage.author.username = "Grammar Nazi";
receivedMessage.author.avatar = "GrammarNaziAvatar";
let parsed;
if (args.length === 2) {
parsed = args;
} else {
parsed = args
.join(" ")
.match(/\"(.+?)\"\s*"(.+?)"/)
?.slice(1);
console.log(parsed);
console.log(args);
}
if (!parsed || !parsed[0].length || !parsed[1].length) {
receivedMessage.content =
"Insufficient arguments; both a keyword and value must be supplied. You must wrap both key and value in double quotes if they contain spaces.";
return receiveMessage(receivedMessage.channel_id, receivedMessage);
}
let [key, value] = parsed;
key = key.toLowerCase();
/* Duplicate Check */
const customDictionary = this.settings.get("customDictionary");
if (customDictionary.hasOwnProperty(key)) {
receivedMessage.content = `Entry "${key}" already exists!`;
} else {
customDictionary[key] = value;
this.settings.set("customDictionary", customDictionary);
receivedMessage.content = `Entry "${key}" successfully created with value of "${value}".`;
}
return receiveMessage(receivedMessage.channel_id, receivedMessage);
}
async removeWord(args) {
const receivedMessage = this.createBotMessage(this.getChannelId(), {});
this.BOT_AVATARS.GrammarNaziAvatar = "https://i.imgur.com/wUcHvh0.png";
receivedMessage.author.username = "Grammar Nazi";
receivedMessage.author.avatar = "GrammarNaziAvatar";
const customDictionary = this.settings.get("customDictionary");
const key = args.join(" ").replace(/"/gm, "");
if (customDictionary.hasOwnProperty(key)) {
delete customDictionary[key];
this.settings.set("customDictionary", customDictionary);
receivedMessage.content = `Entry \`${key}\` was successfully deleted!`;
} else {
receivedMessage.content = `No such entry: \`${key}\``;
}
return receiveMessage(receivedMessage.channel_id, receivedMessage);
}
async listWords() {
const receivedMessage = this.createBotMessage(this.getChannelId(), {});
this.BOT_AVATARS.GrammarNaziAvatar = "https://i.imgur.com/wUcHvh0.png";
receivedMessage.author.username = "Grammar Nazi";
receivedMessage.author.avatar = "GrammarNaziAvatar";
const customDictionary = this.settings.get("customDictionary");
const entries = Object.entries(customDictionary);
if (!entries.length) receivedMessage.content = "Your dictionary is empty!";
else {
let dictionary = ">>> ";
for (const [key, val] of entries) {
dictionary += `${key} -> ${val}\n`;
}
receivedMessage.content = dictionary;
}
return receiveMessage(receivedMessage.channel_id, receivedMessage);
}
};