-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.js
More file actions
62 lines (56 loc) · 2.79 KB
/
Copy pathredis.js
File metadata and controls
62 lines (56 loc) · 2.79 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
const { createClient } = require('redis');
const { getDB } = require('./dbSetup.js');
const Discord = require('discord.js');
const botSettings = require('./settings.json');
const { commands } = require('./lib/commands');
let client;
/* The MockMessage class is a JavaScript class that represents a message in a webhook interaction and
provides methods for deleting and editing the message. */
class MockMessage {
constructor(webhook, interaction) {
this.webhook = webhook;
this.interaction = interaction;
}
async delete() {
return await this.webhook.deleteMessage(this.interaction.message.id);
}
async edit(opts) {
return await this.webhook.editMessage(this.interaction.message, opts);
}
}
module.exports = {
/* The `async setup()` function is responsible for setting up the Redis client connection. */
async setup() {
client = createClient(botSettings.redis);
client.on('error', err => console.log('Redis Client Error: ', err));
await client.connect();
},
async createReactionRow(message, commandName, callback, buttons, allowedUser, state) {
// eslint-disable-next-line camelcase
let opts = { valid_ids: JSON.stringify(buttons.components.map((c) => c.data.custom_id)), command: commandName, callback, state: JSON.stringify(state) };
if (message instanceof Discord.InteractionResponse) opts = { token: message.interaction.token, whid: message.interaction.webhook.id, ...opts };
if (allowedUser) opts.allowedUser = allowedUser.id;
const key = 'messagebuttons:' + (await message.fetch()).id;
// eslint-disable-next-line new-cap
await client.multi().HSET(key, opts).EXPIRE(key, 86400 /* 1 day */).exec();
},
async handleReactionRow(bot, interaction) {
if (!(interaction instanceof Discord.ButtonInteraction)) return false;
// eslint-disable-next-line new-cap
const data = await client.HGETALL('messagebuttons:' + interaction.message.id);
if (!data.command || !data.callback) return false;
if (data.allowedUser && data.allowedUser != interaction.user.id) return false;
const command = commands.get(data.command) || commands.find(cmd => cmd.alias && cmd.alias.includes(data.command));
const callback = command[data.callback];
const db = getDB(interaction.guild.id);
if (data.token) {
const resp = new MockMessage(new Discord.InteractionWebhook(bot, data.whid, data.token), interaction);
await callback(bot, resp, db, interaction.customId, JSON.parse(data.state));
} else {
const resp = interaction.message;
resp.interaction = interaction;
await callback(bot, resp, db, interaction.customId, JSON.parse(data.state));
}
return true;
}
};