This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_state.js
91 lines (84 loc) · 2.99 KB
/
voice_state.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
registerPlugin({
name: 'Discord Voice State',
version: '2.0.0',
description: 'Caches the voice states',
author: 'Lala Sabathil <[email protected]>',
engine: '>= 1.0.0',
backends: ['discord'],
requiredModules: [],
autorun: true,
vars: []
}, function(_, config, meta) {
var event = require('event');
var engine = require('engine');
var backend = require('backend');
var store = require('store');
const errorMessages = {
NotLoggedIn: "You are not logged in",
NotBound: "This account is not bound to a discord user",
UserNotConnected: "You are not connected",
NotConnected: "The bot is not connected",
NotSameChannel: "You are not in the same channel with the bot"
};
engine.log("Voice State loaded!");
event.on('discord:VOICE_STATE_UPDATE', function (ev) {
var cid = ev.channel_id;
if (typeof cid === undefined || cid == null) {
store.unsetGlobal('voicestate-' + ev.user_id);
engine.log('User ' + ev.user_id + ' is disconnected');
} else {
if (store.getGlobal('voicestate-' + ev.user_id) == null) {
store.setGlobal('voicestate-' + ev.user_id, ev.channel_id);
engine.log('User ' + ev.user_id + ' connected.');
} else {
store.setGlobal('voicestate-' + ev.user_id, ev.channel_id);
engine.log('User ' + ev.user_id + ' is already connected');
}
}
});
event.on('api:voicestate', ev => {
const res = new Response();
var user_uid = ev.user().uid().split("/").pop();
engine.log(user_uid);
if (!ev.user()) {
res.setError(errorMessages.NotLoggedIn);
return res.getData();
}
if (!user_uid) {
res.setError(errorMessages.NotBound);
return res.getData();
}
var state = store.getGlobal('voicestate-' + user_uid);
var cur_chan = backend.getCurrentChannel().id().split("/").pop();
engine.log(state + " ? " + cur_chan);
if (state != null && state == cur_chan) {
res.setData({ user: user_uid, connected: true, channel: state });
return res.getData();
} else if (state != null && state != cur_chan) {
res.setError(errorMessages.NotSameChannel);
return res.getData();
} else {
res.setError(errorMessages.UserNotConnected);
return res.getData();
}
});
class Response {
constructor() {
this.success = true;
this.data = null;
}
setData(data) {
this.data = data;
}
getData() {
return {
data: this.data,
success: this.success
}
}
setError(error) {
this.success = false;
this.data = error;
};
}
});