This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
app.js
244 lines (239 loc) · 11.9 KB
/
app.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
234
235
236
237
238
239
240
241
242
243
244
const { Client, Collection, MessageEmbed, Message } = require("discord.js");
const { DiscordUNO } = require("discord-uno");
const fs = require("fs");
const childProcess = require('child_process')
const mongoose = require('mongoose');
const defaultconfig = {
"autoUpdate": true,
"token": "",
"prefix": "!",
"Admin": "",
"osuAPI": {
"client_id": "",
"client_secret": "",
"typeof client_id": "number",
"typeof client_secret": "string"
},
"mongoPath": "",
"youtubecookie": "",
"oauthv2link": "",
"loglevel": "message",
"Vaild value for loglevel": "message / error / none"
}
if (!fs.existsSync('./config.json')) {
console.log('[Config Handlers]', "config.json doesn't exists. Attemping to create a new one...")
fs.writeFileSync('./config.json', JSON.stringify(defaultconfig, null, 4))
}
var config = JSON.parse(fs.readFileSync('./config.json').toString());
for (let a in defaultconfig) {
if (config[a] == undefined) {
console.log('[Config Handlers]', a, 'was not found in config.json. Adding with default value: ' + defaultconfig[a] + '...')
config[a] = defaultconfig[a]
}
fs.writeFileSync('./config.json', JSON.stringify(config, null, 4))
}
var config = JSON.parse(fs.readFileSync('./config.json').toString());
const main = async () => {
if (config.token != '') {
const moment = require('moment');
const ms = require('ms');
const Timeout = new Collection();
const client = new Client({
disableEveryone: true
});
require('discord-buttons')(client);
client.discordUNO = new DiscordUNO();
client.commands = new Collection();
client.aliases = new Collection();
client.categories = fs.readdirSync("./commands/");
fs.readdirSync('./handlers').forEach(handler => {
require(`./handlers/${handler}`)(client);
});
client.on('ready', () => {
console.log('\x1b[33m%s\x1b[0m', `Logged in as ${client.user.tag}!`);
setInterval(() => {
const statuses = [
`github.com/hellsnakes/hellsnakebot`,
`with ${client.guilds.cache.size} servers`,
`with ${client.channels.cache.size} channels`,
`with ${client.users.cache.size} users`,
`HELLSNAKEBOT| ${config.prefix}help`,
`osu!`,
]
const status = statuses[Math.floor(Math.random() * statuses.length)]
client.user.setActivity(status, { type: "PLAYING" })
}, 60000)
// Database Connect
mongoose.connect(config.mongoPath, { useUnifiedTopology: true, useNewUrlParser: true })
.then(() => console.log('\x1b[33m%s\x1b[0m', `Connected to Mongo`))
.catch((error) => { console.log(error) });
});
const prefixSchema = require('./schemas/prefixcustoms')
client.prefix = async function (message) {
let custom;
const data = await prefixSchema.findOne({ Guild: message.guild.id })
.catch(err => console.log(err))
if (data) {
custom = data.Prefix;
} else {
custom = config.prefix;
}
return custom;
}
client.on("message", async message => {
const prefixes = await client.prefix(message)
if (message.content.includes(`<@!${config.Admin}>`)) if (!message.author.id == client.user.id) message.reply('Tag ad bo may cc dmm')
if (message.content == `<@!${client.user.id}>` || message.content == `<@${client.user.id}>`) {
return message.reply(`**Use ${prefixes}help to display all commands available.**`);
} else {
if (!message.content.startsWith(config.prefix) && !message.content.startsWith(`<@!${client.user.id}>`) && message.content.includes(`<@!${client.user.id}>`)) return message.reply(`**Use ${prefixes}help to display all commands available.**`);
if (!message.content.startsWith(config.prefix) &&!message.content.startsWith(`<@${client.user.id}>`) && message.content.includes(`<@${client.user.id}>`)) return message.reply(`**Use ${prefixes}help to display all commands available.**`);
}
if (config.loglevel == 'message') {
if (message.content.startsWith(prefixes) && message.content.startsWith(`<@!${client.user.id}>`)) {
console.log('\x1b[32m%s\x1b[0m', `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${message.author.username} (${message.author.id}) issued command in ${message.channel.id}: ${message.content}`);
} else {
if (message.attachments.first() != undefined && message.content != '') {
console.log('\x1b[32m%s\x1b[0m', `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${message.author.username} (${message.author.id}) messaged in ${message.channel.id}: ${message.content}`);
console.log('\x1b[32m%s\x1b[0m', `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${message.author.username} (${message.author.id}) sent an attachment in ${message.channel.id}: ${message.attachments.first().url}`)
} else if (message.attachments.first() != undefined && message.content == '') {
console.log('\x1b[32m%s\x1b[0m', `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${message.author.username} (${message.author.id}) sent an attachment in ${message.channel.id}: ${message.attachments.first().url}`)
} else if (message.attachments.first() == undefined && message.content != '') {
console.log('\x1b[32m%s\x1b[0m', `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${message.author.username} (${message.author.id}) messaged in ${message.channel.id}: ${message.content}`);
} else {
if (message.embeds.length != 0) {
let a = message.embeds[0]
let embed = {}
for (let b in a) {
if (a[b] != null && (a[b] != [] && a[b].length != 0) && a[b] != {}) {
embed[b] = a[b]
}
}
console.log('\x1b[32m%s\x1b[0m', `[${moment().format('YYYY-MM-DD HH:mm:ss')}] ${message.author.username} (${message.author.id}) sent an embed in ${message.channel.id}: ${JSON.stringify(embed, null, 2)}`)
}
}
}
}
if (message.author.bot) return;
if (!message.guild) return;
//console.log(!message.content.startsWith(prefixes) && (!message.content.startsWith(`<@!${client.user.id}>` || !message.content.startsWith(`<@${client.user.id}>`))))
if (!message.content.startsWith(prefixes)) {
if (!message.content.startsWith(`<@!${client.user.id}>`))
if (!message.content.startsWith(`<@${client.user.id}>`)) return;
}
if (!message.member) message.member = await message.guild.fetchMember(message);
const args = message.content.trim().split(/ +/g);
//console.log(args)
var cmd;
var a = args.shift();
if (a == `<@!${client.user.id}>` || a == `<@${client.user.id}>`) {
cmd = args[0]
args.shift();
} else {
cmd = a.toLowerCase().replace(`<@!${client.user.id}>`, '').replace(`<@${client.user.id}>`, '').replace(prefixes, '');
}
//console.log(args,cmd)
if (cmd.length === 0) return;
let command = client.commands.get(cmd);
if (!command) command = client.commands.get(client.aliases.get(cmd));
if (command) {
if (command.run != undefined) {
if (Timeout.has(`${command.name}${message.author.id}`)) return message.channel.send(`You are on a \`${ms(Timeout.get(`${command.name}${message.author.id}`) - Date.now(), { long: true })}\` cooldown.`)
command.run(client, message, args);
Timeout.set(`${command.name}${message.author.id}`, Date.now() + command.timeout)
setTimeout(() => {
Timeout.delete(`${command.name}${message.author.id}`)
}, command.timeout)
} else return;
}
});
const distube = require('distube');
client.distube = new distube(client, { searchSongs: true, emitNewSongOnly: true, leaveOnEmpty: true, leaveOnFinish: true, updateYouTubeDL: false, youtubeCookie: config.youtubecookie })
const status = (queue) => `Volume: \`${queue.volume}%\` | Loop: \`${queue.repeatMode ? queue.repeatMode == 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\`| Filter: \`${queue.filter || "OFF"}\``;
client.distube
.on("playSong", (message, queue, song) => {
const embed = new MessageEmbed()
.setTitle('<:headphones:879518595602841630> Started Playing')
.setDescription(`[${song.name}](${song.url})`)
.addField('**Views:**', song.views)
.addField('<:like:879371469132562552>', song.likes)
.addField('<:dislike:879371468817973299>', song.dislikes)
.addField('**Duration:**', song.formattedDuration)
.addField('**Status**', status(queue))
.setThumbnail(song.thumbnail)
.setColor("RANDOM")
message.channel.send(embed)
})
.on('addSong', (message, queue, song) => {
const embed = new MessageEmbed()
.setTitle(`<:addsong:879518595665780746> Added song to queue`)
.setDescription(`\`${song.name}\` - \`${song.formattedDuration}\` - Requested by ${song.user}`)
.setColor("RANDOM")
message.channel.send(embed);
})
.on("playList", (message, queue, playlist, song) => {
const embed = new MessageEmbed()
.setTitle(`🎵 PlayList`)
.setDescription(`Play \`${playlist.name}\` playlist (${playlist.songs.length} songs).\nRequested by: ${song.user}\nNow playing \`${song.name}\` - \`${song.formattedDuration}\`\n${status(queue)}`)
message.channel.send(embed)
})
.on("addList", (message, queue, playlist) => {
const embed = new MessageEmbed()
.setTitle(`<:addsong:879518595665780746> Add list`)
.setDescription(`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`)
.setColor("RANDOM")
message.channel.send(embed);
})
.on("searchResult", (message, result) => {
let i = 0;
const embed = new MessageEmbed()
.setTitle('Choose an option from below')
.setDescription(`${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}`)
.setColor("RED")
.setFooter('*Enter anything else or wait 60 seconds to cancel*')
message.channel.send(embed).then(embed => { embed.delete({ timeout: 61000 }) })
})
.on("searchCancel", (message) => message.channel.send(`***Searching canceled***`))
.on('error', (message, e) => {
message.channel.send(`An error encountered: ${e}`)
})
.on('empty', (message, queue) => {
message.channel.send(`***Channel is empty. Leaving the channel***`)
})
.on('finish', (message, queue) => {
message.channel.send(`***No more song in queue. Leaving the channel***`)
})
.on('initQueue', (queue) => {
queue.autoplay = false;
queue.volume = 50;
});
client.login(config.token);
} else {
console.error('[ERROR]', 'Please spectify a Discord bot token in config.json.')
//eslint-disable-next-line no-unreachable
process.exit(1)
}
}
async function update() {
if (config.autoUpdate == true) {
if (fs.existsSync('./.git')) {
//ensures that the bot was cloned by using git ./.git is in directory.
console.log('[Updater]', 'Updating...')
const child = childProcess.spawn('git', ['pull'])
//ensure thats the bot run after update.
child.on('close', async () => {
//Ensuring thats new package are installed.
console.log('[Updater]', 'Installing new package...')
childProcess.execSync('npm i')
console.log('[Updater]', 'Updated. Starting...')
await main()
})
} else {
console.log('[Updater]', './.git was not found in this directory. Skipping update...')
await main()
}
} else {
await main()
}
}
update()