-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (223 loc) · 7.5 KB
/
index.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
require('dotenv').config();
const { DisTube } = require('distube');
const Discord = require('discord.js');
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.DirectMessageTyping,
Discord.GatewayIntentBits.DirectMessages,
Discord.GatewayIntentBits.MessageContent,
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.GuildMessageReactions
],
partials: [
Discord.Partials.Channel,
]
});
const fs = require('fs');
const { emoji, prefix, youtubeCookie, clientId, clientSecret } = require('./configs/prefix.json');
const { SpotifyPlugin } = require('@distube/spotify');
const { SoundCloudPlugin } = require('@distube/soundcloud');
const { YtDlpPlugin } = require('@distube/yt-dlp');
const express = require('express');
const app = express();
const { PORT, TOKEN, DEV } = process.env;
const server = PORT || 3001;
const util = require('./src/helpers/embed');
const message = require('./src/events/message');
app.listen(server, () => {
console.log(`Server is running at port ${PORT}`);
});
app.get('/', (_, res) => {
res.send('Made with love by bayue#1015');
});
client.emotes = emoji;
client.prefix = prefix;
client.youtubeCookie = youtubeCookie;
client.clientId = clientId
client.clientSecret = clientSecret
let plugins;
if (client.clientId && client.clientSecret) {
plugins = [
new SpotifyPlugin({
parallel: true,
emitEventsAfterFetching: true,
api: { clientId: client.clientId, clientSecret: client.clientSecret }
}),
new SoundCloudPlugin(),
new YtDlpPlugin({ update: true })
]
} else {
plugins = [
new SoundCloudPlugin(),
new YtDlpPlugin({ update: true })
]
}
let youtubeCookies;
if (client.youtubeCookie) {
youtubeCookies = client.youtubeCookie;
} else {
youtubeCookies = 'none';
}
client.distube = new DisTube(client, {
leaveOnStop: false,
emitNewSongOnly: true,
emitAddSongWhenCreatingQueue: false,
emitAddListWhenCreatingQueue: false,
savePreviousSongs: true,
searchSongs: 5,
nsfw: true,
plugins: plugins,
youtubeCookie: youtubeCookies,
ytdlOptions: {
highWaterMark: 1024 * 1024 * 64,
quality: "highestaudio",
format: "audioonly",
liveBuffer: 60000,
dlChunkSize: 1024 * 1024 * 4,
}
});
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
const commandFolders = fs.readdirSync('./src/commands');
for (const folder of commandFolders) {
const commandFiles = fs.readdirSync(`./src/commands/${folder}`).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const cmd = require(`./src/commands/${folder}/${file}`);
console.log(`Loaded ${file}`);
client.commands.set(cmd.name, cmd);
if (cmd.aliases) cmd.aliases.forEach(alias => client.aliases.set(alias, cmd.name));
}
}
const eventFiles = fs.readdirSync('./src/events').filter(file => file.endsWith('.js'));
for (const file of eventFiles) {
const event = require(`./src/events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
if (message.name === "messageCreate") {
client.on(event.name, (...args) => event.execute(client, ...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}
}
client.on('messageCreate', async message => {
if (!message.channel.isDMBased() && (message.author.bot || !message.guild)) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
const cmd = client.commands.get(command) || client.commands.get(client.aliases.get(command));
console.log(cmd)
if (!cmd) return;
if (cmd.inVoiceChannel && !message.member.voice.channel) {
return message.channel.send({
embeds: [util.createTextEmbed(`${client.emotes.error} | You must be in a voice channel to use this command.`)]
});
}
try {
cmd.execute(client, message, args);
} catch (e) {
console.error(e);
message.channel.send({
embeds: [
util.createTextEmbed(`${client.emotes.error} | An error occured while executing this command. | \`${e}\``)
]
});
}
});
const status = queue =>
`Volume: \`${queue.volume}%\` | Filter: \`${queue.filters.names.join(', ') || 'Off'}\` | Loop: \`${queue.repeatMode ? (queue.repeatMode === 2 ? 'All Queue' : 'This Song') : 'Off'
}\` | Autoplay: \`${queue.autoplay ? 'On' : 'Off'}\``
client.distube
.on('playSong', (queue, song) => {
queue.textChannel.send({
embeds: [
util.createMessageEmbed('🎵 Playing', null, `[${song.name}](${song.url})`, song.thumbnail, [
{ name: 'Requested By', value: `${song.user}`, inline: true },
{ name: 'Duration', value: `${song.formattedDuration.toString()}`, inline: true },
{ name: 'Status', value: status(queue) }
])
]
});
})
.on('addSong', (queue, song) => {
queue.textChannel.send({
embeds: [
util.createMessageEmbed('🎵 Added to Queue', null, `[${song.name}](${song.url})`, song.thumbnail, [
{ name: 'Requested By', value: `${song.user}`, inline: true },
{ name: 'Duration', value: `${song.formattedDuration.toString()}`, inline: true },
{ name: 'Status', value: status(queue) }
])
]
});
})
.on('addList', (queue, playlist) => {
queue.textChannel.send({
embeds: [
util.createMessageEmbed(
'🎵 PlayList Added to Queue',
null,
`[${playlist.name} (${playlist.songs.length} songs)](${playlist.url})`,
playlist.thumbnail,
[
{ name: 'Requested By', value: `${playlist.user}`, inline: true },
{ name: 'Duration', value: `${playlist.formattedDuration.toString()}`, inline: true },
{ name: 'Status', value: status(queue) }
]
)
]
});
})
.on('noRelated', queue => queue.textChannel.send("Can't find related video to play."))
.on('error', (channel, e) => {
console.error(e);
channel.send({
embeds: [util.createTextEmbed(`${client.emotes.error} | An error encountered: ${e.toString().slice(0, 1974)}`)]
});
})
.on('empty', queue =>
queue.textChannel.send({
embeds: [util.createTextEmbed('Voice channel is empty! Leaving the channel...')]
})
)
.on('searchNoResult', (message, query) =>
message.channel.send({
embeds: [util.createTextEmbed(`${client.emotes.error} | No result found for \`${query}\`!`)]
})
)
.on('finish', queue =>
queue.textChannel.send({
embeds: [util.createTextEmbed('No more song in queue')]
})
)
// DisTubeOptions.searchSongs = true
.on('searchResult', (message, result) => {
let i = 0;
message.channel.send({
embeds: [
util.createTextEmbed(
`**Choose an option from below**\n${result
.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``)
.join('\n')}\n*Enter anything else or wait 60 seconds to cancel*`
)
]
});
})
.on('searchCancel', message =>
message.channel.send({
embeds: [util.createTextEmbed(`${client.emotes.error} | Searching canceled`)]
})
)
.on('searchInvalidAnswer', message =>
message.channel.send({
embeds: [
util.createTextEmbed(
`${client.emotes.error} | Invalid answer! You have to enter the number in the range of the results`
)
]
})
)
.on('searchDone', () => { });
client.login(TOKEN);