-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (130 loc) · 5.16 KB
/
index.js
File metadata and controls
154 lines (130 loc) · 5.16 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const Discord = require('discord.js');
const config = require('./config.json');
const bot = new Discord.Client();
/*
CONFIG STUFF
*/
// bot items
const TOKEN = config.bot.token;
const NICK = config.bot.nickname;
const OWNERS = config.bot.owners;
const CHANNEL_ID = config.bot.music_channel;
const GAME = config.bot.game;
const CMD_PREFIX = config.bot.cmd_prefix;
// radio items
const STREAM_URL = config.radio.stream_url;
const VOL = config.radio.volume;
/*
VARIABLES
*/
var channel;
var voice;
var dispatcher;
/*
COMMAND STUFF
*/
const cmds = [
{name: "play", exec: (msg, args) => {
if (!dispatcher) // checking if a dispatcher exists
return;
if (dispatcher.paused) {
dispatcher.resume(); // resuming playback of the stream
msg ? msg.reply("Playback is at maximum power, general sir.") : console.log("Playback is at maximum power, general sir.");
} else {
msg ? msg.reply("The stream is already playing dingus") : console.log("The stream is already playing dingus");
}
}},
{name: "pause", exec: (msg, args) => {
if (!dispatcher) // checking if a dispatcher exists
return;
if (!dispatcher.paused) {
dispatcher.pause(); // pausing the playback of the stream
msg ? msg.reply("Paused starting rn.") : console.log("Paused starting rn.");
} else {
msg ? msg.reply("Duh, the stream is already paused") : console.log("Duh, the stream is already paused");
}
}},
{name: "reset", exec: (msg, args) => {
if (voice) // checking if a voice exists
voice.disconnect(); // disconnection
channel.join().then(voiceStart).catch(console.error); // starting a new playback from the voice channel
msg ? msg.reply("Welcome back mr. bot") : console.log("Welcome back mr. bot");
}},
{name: "volume", exec: (msg, args) => {
if (dispatcher) { // check for dispatcher
dispatcher.setVolume(args[0]); // set vol
msg ? msg.reply(`Mr. Einstien, ${args[0]} is the right ratio!`) : console.log(`Mr. Einstein, ${args[0]} is the right ratio!`);
}
}},
{name: "nick", exec: (msg, args) => {
if (channel) {
channel.guild.members.get(bot.user.id).setNickname(args.join(" ")); // set of nickname, by joining the args
msg ? msg.reply("Subscribed to new nickname") : console.log("Subscribed to new nickname");
}
}},
{name: "game", exec: (msg, args) => {
bot.user.setGame(args.join(" ")); // setting the game, by joining args
msg ? msg.reply("Game is my middle name.") : console.log("Game is my middle name.");
}}
]
// discord messages
bot.on('message', function(msg) {
var split = msg.content.split(' ');
var val = cmds.find(x => split[0] === CMD_PREFIX + x.name);
if (val) {
console.log(`Incoming command "${msg.content}" from ${msg.author.username}`);
// checking if perms exists (checking for owner)
if (!OWNERS.includes(msg.author.id)) { // check if msg sender is an owner
console.log(`**FLAG**: ${msg.author.username} tried to invoke a command without permissions!`)
msg.reply("You don't have permissions to do that!");
return;
}
split.shift(); // shifting the split args
val.exec(msg, split); // invokation
console.log("Command successfully invoked!");
} else if (split[0].startsWith(CMD_PREFIX)) {
console.log(`Unknown command "${msg.content}" was invoked by ${msg.author.username}`);
msg.reply("It seems that command doesn't exist!");
}
});
// console commands
process.stdin.resume(); // setting up the chat
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(text) {
text = text.replace(/\n/g, ""); // getting rid of new line chars
var split = text.split(" "); // split text by " "
var cmd = split[0]; // finding the command
split.shift(); // taking out the first element
cmds.forEach(function(val, index, arr) {
if (cmd === val.name) {
val.exec(null, split);
}
});
});
/*
MAIN BOT STUFF
*/
bot.on("ready", function() {
console.log("The bot is now ready, lemme just do some stuff to get it setup");
init(); // initialization of the bot's essentials
});
function init() {
channel = bot.channels.get(CHANNEL_ID); // finding the channel
if (!channel) // check if found channel
throw "The bot couldn't find the channel with the specified ID";
bot.user.setGame(GAME); // setting the game
channel.guild.members.get(bot.user.id).setNickname(NICK); // setting the bot's nickname
// connection to the voice channel n stuffs
if (!channel.joinable)
throw "The bot doesn't have permissions to join the voice channel";
channel.join().then(voiceStart).catch(console.error);
}
function voiceStart(_voice) {
voice = _voice;
// starting the voice
dispatcher = voice.playArbitraryInput(STREAM_URL); // playing the stream using ffmpeg
dispatcher.setVolume(VOL); // setting the vol
console.log("Playing audio stream");
}
bot.login(TOKEN);
console.log("The bot is now logged in!");