-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreplyPrefix.js
More file actions
183 lines (156 loc) · 5.42 KB
/
replyPrefix.js
File metadata and controls
183 lines (156 loc) · 5.42 KB
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
module.exports = async function ({ config, commands, knex, formats }) {
const moment = require("moment");
const pluginVersion = 1;
const TABLE_NAME = "replyPrefix";
const prefixMap = new Map();
async function populatePrefixMap() {
const prefixRows = await knex(TABLE_NAME).select().whereNot({
user_id: "version",
});
for (const row of prefixRows) {
prefixMap.set(row.user_id, row.prefix);
}
}
/**
* Sets prefix of specified user
* @param {String} userId
* @param {String} prefix
* @returns {Promise<boolean>}
*/
async function setPrefix(userId, prefix) {
if (prefix.length > 48) return false;
try {
if (prefixMap.has(userId)) {
await knex(TABLE_NAME)
.update({
prefix,
})
.where({
user_id: userId,
});
} else {
await knex(TABLE_NAME).insert({
user_id: userId,
prefix,
});
}
prefixMap.set(userId, prefix);
} catch (e) {
console.log(e);
return false;
}
return true;
}
/**
* Removes prefix of specified user
* @param {String} userId
* @returns {Promise<boolean>}
*/
async function removePrefix(userId) {
try {
await knex(TABLE_NAME)
.where({
user_id: userId,
})
.delete();
} catch (e) {
console.log(e);
return false;
}
prefixMap.delete(userId);
return true;
}
const setPrefixCmd = async function (msg, args) {
const oldPrefix = prefixMap.has(msg.author.id) ? prefixMap.get(msg.author.id) : "None";
const success = setPrefix(msg.author.id, args.prefix);
if (success) {
msg.channel.createMessage({ content: `Changed your prefix from \`${oldPrefix}\` to \`${args.prefix}\``, messageReferenceID: msg.id });
} else {
msg.channel.createMessage(
{ content: "Failed to set prefix, make sure your prefix is below 48 chars and if it is, check the console for errors", messageReferenceID: msg.id },
);
}
};
const removePrefixCmd = async function (msg) {
const success = removePrefix(msg.author.id);
if (success) {
msg.channel.createMessage({ content: "Removed your prefix", messageReferenceID: msg.id });
} else {
msg.channel.createMessage({ content: "Failed to remove your prefix, please check the console for errors", messageReferenceID: msg.id });
}
};
const dmPrefixFormatter = function (threadMessage) {
const roleName = threadMessage.role_name || config.fallbackRoleName;
const prefix = prefixMap.has(threadMessage.user_id) ? prefixMap.get(threadMessage.user_id) : "";
const modInfo = threadMessage.is_anonymous
? roleName
: roleName
? `(${roleName}) ${prefix}${threadMessage.user_name}`
: `${prefix}${threadMessage.user_name}`;
return modInfo ? `**${modInfo}:** ${threadMessage.body}` : threadMessage.body;
};
const threadPrefixFormatter = function (threadMessage) {
const roleName = threadMessage.role_name || config.fallbackRoleName;
const prefix = prefixMap.has(threadMessage.user_id) ? prefixMap.get(threadMessage.user_id) : "";
const modInfo = threadMessage.is_anonymous
? roleName
? `(Anonymous) (${threadMessage.user_name}) ${roleName}`
: `(Anonymous) (${threadMessage.user_name})`
: roleName
? `(${roleName}) ${prefix}${threadMessage.user_name}`
: `${prefix}${threadMessage.user_name}`;
let result = modInfo ? `**${modInfo}:** ${threadMessage.body}` : threadMessage.body;
if (config.threadTimestamps) {
const formattedTimestamp = moment.utc(threadMessage.created_at).format("HH:mm");
result = `[${formattedTimestamp}] ${result}`;
}
result = `\`${threadMessage.message_number}\` ${result}`;
return result;
};
//#region registering
// Register all commands and formatters
commands.addInboxServerCommand("setPrefix", [{ name: "prefix", type: "string", required: true }], setPrefixCmd);
commands.addInboxServerCommand("removePrefix", [], removePrefixCmd);
formats.setStaffReplyDMFormatter(dmPrefixFormatter);
formats.setStaffReplyThreadMessageFormatter(threadPrefixFormatter);
//#endregion
//#region migrations
// If table doesnt exist, create it and insert version
const runMigrations = async function () {
console.log("[ReplyPrefix] Running Migrations...");
let versionInTable = 1;
if (!(await knex.schema.hasTable(TABLE_NAME))) {
await knex.schema.createTable(TABLE_NAME, (table) => {
table.string("user_id", 20);
table.string("prefix", 48);
table.primary(["user_id"]);
});
await knex(TABLE_NAME).insert({
user_id: "version",
prefix: "1",
});
} else {
versionInTable = parseInt(await knex(TABLE_NAME).where("user_id", "version").first().prefix);
}
switch (versionInTable) {
// case 1: Migrations for V2 go here
// case 2: Migrations for V3 go here
// case 3: You get it
default:
// Always update version as last step
await knex(TABLE_NAME)
.update({
prefix: pluginVersion,
})
.where({
user_id: "version",
});
break;
}
console.log("[ReplyPrefix] Migrations Finished!");
};
await runMigrations();
await populatePrefixMap();
console.log(`[ReplyPrefix] Loaded ${prefixMap.size} prefix${prefixMap.size === 1 ? "" : "es"}`);
//#endregion migrations
};