-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
277 lines (233 loc) · 9.28 KB
/
bot.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const { Client, Intents } = require('discord.js-selfbot-v13');
const { createAudioPlayer, joinVoiceChannel, createAudioResource, AudioPlayerStatus, VoiceConnectionStatus } = require('@discordjs/voice');
const fs = require('fs');
const yaml = require('js-yaml');
const googleTTS = require('google-tts-api');
const youtubedl = require('youtube-dl-exec');
const lyricsFinder = require('lyrics-finder');
const config = yaml.load(fs.readFileSync('./config.yaml', 'utf8'));
const prefix = config.prefix;
const token = config.token;
const allowedUserIds = config.admin;
let volume = config.volume;
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], checkUpdate: false });
let player = createAudioPlayer();
let connections = new Map();
let loopSong = null;
let connection = null;
let currentTrack = null;
client.on('ready', async () => {
console.log(`${client.user.username} is ready!`);
});
client.on('messageCreate', async (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
if (!allowedUserIds.includes(message.author.id)) {
return console.log(`Not Allowed by ${message.author.tag} | ${message} | In ${message.channel.name} (${message.guild.name})`);
}
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (command === 'play') {
let channelId = message.member.voice.channel ? message.member.voice.channel.id : null;
let musicName;
if (channelId) {
musicName = args.join(' ');
} else if (args.length >= 2) {
channelId = args[0];
musicName = args.slice(1).join(' ');
}
if (!channelId) {
return message.reply('You need to be in a voice channel to play music or provide a channel ID!');
}
if (!musicName) {
return message.channel.send("You need to provide a music name.");
}
const musicPath = `./musics/${musicName}.mp3`;
if (!fs.existsSync(musicPath)) {
try {
message.channel.send(`Song Not Found | Downloading **${musicName}** | Will Play it Shortly`);
await youtubedl(`ytsearch:${musicName}`, {
extractAudio: true,
audioFormat: 'mp3',
output: musicPath,
});
console.log(`Downloaded ${musicName}.mp3`);
connection = joinVoiceChannel({
channelId: channelId,
guildId: message.guild.id,
selfDeaf: false,
adapterCreator: message.guild.voiceAdapterCreator,
});
connections.set(channelId, connection);
const resource = createAudioResource(musicPath, { inlineVolume: true });
resource.volume.setVolume(volume);
player = createAudioPlayer();
player.play(resource);
connection.subscribe(player);
player.on('error', error => {
console.error('Audio player error:', error);
});
message.channel.send(`Now Playing: **${musicName}** in <#${channelId}>`);
currentTrack = musicName;
} catch (error) {
console.error('Error downloading the song:', error);
message.channel.send("An error occurred while trying to download the song.");
}
} else {
connection = joinVoiceChannel({
channelId: channelId,
guildId: message.guild.id,
selfDeaf: false,
adapterCreator: message.guild.voiceAdapterCreator,
});
connections.set(channelId, connection);
const resource = createAudioResource(musicPath, { inlineVolume: true });
resource.volume.setVolume(volume);
player = createAudioPlayer();
player.play(resource);
connection.subscribe(player);
player.on('error', error => {
console.error('Audio player error:', error);
});
message.channel.send(`Now Playing: **${musicName}** in <#${channelId}>`);
currentTrack = musicName;
}
} else if (command === 'tts') {
let channelId = message.member.voice.channel ? message.member.voice.channel.id : null;
let text;
if (channelId) {
text = args.join(' ');
} else if (args.length >= 2) {
channelId = args[0];
text = args.slice(1).join(' ');
}
if (!channelId) {
return message.reply('You need to be in a voice channel to play TTS or provide a channel ID!');
}
if (!text) {
return message.channel.send("You need to provide a Text to Speech.");
}
try {
const url = googleTTS.getAudioUrl(text, {
lang: config.TTS.lang,
slow: config.TTS.slow,
host: 'https://translate.google.com',
});
let connection = connections.get(channelId);
if (!connection) {
connection = joinVoiceChannel({
channelId: channelId,
guildId: message.guild.id,
selfDeaf: false,
adapterCreator: message.guild.voiceAdapterCreator,
});
connections.set(channelId, connection);
}
const resource = createAudioResource(url, { inlineVolume: true });
resource.volume.setVolume(volume);
player.play(resource);
connection.subscribe(player);
player.on('error', error => {
console.error('Audio player error:', error);
});
player.on(AudioPlayerStatus.Idle, () => {
console.log(`Finished playing TTS message in channel ${channelId}.`);
});
console.log(`Playing TTS message in channel ${channelId}.`);
} catch (error) {
console.error('Error joining the voice channel:', error);
message.channel.send("An error occurred while trying to join the voice channel.");
}
} else if (command === 'nowplaying') {
if (currentTrack) {
message.channel.send(`Now Playing: **${currentTrack}**`);
} else {
message.channel.send("No track is currently playing.");
}
} else if (command === 'stop') {
let channelId = message.member.voice.channel ? message.member.voice.channel.id : null;
if (!channelId && args.length >= 1) {
channelId = args[0];
}
if (!channelId) {
return message.reply('You need to be in a voice channel to stop the music or provide a channel ID!');
}
try {
const connection = connections.get(channelId);
if (connection) {
connection.destroy();
connections.delete(channelId);
if (player) {
player.stop();
}
message.channel.send(`Disconnected from <#${channelId}>`);
} else {
message.channel.send(`Bot is not connected to channel <#${channelId}>`);
}
} catch (error) {
console.error('Error disconnecting from the voice channel:', error);
message.channel.send("An error occurred while trying to disconnect from the voice channel.");
}
}
else if (command === 'loop') {
const musicName = args.slice(0).join(' ');
if (!musicName) {
return message.channel.send("You need to provide a music name.");
}
loopSong = musicName;
message.channel.send(`Now Looping: **${musicName}**`);
} else if (command === 'unloop') {
loopSong = null;
message.channel.send(`Successfully Stopped the Current Loop`);
} else if (command === 'help') {
message.channel.send(`
**Available Commands:**
-# channelid is only Required if you are not in VC
\`${prefix}play (channelid) (musicname)\` - Play a music file in the specified voice channel.
\`${prefix}stop (channelid)\` - Disconnect from the specified voice channel.
\`${prefix}nowplaying\` - Display the currently playing track.
\`${prefix}loop (musicname)\` - Loop the specified music file.
\`${prefix}unloop\` - Stop looping the current music file.
\`${prefix}tts (channelid) (text)\` - Speaks the Text you Type in Voice Channel.
\`${prefix}volume (level)\` - Set the volume level (0 to 1000).
\`${prefix}lyrics (musicname)\` - Fetch and display lyrics for the specified or currently playing track. (Only Works for songs having lyrics)
\`${prefix}help\` - Display this help message.
`);
} else if (command === 'lyrics') {
const musicName = args.join(' ') || currentTrack;
if (!musicName) {
return message.channel.send("You need to specify a track name or be playing a track.");
}
lyricsFinder(musicName).then((lyrics) => {
if (lyrics) {
message.channel.send(`# **Lyrics for ${musicName}**:\n${lyrics}`);
} else {
message.channel.send(`Lyrics not found for **${musicName}**.`);
}
}).catch((error) => {
console.error('Error fetching lyrics:', error);
message.channel.send("An error occurred while trying to fetch the lyrics.");
});
} else if (command === 'volume') {
const volumeLevel = parseFloat(args[0]);
if (isNaN(volumeLevel) || volumeLevel < 0 || volumeLevel > 1000) {
return message.channel.send("You need to provide a valid volume level between 0% and 1000%");
}
volume = volumeLevel / 100;
message.channel.send(`Volume Set to **${volumeLevel}**%.`);
if (player.state.status === AudioPlayerStatus.Playing && player.state.resource.volume) {
player.state.resource.volume.setVolume(volume);
}
}
if (player) {
player.on(AudioPlayerStatus.Idle, () => {
if (loopSong) {
const resource = createAudioResource(`./musics/${loopSong}.mp3`, { inlineVolume: true });
resource.volume.setVolume(volume);
player.play(resource);
}
});
}
});
client.login(token).catch((err) => {
console.error(`Error logging in with token "${token}":`, err);
});