-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.mjs
144 lines (126 loc) · 4.69 KB
/
bot.mjs
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
import {
Client,
GatewayIntentBits,
Collection,
EmbedBuilder,
GatewayDispatchEvents
} from "discord.js";
import { token } from "./config.mjs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const { Aqua } = require('aqualink');
const nodes = [{
host: "",
password: "a",
port: 433,
secure: false,
name: "toddys"
}];
const __dirname = dirname(fileURLToPath(import.meta.url));
export const rootPath = __dirname;
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
],
partials: ["CHANNEL"],
});
const aqua = new Aqua(client, nodes, {
send: (payload) => {
const guild = client.guilds.cache.get(payload.d.guild_id);
if (guild) guild.shard.send(payload);
},
defaultSearchPlatform: "ytsearch",
restVersion: "v4",
shouldDeleteMessage: true
});
// Format time into MM:SS format
const formatTime = (time) => {
const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
return `${minutes}:${seconds < 10 ? "0" : ""}${seconds}`;
};
// Create a track embed with enhanced UI
const createTrackEmbed = (player, track) => {
return new EmbedBuilder()
.setColor(0x000000)
.setDescription(`> [\`${track.info.title}\`](${track.info.uri})`)
.addFields(
{ name: "> ⏱️ Duration", value: `> \`${formatTime(Math.round(track.info.length / 1000))}\``, inline: true },
{ name: "> 👤 Author", value: `> \`${track.info.author}\``, inline: true },
{ name: "> 💿 Album", value: `> \`${track.info.album || 'N/A'}\``, inline: true },
{ name: "> 🔊 Volume", value: `> \`${player.volume}%\``, inline: true },
{ name: "> 🔁 Loop", value: `> ${player.loop ? 'On' : 'Off'}`, inline: true }
)
.setThumbnail(track.info.artworkUrl)
.setAuthor({ name: "Kenium v2.4.0 | by mushroom0162", iconURL: client.user.avatarURL() })
.setTimestamp();
};
// Event listeners
aqua.on('trackStart', async (player, track) => {
const channel = client.channels.cache.get(player.textChannel);
if (channel) {
player.nowPlayingMessage = await channel.send({ embeds: [createTrackEmbed(player, track)] });
}
});
aqua.on('trackChange', async (player, newTrack) => {
if (player.nowPlayingMessage && !player.shouldDeleteMessage) {
await player.nowPlayingMessage.edit({ embeds: [createTrackEmbed(player, newTrack)] });
}
});
aqua.on('trackEnd', async (player) => {
if (player.nowPlayingMessage && !player.shouldDeleteMessage) {
try {
await player.nowPlayingMessage.delete();
} catch (error) {
if (error.code !== 10008) {
console.error('Error deleting now playing message:', error);
}
}
player.nowPlayingMessage = null;
}
});
aqua.on('trackError', async (player, track, payload) => {
console.error(`Error ${payload} / ${payload}`);
const channel = client.channels.cache.get(player.textChannel);
if (channel && player.nowPlayingMessage) {
const embed = new EmbedBuilder()
.setColor(0xff0000)
.setTitle("❌ Error Playing Track")
.setDescription(`Error playing track: \`${track.info.title}\`\nMessage: \`${payload.exception.message}\``)
.setFooter({ text: "Kenium v2.4.0 | by mushroom0162" })
.setTimestamp();
const message = await channel.send({ embeds: [embed] });
setTimeout(() => message.delete().catch(console.error), 5000);
}
});
client.aqua = aqua;
// Update the voice state
client.on("raw", (d) => {
if (![GatewayDispatchEvents.VoiceStateUpdate, GatewayDispatchEvents.VoiceServerUpdate].includes(d.t)) return;
client.aqua.updateVoiceState(d);
});
// Node connection events
client.aqua.on('nodeConnect', (node) => {
console.log(`Node "${node.name}" connected.`);
});
client.aqua.on('nodeError', (node, error) => {
console.error(`Node "${node.name}" encountered an error: ${error.message}.`);
});
// Collections for commands and events
client.slashCommands = new Collection();
client.events = new Collection();
client.buttonCommands = new Collection();
client.selectMenus = new Collection();
// Load handlers
await Promise.all([
import("./src/handlers/Command.mjs").then(({ CommandHandler }) => CommandHandler(client, rootPath)),
import("./src/handlers/Events.mjs").then(({ EventHandler }) => EventHandler(client, rootPath)),
import("./src/handlers/Button.mjs").then(({ ButtonHandler }) => ButtonHandler(client, rootPath)),
]);
client.login(token);