Add more commands
This commit is contained in:
@@ -14,7 +14,8 @@ async function getMusicStream(query) {
|
||||
stream = await spotify.getStream(query);
|
||||
songTitle = stream.title;
|
||||
songDuration = stream.duration;
|
||||
stream = stream.stream;
|
||||
stream = stream?.stream;
|
||||
type = StreamType.Opus;
|
||||
} else if (query.includes('soundcloud.com')) {
|
||||
stream = await soundcloud.getStream(query);
|
||||
songTitle = stream.title;
|
||||
|
||||
57
utils/logger.js
Normal file
57
utils/logger.js
Normal file
@@ -0,0 +1,57 @@
|
||||
/* eslint-disable no-console */
|
||||
function loggerDate() {
|
||||
const date = new Date();
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
|
||||
const hours = date.getHours();
|
||||
const minutes = date.getMinutes();
|
||||
const seconds = date.getSeconds();
|
||||
|
||||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||||
}
|
||||
|
||||
class ConsolerLogger {
|
||||
constructor() {
|
||||
this.colors = {
|
||||
info: '\x1b[36m%s\x1b[0m',
|
||||
success: '\x1b[32m%s\x1b[0m',
|
||||
warning: '\x1b[33m%s\x1b[0m',
|
||||
error: '\x1b[31m%s\x1b[0m',
|
||||
register: '\x1b[35m',
|
||||
add: '\x1b[36m',
|
||||
log: '\x1b[37m',
|
||||
};
|
||||
}
|
||||
|
||||
info(...messages) {
|
||||
console.log(this.colors.info, loggerDate(), ...messages);
|
||||
}
|
||||
|
||||
success(...messages) {
|
||||
console.log(this.colors.success, loggerDate(), ...messages);
|
||||
}
|
||||
|
||||
warning(...messages) {
|
||||
console.log(this.colors.warning, loggerDate(), ...messages);
|
||||
}
|
||||
|
||||
error(...messages) {
|
||||
console.log(this.colors.error, loggerDate(), ...messages);
|
||||
}
|
||||
|
||||
register(...messages) {
|
||||
console.log(this.colors.register, loggerDate(), ...messages);
|
||||
}
|
||||
|
||||
add(...messages) {
|
||||
console.log(this.colors.add, loggerDate(), ...messages);
|
||||
}
|
||||
|
||||
log(...messages) {
|
||||
console.log(this.colors.log, loggerDate(), ...messages);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ConsolerLogger;
|
||||
@@ -10,80 +10,23 @@ const process = require('dotenv').config();
|
||||
|
||||
const { clientId } = process.parsed;
|
||||
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const musicQueue = require('../musicQueue');
|
||||
const { progressBar } = require('./progress');
|
||||
const { nowPlayingMessage, intervalMap, currentInteractionIds } = require('./nowPlayingMessage');
|
||||
const ConsolerLogger = require('./logger');
|
||||
|
||||
const currentInteractionIds = new Map();
|
||||
const currentInteractions = new Map();
|
||||
const oldConnections = [];
|
||||
const timeoutTimer = new Map();
|
||||
|
||||
// TODO FIX THIS SHIT!!! ISSUES WITH DISPLAYING NAME AND STATUS WHEN UPDATING
|
||||
function nowPLayingMessage(interaction, song, oldInteractionId) {
|
||||
progressBar(0, 0, true);
|
||||
|
||||
if (interaction.commandName === 'play') {
|
||||
interaction.followUp('~🎵~').then((message) => {
|
||||
const songTitle = song.title;
|
||||
// const embed = new EmbedBuilder()
|
||||
// .setColor('#E0B0FF')
|
||||
// .setTitle(`Now playing: ${songTitle}`)
|
||||
// .setDescription(
|
||||
// progressBar(song.duration, 10).progressBarString,
|
||||
// );
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor('#E0B0FF')
|
||||
.setTitle(`Now playing: ${songTitle}`);
|
||||
|
||||
message.edit({
|
||||
embeds: [embed],
|
||||
});
|
||||
|
||||
const inter = setInterval(async () => {
|
||||
const { progressBarString, isDone } = progressBar(
|
||||
song.duration,
|
||||
10,
|
||||
);
|
||||
if (isDone || message.id !== oldInteractionId) {
|
||||
// clearInterval(inter);
|
||||
return;
|
||||
}
|
||||
|
||||
if (message.id != null && interaction.guild.id !== oldInteractionId) {
|
||||
interaction.channel.messages.fetch().then(async (channel) => {
|
||||
const filter = channel.filter((msg) => msg.author.id === clientId);
|
||||
const latestMessage = await interaction.channel.messages.fetch(filter.first().id);
|
||||
latestMessage.edit({
|
||||
embeds: [embed.setTitle(`Now playing: ${songTitle}`)],
|
||||
});
|
||||
});
|
||||
}
|
||||
}, 2000);
|
||||
|
||||
currentInteractionIds.set(interaction.guild.id, interaction);
|
||||
currentInteractions.set(interaction.guild.id, interaction.id);
|
||||
});
|
||||
}
|
||||
}
|
||||
const logger = new ConsolerLogger();
|
||||
|
||||
async function musicPlayer(guildId, connection, interaction) {
|
||||
try {
|
||||
const oldInteractions = await currentInteractions.get(interaction.guild.id);
|
||||
const oldInteractionId = await currentInteractionIds.get(interaction.guild.id);
|
||||
const serverQueue = musicQueue.getQueue(guildId);
|
||||
const oldConnection = oldConnections
|
||||
.find((guidConnection) => guidConnection[0] === interaction.guild.id);
|
||||
|
||||
if (serverQueue.length === 0) {
|
||||
oldConnection[1].destroy();
|
||||
}
|
||||
|
||||
const song = serverQueue[0];
|
||||
|
||||
if (song.stream === undefined) {
|
||||
musicQueue.removeFromQueue(guildId);
|
||||
musicPlayer(guildId, connection);
|
||||
if (!song || song.stream === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -97,30 +40,44 @@ async function musicPlayer(guildId, connection, interaction) {
|
||||
},
|
||||
});
|
||||
|
||||
player.play(resource);
|
||||
if (!resource.ended) {
|
||||
player.play(resource);
|
||||
} else {
|
||||
logger.warning('Song ended prematurely.', song.title);
|
||||
}
|
||||
|
||||
connection.subscribe(player);
|
||||
|
||||
nowPLayingMessage(interaction, song, oldInteractions);
|
||||
nowPlayingMessage(interaction, song);
|
||||
|
||||
oldConnections.push([interaction.guild.id, connection]);
|
||||
|
||||
// Add an event listener for the Idle event of the audio player
|
||||
player.on(AudioPlayerStatus.Idle, async () => {
|
||||
console.log('Song ended:', song.title);
|
||||
if (serverQueue.length !== 1) {
|
||||
await musicQueue.removeFromQueue(guildId);
|
||||
musicPlayer(guildId, connection, interaction);
|
||||
}
|
||||
logger.info(`Song ended: ${song.title}`, 'Started by:', interaction.user.username);
|
||||
// Check if the audio resource has ended
|
||||
if (resource.ended) {
|
||||
// If the audio resource has ended, play the next song
|
||||
|
||||
// timeoutTimer.set(guildId, setTimeout(async () => {
|
||||
// await musicQueue.removeFromQueue(guildId);
|
||||
// connection.destroy();
|
||||
// }, 10000));
|
||||
if (serverQueue.length !== 0) {
|
||||
await musicQueue.removeFromQueue(guildId);
|
||||
musicPlayer(guildId, connection, interaction);
|
||||
logger.info(`Playing next song...${serverQueue}`);
|
||||
} else {
|
||||
// If there are no more songs in the queue, destroy the connection
|
||||
connection.destroy();
|
||||
logger.info('Connection destroyed.');
|
||||
// Clear the interval for updating the progress bar
|
||||
const interval = intervalMap.get(interaction.guild.id);
|
||||
clearInterval(interval);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
player.on(AudioPlayerStatus.Playing, () => {
|
||||
console.log('pausing timer');
|
||||
logger.info(`Playing: ${song.title}, Started by: ${interaction.user.username}`);
|
||||
clearTimeout(
|
||||
logger.info('Previous song timer cleared.'),
|
||||
timeoutTimer.get(guildId),
|
||||
);
|
||||
});
|
||||
@@ -130,13 +87,15 @@ async function musicPlayer(guildId, connection, interaction) {
|
||||
const { lastMessage } = oldInteractionId.channel;
|
||||
const filter = channel.filter((message) => message.author.id === clientId && message.id !== lastMessage.id);
|
||||
setTimeout(() => {
|
||||
oldInteractionId.channel.bulkDelete(filter);
|
||||
logger.info('Removing old messages...');
|
||||
oldInteractionId.channel.bulkDelete(filter)
|
||||
.catch((error) => logger.error(error));
|
||||
}, 1000);
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
interaction.followUp('There was an error playing the song.');
|
||||
logger.error(error);
|
||||
interaction.followUp('There was an error playing the song.', { ephemeral: true });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
68
utils/nowPlayingMessage.js
Normal file
68
utils/nowPlayingMessage.js
Normal file
@@ -0,0 +1,68 @@
|
||||
const process = require('dotenv').config();
|
||||
|
||||
const { clientId } = process.parsed;
|
||||
const { EmbedBuilder } = require('discord.js');
|
||||
const { progressBar } = require('./progress');
|
||||
|
||||
const currentInteractionIds = new Map();
|
||||
const currentInteractions = new Map();
|
||||
const messageTimerMap = new Map();
|
||||
const intervalMap = new Map(); // Add a new map to keep track of interval IDs
|
||||
|
||||
function nowPlayingMessage(interaction, song, prematureEnd = false) {
|
||||
const timeoutIDs = messageTimerMap.get(interaction.guild.id);
|
||||
if (timeoutIDs) {
|
||||
timeoutIDs.forEach((timeoutID) => clearTimeout(timeoutID));
|
||||
}
|
||||
|
||||
if (interaction.commandName === 'play') {
|
||||
interaction.followUp('~🎵~').then((message) => {
|
||||
const songTitle = song.title;
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor('#E0B0FF')
|
||||
.setTitle(`Now playing: ${songTitle}`)
|
||||
.setDescription(
|
||||
progressBar(song.duration, 10).progressBarString,
|
||||
);
|
||||
|
||||
message.edit({
|
||||
embeds: [embed],
|
||||
});
|
||||
|
||||
const inter = setInterval(async () => {
|
||||
const messageString = progressBar(
|
||||
song.duration,
|
||||
10,
|
||||
);
|
||||
|
||||
if (message.id != null) {
|
||||
interaction.channel.messages.fetch().then(async (channel) => {
|
||||
const filter = channel.filter((msg) => msg.author.id === clientId);
|
||||
const latestMessage = await interaction.channel.messages.fetch(filter.first().id);
|
||||
latestMessage.edit({
|
||||
embeds: [embed.setDescription(messageString.progressBarString)],
|
||||
});
|
||||
});
|
||||
}
|
||||
}, 1000);
|
||||
|
||||
// Store the interval ID in the intervalMap
|
||||
intervalMap.set(interaction.guild.id, inter);
|
||||
|
||||
// Store the timeoutID in an array associated with the key
|
||||
if (!messageTimerMap.has(interaction.guild.id) || !prematureEnd) {
|
||||
messageTimerMap.set(interaction.guild.id, []);
|
||||
}
|
||||
|
||||
messageTimerMap.get(interaction.guild.id).push(inter);
|
||||
progressBar(0, 0, true);
|
||||
|
||||
currentInteractionIds.set(interaction.guild.id, interaction);
|
||||
currentInteractions.set(interaction.guild.id, interaction.id);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.nowPlayingMessage = nowPlayingMessage;
|
||||
module.exports.intervalMap = intervalMap;
|
||||
module.exports.currentInteractionIds = currentInteractionIds;
|
||||
@@ -30,8 +30,9 @@ const progressBar = (totalInMilliseconds, size, reset = false) => {
|
||||
totalTimeText = totalTimeText.slice(3);
|
||||
}
|
||||
|
||||
const progressBarString = `${elapsedTimeText} \`\`\`${progressText}${emptyProgressText}\`\`\` ${totalTimeText}`; // Creating and returning the bar
|
||||
const progressBarString = `${elapsedTimeText} \`${progressText}${emptyProgressText}\` ${totalTimeText}`; // Creating and returning the bar
|
||||
|
||||
return { progressBarString, isDone: percentage === 1 };
|
||||
};
|
||||
|
||||
module.exports.progressBar = progressBar;
|
||||
@@ -1,6 +1,9 @@
|
||||
const { SlashCommandBuilder } = require('@discordjs/builders');
|
||||
const { REST } = require('@discordjs/rest');
|
||||
const { Routes } = require('discord-api-types/v9');
|
||||
const ConsolerLogger = require('./logger');
|
||||
|
||||
const logger = new ConsolerLogger();
|
||||
|
||||
async function registerCommands(clientId, token) {
|
||||
const commands = [
|
||||
@@ -32,6 +35,15 @@ async function registerCommands(clientId, token) {
|
||||
new SlashCommandBuilder()
|
||||
.setName('stop')
|
||||
.setDescription('Stops the current song!'),
|
||||
new SlashCommandBuilder()
|
||||
.setName('skip')
|
||||
.setDescription('Skips the current song!'),
|
||||
new SlashCommandBuilder()
|
||||
.setName('leave')
|
||||
.setDescription('Leaves the voice channel!'),
|
||||
new SlashCommandBuilder()
|
||||
.setName('previous')
|
||||
.setDescription('Plays the previous song!'),
|
||||
];
|
||||
|
||||
const rest = new REST({
|
||||
@@ -40,15 +52,15 @@ async function registerCommands(clientId, token) {
|
||||
.setToken(token);
|
||||
|
||||
try {
|
||||
console.log('\x1b[35m', 'Started refreshing application (/) commands.');
|
||||
logger.register('Started refreshing application (/) commands.');
|
||||
|
||||
await rest.put(Routes.applicationCommands(clientId), {
|
||||
body: commands,
|
||||
});
|
||||
|
||||
console.log('\x1b[35m', 'Successfully reloaded application (/) commands.');
|
||||
logger.register('Successfully reloaded application (/) commands.');
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
logger.error('Failed to reload application (/) commands.');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
8
utils/setStatus.js
Normal file
8
utils/setStatus.js
Normal file
@@ -0,0 +1,8 @@
|
||||
const { botClient } = require('../index');
|
||||
|
||||
async function setStatus(status) {
|
||||
await botClient.user.setActivity(status);
|
||||
}
|
||||
|
||||
module.exports.setStatus = setStatus;
|
||||
module.exports.setStatus = setStatus;
|
||||
Reference in New Issue
Block a user