-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.js
126 lines (103 loc) · 2.46 KB
/
commands.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
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
const {
playMusic,
pauseMusic,
resumeMusic,
toggleMusic,
} = require("./youtube");
const { sendMsgToMeet } = require("./meet");
/**
* Check if the message is a command.
*
* If message is a command, execute command.
* @param {string} message
* @param {Array} pages
*/
async function processCommand(message, pages) {
const command = await decipherMsg(message);
if (command === null) return null;
const { ymusic, meet } = pages;
const { cmdName, args } = command;
var reply;
switch (cmdName.toLowerCase()) {
case "p":
case "play":
reply = await playMusicCmd(ymusic, args);
break;
case "pause":
reply = await pauseMusicCmd(ymusic);
break;
case "resume":
reply = await resumeMusicCmd(ymusic);
break;
case "toggle":
reply = await toggleMusicCmd(ymusic);
break;
case "h":
case "help":
reply = await helpCmd();
break;
case "volume":
reply = "Coming Soon...";
break;
default:
reply = "Invalid Command!!!";
}
await sendMsgToMeet(meet, reply);
}
/**
* Checks if message is command:
* * If it is, returns an object containing command details.
* * Else returns null.
* @param {string} message
* @returns {Promise<{ cmdName: string, args: string }>}
*/
async function decipherMsg(message) {
const MATCH_COMMAND = /^\/(?<cmdName>\w+)\s*(?<args>(\w|\s)*\w)?\s*$/i;
const command = message.match(MATCH_COMMAND);
// if it is not a command return null
if (command === null) return null;
return command.groups;
}
/**
* Return the list of commands.
*/
async function helpCmd() {
const COMMAND_LIST = `
/play \<songName>
/p \<songName>
/pause
/resume
/toggle
/help
`;
return COMMAND_LIST;
}
/**
* Command for playing music.
* @param {page} ymusic
* @param {string} args
* @returns {Promise<"Playing [songname] by [artist]"|"Enter a song!!!"|"No songs found.">}
*/
async function playMusicCmd(ymusic, args) {
if (args === undefined) return "Enter a song!!!";
const query = args;
const songData = await playMusic(ymusic, query);
if (songData === null) return "No songs found.";
const reply = `Playing ${songData.name} by ${songData.artist}`;
return reply;
}
async function pauseMusicCmd(ymusic) {
const reply = await pauseMusic(ymusic);
return reply;
}
async function resumeMusicCmd(ymusic) {
const reply = await resumeMusic(ymusic);
return reply;
}
async function toggleMusicCmd(ymusic) {
const reply = await toggleMusic(ymusic);
return reply;
}
module.exports = {
processCommand,
};