This repository has been archived by the owner on Sep 29, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
app.js
232 lines (230 loc) · 10.6 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
const Discord = require("discord.js")
require("dotenv").config()
const { Client, Intents, MessageEmbed } = require("discord.js")
const client = new Client({
intents: [Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS],
ws: { properties: { $browser: "Discord iOS" } }
})
const { readdirSync } = require("fs")
const moment = require("moment")
const humanizeDuration = require("humanize-duration")
const Timeout = new Set()
client.slash = new Discord.Collection()
const { REST } = require("@discordjs/rest")
const { Routes } = require("discord-api-types/v9")
const path = require("path")
const { keepalive } = require("./keepalive")
const commands = []
readdirSync("./commands/").map(async dir => {
readdirSync(`./commands/${dir}/`).map(async (cmd) => {
commands.push(require(path.join(__dirname, `./commands/${dir}/${cmd}`)))
})
})
const rest = new REST({ version: "9" }).setToken(process.env.token);
(async () => {
try {
await rest.put(
Routes.applicationCommands(process.env.botID),
{ body: commands }
)
console.log("\x1b[34m%s\x1b[0m", "Successfully reloaded application (/) commands.")
} catch (error) {
console.error(error)
}
})();
["slash", "anticrash"].forEach(handler => {
require(`./handlers/${handler}`)(client)
})
client.on("ready", () => {
console.log("\x1b[34m%s\x1b[0m", `Logged in as ${client.user.tag}!`)
const statuses = [ // status bot
"Hentaiz",
`with ${client.guilds.cache.size} servers`,
`with ${client.guilds.cache.reduce((a, b) => a + b.memberCount, 0)} users`,
"Youtube",
"Slash command",
"Spotify",
"soundcloud",
"Twitch"
]
let index = 0
setInterval(() => {
if (index === statuses.length) index = 0
const status = statuses[index]
client.user.setActivity(`${status}`, {
type: "LISTENING",
browser: "DISCORD IOS"
})
index++
}, 60000)
})
client.on("messageCreate", async (message) => {
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) {
const a = message.embeds[0]
const embed = {}
for (const 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)}`)
}
}
})
client.on("interactionCreate", async (interaction) => {
if (interaction.isCommand() || interaction.isContextMenu()) {
if (!client.slash.has(interaction.commandName)) return
if (!interaction.guild) return
const command = client.slash.get(interaction.commandName)
try {
if (command.timeout) {
if (Timeout.has(`${interaction.user.id}${command.name}`)) {
return interaction.reply({ content: `You need to wait **${humanizeDuration(command.timeout, { round: true })}** to use command again`, ephemeral: true })
}
}
if (command.permissions) {
if (!interaction.member.permissions.has(command.permissions)) {
return interaction.reply({ content: `:x: You need \`${command.permissions}\` to use this command`, ephemeral: true })
}
}
command.run(interaction, client)
Timeout.add(`${interaction.user.id}${command.name}`)
setTimeout(() => {
Timeout.delete(`${interaction.user.id}${command.name}`)
}, command.timeout)
} catch (error) {
console.error(error)
await interaction.reply({ content: ":x: There was an error while executing this command!", ephemeral: true })
}
}
})
client.on("guildCreate", guild => {
const embed = new MessageEmbed()
.setTitle("I'm added to a new server")
.setThumbnail(client.user.displayAvatarURL())
.setDescription(`I'm added to ${guild.name} | ID ${guild.id}\n Server member: ${guild.memberCount}\nTotal server: ${client.guilds.cache.size}`)
.setTimestamp()
const logchannel = client.channels.cache.get(process.env.Channel_log)
logchannel.send({ embeds: [embed] })
})
client.on("guildDelete", guild => {
const embed = new MessageEmbed()
.setTitle("I'm left a new server")
.setThumbnail(client.user.displayAvatarURL())
.setDescription(`I'm left to ${guild.name} | ID ${guild.id}\n Server member: ${guild.memberCount}\nTotal server: ${client.guilds.cache.size}`)
.setTimestamp()
const logchannel = client.channels.cache.get(process.env.Channel_log)
logchannel.send({ embeds: [embed] })
})
// Distube
const Distube = require("distube")
const { SoundCloudPlugin } = require("@distube/soundcloud")
const { SpotifyPlugin } = require("@distube/spotify")
const { YouTubeDLPlugin } = require("@distube/yt-dlp")
/* eslint new-cap: ["error", { "properties": false }] */
client.distube = new Distube.default(client, {
youtubeDL: false,
leaveOnEmpty: true,
emptyCooldown: 30,
leaveOnFinish: false,
emitNewSongOnly: true,
updateYouTubeDL: true,
nsfw: true,
youtubeCookie: process.env.ytcookie,
plugins: [new SoundCloudPlugin(), new SpotifyPlugin(), new YouTubeDLPlugin()]
})
const status = (queue) => `Volume: \`${queue.volume}%\` | Loop: \`${queue.repeatMode ? queue.repeatMode === 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\` | Filter: \`${queue.filters.join(", ") || "Off"}\``
// DisTube event listeners
client.distube
.on("playSong", (queue, song) => {
const embed = new MessageEmbed()
.setColor("RANDOM")
.setAuthor({ name: "Started Playing", iconURL: "https://raw.githubusercontent.com/HELLSNAKES/Music-Slash-Bot/main/assets/music.gif" })
.setThumbnail(song.thumbnail)
.setDescription(`[${song.name}](${song.url})`)
.addField("**Views:**", song.views.toString(), true)
.addField("**Like:**", song.likes.toString(), true)
.addField("**Duration:**", song.formattedDuration.toString(), true)
.addField("**Status**", status(queue).toString())
.setFooter({ text: `Requested by ${song.user.username}`, iconURL: song.user.avatarURL() })
.setTimestamp()
queue.textChannel.send({ embeds: [embed] })
})
.on("addSong", (queue, song) => {
const embed = new MessageEmbed()
.setTitle(":ballot_box_with_check: | Added song to queue")
.setDescription(`\`${song.name}\` - \`${song.formattedDuration}\` - Requested by ${song.user}`)
.setColor("RANDOM")
.setTimestamp()
queue.textChannel.send({ embeds: [embed] })
})
.on("addList", (queue, playlist) => {
const embed = new MessageEmbed()
.setTitle(":ballot_box_with_check: | Add list")
.setDescription(`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`)
.setColor("RANDOM")
.setTimestamp()
queue.textChannel.send({ embeds: [embed] })
})
.on("error", (textChannel, e) => {
console.error(e)
textChannel.send(`An error encountered: ${e}`)
})
// .on("finish", queue => queue.textChannel.send("***No more song in queue. Leaving the channel***"))
.on("finishSong", queue => {
const embed = new MessageEmbed()
.setDescription(`:white_check_mark: | Finished playing \`${queue.songs[0].name}\``)
queue.textChannel.send({ embeds: [embed] })
})
.on("disconnect", queue => {
const embed = new MessageEmbed()
.setDescription(":x: | Disconnected from voice channel")
queue.textChannel.send({ embeds: [embed] })
})
.on("empty", queue => {
const embed = new MessageEmbed()
.setDescription(":x: | Channel is empty. Leaving the channel!")
queue.textChannel.send({ embeds: [embed] })
})
.on("initQueue", (queue) => {
queue.autoplay = false
queue.volume = 50
})
keepalive()
if (!process.env.token) {
console.error("[ERROR]", "Token not found please visit: https://discord.com/developers/application to get token")
process.exit(0)
}
client.login(process.env.token)
process.on("SIGINT", () => {
console.log("\x1b[36m%s\x1b[0m", "SIGINT detected, exiting...")
process.exit(0)
})
// check update repo
const fetch = require("node-fetch")
const { version } = require("./version.json")
console.log("\x1b[33m%s\x1b[0m", `Current version : ${version}`)
fetch("https://raw.githubusercontent.com/HELLSNAKES/Music-Slash-Bot/main/version.json")
.then((res) => res.json())
.then((data) => {
if (data.version !== version) {
console.log("\x1b[32m%s\x1b[0m", "===============================Update Available===================================")
console.log("Ver:", data.version)
console.log("\x1b[36m%s\x1b[0m", "Check commit : https://github.com/HELLSNAKES/Music-Slash-Bot/commits/main")
console.log("\x1b[31m%s\x1b[0m", "Use `npm run updatebot` to update")
console.log("\x1b[32m%s\x1b[0m", "==================================================================================")
} else {
console.log("\x1b[32m%s\x1b[0m", "No Update Available")
}
})
.catch((err) => {
console.log("\x1b[31m%s\x1b[0m", err)
})