forked from laniku/Soundy-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soundy.js
233 lines (224 loc) · 12.8 KB
/
soundy.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//This part of the code initializes all variables from the Modules folder. Remember to change it whenever you install a dependency.
const { Discord, Client, token, prefix, fs, HelpMenu, InviteMenu, tts, util, ActivitiesList, VoteMenu, Languages, ConfigMenu } = require("./Modules/Variables.js");
const { SaveTTS } = require("./Modules/Functions.js");
const { strike } = require("./Modules/Token.js");
//The variable isReady determines whenever the bot is able to play an audio or not. Be careful, it can cause problems sometimes.
isReady = true;
//This part of the code is executed when the bot logs in, also used for random status.
Client.on('ready', () => {
console.log("Logged in!");
//This whole piece of code is the random status.
var SoundyHelp = true
setInterval(() => {
if (SoundyHelp == true){
//This is the default status that executes between the random statuses.
Client.user.setActivity("Type \"soundy help\"");
SoundyHelp = false
}else{
const index = Math.floor(Math.random() * ActivitiesList.length + 1)
Client.user.setActivity(ActivitiesList[index])
SoundyHelp = true
}
}, 10000)
//^^ You can change that number to change the interval of time between every status in miliseconds.
});
//This part of the code is executed when a message is sent.
Client.on('message', message => {
const args = message.content.slice(prefix.length).trim().split(' ');
const command = args.shift()
//This is the Play function, i couldn't declare it in the Modules folder because it wouldn't recognise the variables.
//I'm pretty sure that declaring it right here is a bad way of doing it and makes the code look messier but it's the only workaround i could find
function Play(Wtp) {
try{
voiceChannel = message.member.voice.channel;
if (voiceChannel != null)
{
if (voiceChannel.joinable){
isReady = false
voiceChannel.join().then(connection =>
{
message.guild.me.voice.setSelfDeaf(true)
const dispatcher = connection.play(Wtp);
dispatcher.on("finish", end => {
voiceChannel.leave()
isReady = true;
})
dispatcher.on("error", end => {
console.error()
voiceChannel.leave()
isReady = true;
})
}).catch(err => console.log(err));
}else
message.channel.send("I don't have access to that voice channel! I don't know what to do :(")
}else{
message.channel.send("You\'re not in a voice channel! I don't know what to do :(")
}
}catch(err){
console.log("Failed to play an audio")
console.log(err)
Client.users.cache.get("561560432100245520").send("An error ocurred playing an audio btw, lemme give you more info: \n```\n" + err + "\n```");
}
}
//This piece of code is executed when the message that is sent doesn't contain the Soundy prefix. Do not touch.
if (!message.content.startsWith(prefix) || message.author.bot) return;
else if (isReady || command.toLowerCase() == "stop" || command.toLowerCase() == "eval") {
//This checks the config file of the server
if (fs.existsSync("./ServerConfigs/" + message.guild.id + ".json")){
if (JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json")).CFVersion != JSON.parse(fs.readFileSync("./ServerConfigs/Template.json")).CFVersion){
fs.unlinkSync("./ServerConfigs/" + message.guild.id + ".json")
fs.copyFileSync("./ServerConfigs/Template.json", "./ServerConfigs/" + message.guild.id + ".json", fs.constants.COPYFILE_EXCL, (err) => {
if (err) throw err;
console.log('File was copied to destination');
});
}
}else
fs.copyFileSync("./ServerConfigs/Template.json", "./ServerConfigs/" + message.guild.id + ".json", fs.constants.COPYFILE_EXCL, (err) => {
if (err) throw err;
console.log('File was copied to destination');
});
//This is the switch statement that contains all the commands, i'm pretty happy i used a switch. It makes the code look a lot prettier and more efficient. Adding a command is also very easy.
if (JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json")).DisabledCommands.includes(command) == false)
switch(command.toLowerCase()){
case "stop":
if (message.guild.me.voice.channel != null){
message.guild.me.voice.channel.leave();
isReady = true
}else
message.channel.send("Umm what do you want to stop? xD")
break
case "help":
message.react("☑")
message.author.send(HelpMenu);
break
case "flipacoin":
if (Math.floor((Math.random() * 2) + 1) == 1){
message.channel.send("You got head :\"\"0")
}else{
message.channel.send("You got tail :\"\"0")
}
break
case "ping":
//I'm sure this isn't a very efficient way of converting a negative number to a positive one but i'll find another workaround.
message.channel.send("The ping is \"" + `${(message.createdTimestamp - Date.now()).toString().slice(1)}` + " ms\"");
break
case "invite":
message.channel.send(InviteMenu)
break
case "vote":
//TODO: Give this thing a use. It's kinda useless and looks ugly.
message.channel.send(VoteMenu)
break
case "eval":
//The eval command, not the best way of doing eval but it works like a charm ^_^
if(message.author.id !== "561560432100245520"){
message.channel.send("Woah there you know Eval is dangerous af");
return;
}
try {
const code = args.join(" ");
let evaled = eval(code);
if (typeof evaled !== "string")
evaled = require("util").inspect(evaled);
message.channel.send(evaled, {code:"xl"});
} catch (err) {
message.channel.send("\`ERROR\` \`\`\`xl\n " + err + "\n\`\`\`");
}
break
case "tts":
//The TTS, the API i'm using is kinda unstable so it sometimes throws unwanted results. But i'm still pretty hapy with it.
if (args.length > 0){
SaveTTS(Play("TTS.mp3"), message.content.split(' ').splice(2).join(' '), JSON.parse(fs.readFileSync("./Config.json")).Languages[JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json")).Lang])
}else
message.channel.send("It would be good if you uuuh... Provided text")
break
case "say":
//I'm planning to make this command optional in the config menu i have planned since it can rate limit Soundy and increase raids.
if (args.length > 0){
message.delete()
message.channel.send("Sending message...").then((sentMessage) => sentMessage.edit(message.content.split(' ').splice(2).join(' ')))
}else
message.channel.send("*says nothing*")
break
case "snipe":
//Hahaha inspiration on dankmemer go brrrrr
message.channel.send("Here you have", {files: ["./TTS.mp3"]})
break
case "config":
if (message.member.hasPermission('MANAGE_GUILD')) {
if (args.length > 0){
console.log(args)
switch(args[0].toLowerCase()){
case "lang":
console.log(args.length = 2)
console.log(Languages.includes(args[1]))
if (args.length == 2 && Languages.includes(args[1])){
let File = JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json"))
File.Lang = args[1]
fs.writeFileSync("./ServerConfigs/" + message.guild.id + ".json", JSON.stringify(File, null, 2));
message.channel.send("Done!")
}else
message.channel.send("Not enough arguments or the language code doesn't exist :\"(\n\nCurrent language codes: " + Languages.toString().replace(/,/g, " "))
break
case "disable":
if (args.length == 2 && JSON.parse(fs.readFileSync("./Config.json")).Commands.includes(args[1]) && JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json")).DisabledCommands.includes(args[1]) == false){
let File = JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json"))
File.DisabledCommands.push(args[1])
fs.writeFileSync("./ServerConfigs/" + message.guild.id + ".json", JSON.stringify(File, null, 2));
message.channel.send("Done!")
}else
message.channel.send("Hey that's illegal!")
break
case "enable":
if (args.length == 2 && JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json")).DisabledCommands.includes(args[1])){
let File = JSON.parse(fs.readFileSync("./ServerConfigs/" + message.guild.id + ".json"))
File.DisabledCommands.splice(File.DisabledCommands.indexOf(args[1]), 1);
fs.writeFileSync("./ServerConfigs/" + message.guild.id + ".json", JSON.stringify(File, null, 2));
message.channel.send("Done!")
}else
message.channel.send("That command isn't disabled!")
break
case "restore":
fs.unlinkSync("./ServerConfigs/" + message.guild.id + ".json")
fs.copyFileSync("./ServerConfigs/Template.json", "./ServerConfigs/" + message.guild.id + ".json", fs.constants.COPYFILE_EXCL, (err) => {
if (err) throw err;
});
break
case "export":
if (args.length == 1){
fs.unlinkSync("./Temp/Config.soundy")
fs.copyFileSync("./ServerConfigs/" + message.guild.id + ".json", "./Temp/Config.soundy", fs.constants.COPYFILE_EXCL, (err) => {
if (err) throw err;
});
message.channel.send("Here you have", {files: ["./Temp/Config.soundy"]})
}
break
}
message.channel.send
}else
message.channel.send(ConfigMenu)
}else
message.channel.send("You need the manage server permission to change the guild configuration :no_entry:")
break
default:
//This code executes when none of the commands are executed and searchs for a sound. This is a very efficient way fo doing it.
try {
if (fs.existsSync('./Audio/' + command.toLowerCase() + ".mp3")) {
message.delete();
Play('./Audio/' + command.toLowerCase() + ".mp3")
}else{
message.channel.send("I can't find that! o.O")
}
} catch(err) {
console.error(err)
}
break
}
}
//This piece of code executes when the bot isn't ready to play sounds
else if (isReady == false)
{
message.channel.send("Hold on a second! I'm busy")
}
});
Client.login(token);