forked from Mimickal/ReactionRoleBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.js
More file actions
128 lines (114 loc) · 3.54 KB
/
cache.js
File metadata and controls
128 lines (114 loc) · 3.54 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
/*******************************************************************************
* This file is part of ReactionRoleBot, a role-assigning Discord bot.
* Copyright (C) 2020 Mimickal (Mia Moretti).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
******************************************************************************/
const database = require('./database');
// The live cache of selected messages for each user.
const selectedMessages = new Map();
/**
* Selects a message to use for each subsequent command the user enters.
*/
function selectMessage(user_id, message) {
selectedMessages.set(user_id, message);
// TODO clear selected cache after some time
}
/**
* Gets the message the given user currently has selected.
*
* Throws an exception if the user has no message selected.
*/
function getSelectedMessage(user_id) {
let message = selectedMessages.get(user_id);
if (!message) {
throw new Error('No message selected!');
}
return message;
}
/**
* Clears the selected message for the given user.
*/
function clearSelectedMessage(user_id) {
selectedMessages.delete(user_id);
}
/**
* Add an emoji-role association to the message the user has selected.
* If the emoji was already associated with another role on this message, the
* original mapping will be overwritten.
*
* Throws an exception if the user has no message selected.
*/
async function addEmojiRole(user_id, emoji_id, role_id) {
let message = await getSelectedMessage(user_id);
return database.addRoleReact({
guild_id: message.guild.id,
message_id: message.id,
emoji_id: emoji_id,
role_id: role_id
});
}
/**
* Remove an emoji-role association from the message the user has selected.
*
* Throws an exception:
* - If the user has no message selected.
* - If the message did not have a mapping for the given emoji.
*/
async function removeEmojiRole(user_id, emoji_id) {
let message = getSelectedMessage(user_id);
let args = {
message_id: message.id,
emoji_id: emoji_id
};
let role_id = await database.getRoleReact(args);
if (role_id) {
return database.removeRoleReact(args);
} else {
throw new Error('No role mapping found');
}
}
/**
* Removes all emoji-role associations from the message the user has selected.
*
* Throws an exception:
* - If the user has no message selected.
* - If the message did not have any emojis mappings on it.
*/
async function removeAllEmojiRoles(user_id) {
let message = getSelectedMessage(user_id);
let rows = await database.removeAllRoleReacts(message.id);
if (rows === 0) {
throw new Error('No role mapping found');
}
return message;
}
/**
* Gets the role mapped to the given emoji on the given message, or null if
* there's no role associated with it (or if the message is unknown).
*/
function getReactRole(message_id, emoji_id) {
return database.getRoleReact({
message_id: message_id,
emoji_id: emoji_id
});
}
module.exports = {
selectMessage,
getSelectedMessage,
clearSelectedMessage,
addEmojiRole,
removeEmojiRole,
removeAllEmojiRoles,
getReactRole
};