-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebhookSocket.mjs
More file actions
71 lines (59 loc) · 2.83 KB
/
webhookSocket.mjs
File metadata and controls
71 lines (59 loc) · 2.83 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
import fs from "fs";
import path from "path";
import { generateId, validateMemberId } from "../../../modules/functions/main.mjs";
import { hasPermission } from "../../../modules/functions/chat/main.mjs";
const WEBHOOKS_FILE = path.join(process.cwd(), "configs", "webhooks.json");
export default (socket) => {
socket.on('getWebhooks', (data, response) => {
if (validateMemberId(data?.id, socket, data?.token) === true) {
if (!hasPermission(data.id, "manageChannels", data.channelId)) {
return response({ type: 'error', error: "No permission" });
}
const channelId = data.channelId;
const webhooks = fs.existsSync(WEBHOOKS_FILE) ? JSON.parse(fs.readFileSync(WEBHOOKS_FILE, 'utf-8')) : {};
let channelWebhooks = Object.values(webhooks).filter(wh => wh.channel === channelId);
response({ type: 'success', data: channelWebhooks });
}
});
socket.on('createWebhook', (data, response) => {
if (validateMemberId(data?.id, socket, data?.token) === true) {
if (!hasPermission(data.id, "manageChannels", data.channelId)) {
return response({ type: 'error', error: "No permission" });
}
const group = data.group;
const category = data.category;
const channelId = data.channelId;
const name = data.name || "New Webhook";
const webhooks = fs.existsSync(WEBHOOKS_FILE) ? JSON.parse(fs.readFileSync(WEBHOOKS_FILE, 'utf-8')) : {};
let whId = generateId(8);
let whToken = generateId(32);
let newWh = {
id: whId,
token: whToken,
group: group,
category: category,
channel: channelId,
name: name,
avatar: ""
};
webhooks[whId] = newWh;
fs.writeFileSync(WEBHOOKS_FILE, JSON.stringify(webhooks, null, 2));
response({ type: 'success', data: newWh });
}
});
socket.on('deleteWebhook', (data, response) => {
if (validateMemberId(data?.id, socket, data?.token) === true) {
if (!hasPermission(data.id, "manageChannels", data.channelId)) {
return response({ type: 'error', error: "No permission" });
}
const webhooks = fs.existsSync(WEBHOOKS_FILE) ? JSON.parse(fs.readFileSync(WEBHOOKS_FILE, 'utf-8')) : {};
if (webhooks[data.webhookId]) {
delete webhooks[data.webhookId];
fs.writeFileSync(WEBHOOKS_FILE, JSON.stringify(webhooks, null, 2));
response({ type: 'success' });
} else {
response({ type: 'error', error: "Not found" });
}
}
});
};