-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (161 loc) · 7.4 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
// Add this line at the top of your file
require('dotenv').config()
const { Client, GatewayIntentBits } = require('discord.js');
const { joinVoiceChannel, createAudioPlayer, createAudioResource, AudioPlayerStatus } = require('@discordjs/voice');
const ytdl = require('@distube/ytdl-core');
const yts = require('yt-search');
const SpotifyWebApi = require('spotify-web-api-node');
const stringSimilarity = require('string-similarity');
const config = require("./config.json");
const fs = require("fs");
const { connect } = require('http2');
const agent = ytdl.createAgent(JSON.parse(fs.readFileSync("cookies.json")));
let OpusEncoder;
try {
OpusEncoder = require('@discordjs/opus');
} catch (err) {
try {
OpusEncoder = require('opusscript');
} catch (e) {
console.error('Neither @discordjs/opus nor opusscript are installed. Please install one of them to play audio.');
process.exit(1);
}
}
// Create a new Discord client
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildVoiceStates] });
function cleanSongTitle(title) {
// Regular expression to remove content within parentheses, square brackets, and "feat." clauses
return title.replace(/\s*\(.*?\)|\s*.∗?.*?|\s*feat\..*/gi, '').trim();
}
// Spotify API setup
const spotifyApi = new SpotifyWebApi({
clientId: config.SPOTIFY_CLIENT_ID,
clientSecret: config.SPOTIFY_CLIENT_SECRET,
});
// Get Spotify access token
spotifyApi.clientCredentialsGrant().then(
data => spotifyApi.setAccessToken(data.body['access_token']),
err => console.log('Something went wrong when retrieving an access token', err)
);
// Points system
let points = {};
let roundWinners = [];
function displayLeaderboard(message) {
const sortedPoints = Object.entries(points).sort(([, a], [, b]) => b - a);
let leaderboardMessage = 'Leaderboard:\n';
sortedPoints.forEach(([userId, score]) => {
const user = client.users.cache.get(userId);
leaderboardMessage += `${user ? user.tag : 'Unknown User'}: ${score} point(s)\n`;
});
message.channel.send(leaderboardMessage);
}
function displayRoundResults(message) {
const sortedPoints = Object.entries(points).sort(([, a], [, b]) => b - a);
if (sortedPoints.length === 0) {
message.channel.send('No one won this round.');
return;
}
const [topPlayerId, topPlayerPoints] = sortedPoints[0];
const topPlayer = client.users.cache.get(topPlayerId);
if (topPlayer) {
message.channel.send(`${topPlayer.tag} has the highest points with ${topPlayerPoints} point(s)!`);
} else {
message.channel.send('Could not find the top player.');
}
}
// On bot ready
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async (message) => {
if (message.content.startsWith('!play')) {
const args = message.content.split(' ');
if (args.length < 3) {
return message.reply('Usage: !play <spotify-playlist-link> <numofsongs>');
}
const playlistUrl = args[1];
const numOfSongs = parseInt(args[2], 10);
if (isNaN(numOfSongs) || numOfSongs <= 0) {
return message.reply('Please provide a valid number of songs to play.');
}
const playlistIdMatch = playlistUrl.match(/playlist\/(.*)\?/);
if (!playlistIdMatch) {
return message.reply('Invalid Spotify playlist link.');
}
const playlistId = playlistIdMatch[1];
// Fetch playlist tracks
const playlist = await spotifyApi.getPlaylistTracks(playlistId);
// Number of songs to play
if (message.member.voice.channel) {
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator,
});
let totalSongs=numOfSongs;
for (let i = 0; i < totalSongs; i++) {
let random = Math.floor(Math.random() * playlist.body.total);
const trackNumber = random;
const track = playlist.body.items[trackNumber].track;
if(!track){
continue;
}
// Search for the song on YouTube
const searchResult = await yts(`${track.name} ${track.artists[0].name}` );
const songInfo = searchResult.videos[0];
console.log(songInfo);
if (!songInfo) {
message.reply(`Couldn't find ${track.name} on YouTube.`);
continue;
}
const videoLengthSeconds = songInfo.seconds;
const startTime = Math.max(0, Math.floor(Math.random() * (videoLengthSeconds - 30)));
const resource = createAudioResource(await ytdl(songInfo.url, {
agent,
begin: `${startTime}s`,
filter: 'audioonly',
quality: 'highestaudio',
} ));
const player = createAudioPlayer();
player.play(resource);
connection.subscribe(player);
// Stop the player after 50 seconds if the song isn't guessed
const timeout = setTimeout(() => {
player.stop();
}, 50000); // 50 seconds
// const filter = response => response.content.toLowerCase() === track.name.toLowerCase();
const filter = response => {
const cleanedTitle = cleanSongTitle(track.name.toLowerCase());
const similarity = stringSimilarity.compareTwoStrings(response.content.toLowerCase(), cleanedTitle);
return similarity > 0.7;
};
try {
const collected = await message.channel.awaitMessages({ filter, max: 1, time: 50000, errors: ['time'] });
const winner = collected.first().author;
points[winner.id] = (points[winner.id] || 0) + 1;
roundWinners.push(winner);
message.channel.send(`${winner} guessed it right! The song was ${track.name}. They now have ${points[winner.id]} point(s).`);
clearTimeout(timeout);
player.stop();
} catch (err) {
console.log(err);
message.channel.send(`Time's up! The song was ${track.name}.`);
}
await new Promise(resolve => player.on(AudioPlayerStatus.Idle, resolve)); // Wait for the player to stop before moving to the next song
displayLeaderboard(message);
}
connection.destroy();
message.channel.send('Game over! Thanks for playing.');
displayRoundResults(message);
points={};
} else {
message.reply('You need to join a voice channel first!');
}
}
// Check points command
if (message.content.startsWith('!points')) {
const userPoints = points[message.author.id] || 0;
message.reply(`You have ${userPoints} point(s).`);
}
});
client.login(config.KEY);//DISCORD API KEY