Фикс loop
Выбор при поиске
Фикс queue
Фикс автовоспроизведения
This commit is contained in:
JonnyBro 2022-08-28 16:45:05 +05:00
parent 9136500baa
commit e8ab25d80c
12 changed files with 2001 additions and 1627 deletions

View file

@ -248,16 +248,14 @@ class JaBa extends Client {
* @param {Array} args
* @param {String} locale
*/
translate(key, args, locale) {
if (!locale) locale = this.defaultLanguage;
translate(key, args, locale = this.defaultLanguage) {
const language = this.translations.get(locale);
if (!language) throw "Invalid language set in data.";
return language(key, args);
}
printDate(date, format, locale) {
if (!locale) locale = this.defaultLanguage;
printDate(date, format = false, locale = this.defaultLanguage) {
const languageData = this.languages.find((language) => language.name === locale || language.aliases.includes(locale));
if (!format) format = languageData.defaultMomentFormat;
@ -266,9 +264,7 @@ class JaBa extends Client {
.format(format);
}
convertTime(time, type, noPrefix, locale) {
if (!type) type = false;
if (!locale) locale = this.defaultLanguage;
convertTime(time, type = false, noPrefix = false, locale = this.defaultLanguage) {
const languageData = this.languages.find((language) => language.name === locale || language.aliases.includes(locale));
const m = moment(time).locale(languageData.moment);

View file

@ -1,4 +1,4 @@
const { SlashCommandBuilder, ActionRowBuilder, SelectMenuBuilder, InteractionCollector, ComponentType } = require("discord.js"),
const { SlashCommandBuilder, ActionRowBuilder, SelectMenuBuilder } = require("discord.js"),
{ QueueRepeatMode } = require("discord-player");
const BaseCommand = require("../../base/BaseCommand");
@ -32,6 +32,8 @@ class Loop extends BaseCommand {
* @param {Object} data
*/
async execute(client, interaction) {
await interaction.deferReply();
const voice = interaction.member.voice.channel;
if (!voice) return interaction.error("music/play:NO_VOICE_CHANNEL");
const queue = client.player.getQueue(interaction.guildId);
@ -62,26 +64,23 @@ class Loop extends BaseCommand {
])
);
const msg = await interaction.reply({
await interaction.editReply({
content: interaction.translate("common:AVAILABLE_OPTIONS"),
components: [row],
fetchReply: true
components: [row]
});
const collector = new InteractionCollector(client, {
componentType: ComponentType.SelectMenu,
message: msg,
idle: (2 * 1000)
});
const filter = i => i.user.id === interaction.user.id;
const collector = interaction.channel.createMessageComponentCollector({ filter, idle: (15 * 1000) });
collector.on("collect", async i => {
if (i.isSelectMenu()) {
const type = i?.values[0];
const mode = type === "3" ? QueueRepeatMode.AUTOPLAY :
type === "2" ? QueueRepeatMode.QUEUE :
type === "1" ? QueueRepeatMode.TRACK : QueueRepeatMode.OFF;
queue.setRepeatMode(mode);
return i.update({
return interaction.editReply({
content: interaction.translate(`music/loop:${
type === "3" ? "AUTOPLAY_ENABLED" :
type === "2" ? "QUEUE_ENABLED" :
@ -89,14 +88,11 @@ class Loop extends BaseCommand {
}`),
components: []
});
}
});
collector.on("end", (_, reason) => {
if (reason === "idle") {
if (msg) msg.update({
components: []
});
}
if (reason) interaction.deleteReply();
});
}
}

View file

@ -42,7 +42,7 @@ class Nowplaying extends BaseCommand {
.setAuthor({
name: interaction.translate("music/queue:TITLE")
})
.setThumbnail(track.thumbnail || "https://cdn.discordapp.com/attachments/708642702602010684/1012418217660121089/noimage.png")
.setThumbnail(track.thumbnail)
.addFields([
{
name: interaction.translate("music/nowplaying:T_TITLE"),

View file

@ -1,4 +1,4 @@
const { SlashCommandBuilder, PermissionsBitField } = require("discord.js");
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, PermissionsBitField } = require("discord.js");
const BaseCommand = require("../../base/BaseCommand");
class Play extends BaseCommand {
@ -61,6 +61,154 @@ class Play extends BaseCommand {
spotifyBridge: false
});
const searched = searchResult.tracks[0].description === "search";
if (searched) {
const row1 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("1")
.setStyle(ButtonStyle.Primary)
.setEmoji("1⃣"),
new ButtonBuilder()
.setCustomId("2")
.setStyle(ButtonStyle.Primary)
.setEmoji("2⃣"),
new ButtonBuilder()
.setCustomId("3")
.setStyle(ButtonStyle.Primary)
.setEmoji("3⃣"),
new ButtonBuilder()
.setCustomId("4")
.setStyle(ButtonStyle.Primary)
.setEmoji("4⃣"),
new ButtonBuilder()
.setCustomId("5")
.setStyle(ButtonStyle.Primary)
.setEmoji("5⃣"),
);
const row2 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("6")
.setStyle(ButtonStyle.Primary)
.setEmoji("6⃣"),
new ButtonBuilder()
.setCustomId("7")
.setStyle(ButtonStyle.Primary)
.setEmoji("7⃣"),
new ButtonBuilder()
.setCustomId("8")
.setStyle(ButtonStyle.Primary)
.setEmoji("8⃣"),
new ButtonBuilder()
.setCustomId("9")
.setStyle(ButtonStyle.Primary)
.setEmoji("9⃣"),
new ButtonBuilder()
.setCustomId("10")
.setStyle(ButtonStyle.Primary)
.setEmoji("🔟"),
);
const row3 = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("search_cancel")
.setLabel(interaction.translate("common:CANCEL"))
.setStyle(ButtonStyle.Danger)
.setEmoji("❌"),
);
const rows = [row1, row2, row3];
const embed = new EmbedBuilder()
.setTitle(interaction.translate("music/play:RESULTS_TITLE", {
query
}))
.setThumbnail(interaction.client.user.avatarURL())
.setColor(client.config.embed.color)
.setDescription(searchResult.tracks.map(track => {
const views = new Intl.NumberFormat(interaction.client.languages.find(language => language.name === interaction.guild.data.language).moment, {
notation: "compact", compactDisplay: "short"
}).format(track.views);
return `${searchResult.tracks.indexOf(track) + 1}. [${track.title}](${track.url})\n> ${interaction.translate("common:VIEWS")}: **${views}**`;
}).join("\n"))
.setTimestamp();
await interaction.editReply({
content: interaction.translate("music/play:SEARCH_RESULTS"),
embeds: [embed],
components: [row1, row2, row3]
});
const filter = i => i.user.id === interaction.user.id;
const collector = interaction.channel.createMessageComponentCollector({ filter, idle: (15 * 1000) });
collector.on("collect", async i => {
if (i.isButton()) {
if (i.customId >= 1 && i.customId <= 10) {
i.deferUpdate();
var selected = searchResult.tracks[i.customId - 1];
queue.addTrack(selected);
try {
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
if (!queue.playing) await queue.play();
rows.forEach(row => {
row.components.forEach(component => {
component.setDisabled(true);
});
});
return interaction.editReply({
content: interaction.translate("music/play:ADDED_QUEUE", {
songName: selected.title
}),
components: [row1, row2, row3]
});
} catch (e) {
client.player.deleteQueue(interaction.guildId);
console.error(e);
return interaction.editReply({
content: interaction.translate("music/play:ERR_OCCURRED", {
error: e
})
});
}
} else if (i.customId === "search_cancel") {
i.deferUpdate();
rows.forEach(row => {
row.components.forEach(component => {
component.setDisabled(true);
});
});
return interaction.editReply({
components: [row1, row2, row3]
});
}
}
});
collector.on("end", async (_, reason) => {
if (reason) {
rows.forEach(row => {
row.components.forEach(component => {
component.setDisabled(true);
});
});
return interaction.editReply({
components: [row1, row2, row3]
});
}
});
return;
}
searchResult.playlist ? queue.addTracks(searchResult.tracks) : queue.addTrack(searchResult.tracks[0]);
try {

View file

@ -1,4 +1,5 @@
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js");
const { SlashCommandBuilder, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require("discord.js"),
{ QueueRepeatMode } = require("discord-player");
const BaseCommand = require("../../base/BaseCommand");
class Queue extends BaseCommand {
@ -121,8 +122,8 @@ class Queue extends BaseCommand {
};
interaction.channel.awaitMessages({ filter, max: 1, time: (30 * 1000) }).then(collected => {
if (embeds[Number(collected.first().content) - 1]) {
currentPage = Number(collected.first().content) - 1;
if (embeds[collected.first().content - 1]) {
currentPage = collected.first().content - 1;
interaction.editReply({
content: interaction.translate("music/queue:PAGE", {
current: currentPage + 1,
@ -149,33 +150,10 @@ class Queue extends BaseCommand {
collector.on("end", async (_, reason) => {
if (reason) {
const row = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setCustomId("queue_prev_page")
.setLabel(interaction.translate("music/queue:PREV_PAGE"))
.setStyle(ButtonStyle.Primary)
.setEmoji("⬅️")
.setDisabled(true),
new ButtonBuilder()
.setCustomId("queue_next_page")
.setLabel(interaction.translate("music/queue:NEXT_PAGE"))
.setStyle(ButtonStyle.Primary)
.setEmoji("➡️")
.setDisabled(true),
new ButtonBuilder()
.setCustomId("queue_jump_page")
.setLabel(interaction.translate("music/queue:JUMP_PAGE"))
.setStyle(ButtonStyle.Secondary)
.setEmoji("↗️")
.setDisabled(true),
new ButtonBuilder()
.setCustomId("queue_stop")
.setLabel(interaction.translate("common:CANCEL"))
.setStyle(ButtonStyle.Danger)
.setEmoji("⏹️")
.setDisabled(true),
);
row.components.forEach(component => {
component.setDisabled(true);
});
return interaction.editReply({
components: [row]
});
@ -184,11 +162,33 @@ class Queue extends BaseCommand {
}
}
/**
*
* @param {import("discord.js").ChatInputCommandInteraction} interaction
* @param {import("discord-player").Queue} queue
* @returns
*/
function generateQueueEmbed(interaction, queue) {
const embeds = [];
const currentTrack = queue.current;
let k = 10;
if (!queue.tracks.length) {
const embed = new EmbedBuilder()
.setTitle(interaction.translate("music/nowplaying:CURRENTLY_PLAYING"))
.setThumbnail(currentTrack.thumbnail)
.setColor(interaction.client.config.embed.color)
.setDescription(`${interaction.translate("music/nowplaying:REPEAT")}: \`${
queue.repeatMode === QueueRepeatMode.AUTOPLAY ? interaction.translate("music/nowplaying:AUTOPLAY") :
queue.repeatMode === QueueRepeatMode.QUEUE ? interaction.translate("music/nowplaying:QUEUE") :
queue.repeatMode === QueueRepeatMode.TRACK ? interaction.translate("music/nowplaying:TRACK") : interaction.translate("common:DISABLED")
}\`\n${interaction.translate("music/queue:DURATION")}: \`${interaction.client.convertTime(Date.now() + queue.totalTime, false, true, interaction.guild.data.language)}\`\n[${currentTrack.title}](${currentTrack.url})\n> ${interaction.translate("music/queue:ADDED")} ${currentTrack.requestedBy}\n\n**${interaction.translate("music/queue:NEXT")}**\n${interaction.translate("music/queue:NO_QUEUE")}`)
.setTimestamp();
embeds.push(embed);
return embeds;
}
for (let i = 0; i < queue.tracks.length; i += 10) {
const current = queue.tracks.slice(i, k);
let j = i;
@ -198,9 +198,13 @@ function generateQueueEmbed(interaction, queue) {
const embed = new EmbedBuilder()
.setTitle(interaction.translate("music/nowplaying:CURRENTLY_PLAYING"))
.setThumbnail(currentTrack.thumbnail || "https://cdn.discordapp.com/attachments/708642702602010684/1012418217660121089/noimage.png")
.setThumbnail(currentTrack.thumbnail)
.setColor(interaction.client.config.embed.color)
.setDescription(`[${currentTrack.title}](${currentTrack.url})\n> ${interaction.translate("music/queue:ADDED")} ${currentTrack.requestedBy}\n\n**${interaction.translate("music/queue:NEXT")}**\n${info}`)
.setDescription(`${interaction.translate("music/nowplaying:REPEAT")}: \`${
queue.repeatMode === QueueRepeatMode.AUTOPLAY ? interaction.translate("music/nowplaying:AUTOPLAY") :
queue.repeatMode === QueueRepeatMode.QUEUE ? interaction.translate("music/nowplaying:QUEUE") :
queue.repeatMode === QueueRepeatMode.TRACK ? interaction.translate("music/nowplaying:TRACK") : interaction.translate("common:DISABLED")
}\`\n${interaction.translate("music/queue:DURATION")}: \`${interaction.client.convertTime(Date.now() + queue.totalTime, false, true, interaction.guild.data.language)}\`\n[${currentTrack.title}](${currentTrack.url})\n> ${interaction.translate("music/queue:ADDED")} ${currentTrack.requestedBy}\n\n**${interaction.translate("music/queue:NEXT")}**\n${info}`)
.setTimestamp();
embeds.push(embed);
}

View file

@ -1,3 +1,14 @@
### JaBa v4.1.2
* Добавлено
* Выбор при поиске текстом через команду *play*.
* Изменения
* Немного переделана команда *loop*.
* Исправления
* Исправлена ошибка команды *queue* если очередь пуста.
* Починено автовоспроизведение.
### JaBa v4.1.1
* Добавлено
* Страницы в *queue*.
@ -55,16 +66,16 @@
* Изменения
* Все команды теперь являются слэш-командами!
* Все команды доступны через **/**.
* С помощью слэш-команд намного удобнее указывать аргументы для команд.
* Запрет на использование слэш-команд настраивается в **Настройки сервера** - **Интеграция** - **JaBa**.
* Команда *avatar* перенесена в категорию *General*.
* Команда *jump* переименована в *skipto*.
* Команда *setafk* переименована в *afk*.
* Команда *lmg* переименована в *lmgtfy*.
* Команда *warn* доступна при нажатии на **пользователя** - **Приложения** - *warn*. В будущем там будет больше команд.
* Автопроигрывание музыки теперь находится в *loop*.
* Все клипы доступны через команду *clips*.
* Все команды доступны через **/**!
* С помощью слэш-команд намного удобнее указывать аргументы для команд!
* Запрет на использование слэш-команд настраивается в **Настройки сервера** - **Интеграция** - **JaBa**!
* Команда *avatar* перенесена в категорию *General*!
* Команда *jump* переименована в *skipto*!
* Команда *setafk* переименована в *afk*!
* Команда *lmg* переименована в *lmgtfy*!
* Команда *warn* доступна при нажатии **на пользователя** - **Приложения** - *warn*! В будущем там будет больше команд.
* Автопроигрывание музыки теперь находится в *loop*!
* Все клипы доступны через команду *clips*!
* Удалено
* Категория *Images*

View file

@ -1,4 +1,5 @@
const playdl = require("play-dl");
const playdl = require("play-dl"),
fallbackImage = "https://cdn.discordapp.com/attachments/708642702602010684/1012418217660121089/noimage.png";
// const fetch = require("node-fetch");
// const { getData, getPreview, getTracks } = require("spotify-url-info")(fetch);
@ -35,7 +36,7 @@ module.exports = {
const track = {
title: info.name,
duration: info.durationInMs,
thumbnail: info.thumbnail,
thumbnail: info.thumbnail || fallbackImage,
async engine() {
return (await playdl.stream(info.url, { discordPlayerCompatibility: true })).stream;
},
@ -52,7 +53,7 @@ module.exports = {
return {
title: track.name,
duration: track.durationInMs,
thumbnail: track.thumbnail,
thumbnail: track.thumbnail || fallbackImage,
async engine() {
return (await playdl.stream(track.url, { discordPlayerCompatibility: true })).stream;
},
@ -66,7 +67,7 @@ module.exports = {
const playlist = {
title: info.name,
description: "",
thumbnail: info.user.thumbnail,
thumbnail: info.user.thumbnail || fallbackImage,
type: "playlist",
source: "soundcloud-custom",
author: info.user.name,
@ -140,7 +141,7 @@ module.exports = {
const track = {
title: info.video_details.title,
duration: info.video_details.durationInSec * 1000,
thumbnail: info.video_details.thumbnails[0].url,
thumbnail: info.video_details.thumbnails[0].url || fallbackImage,
async engine() {
return (await playdl.stream(`https://music.youtube.com/watch?v=${info.video_details.id}`, { discordPlayerCompatibility: true })).stream;
},
@ -148,9 +149,8 @@ module.exports = {
author: info.video_details.channel.name,
description: "",
url: `https://music.youtube.com/watch?v=${info.video_details.id}`,
related_videos: info.related_videos,
raw: info,
source: "youtube-music-custom"
source: "youtube"
};
return resolve({ playlist: null, info: [track] });
@ -162,7 +162,7 @@ module.exports = {
const track = {
title: info.video_details.title,
duration: info.video_details.durationInSec * 1000,
thumbnail: info.video_details.thumbnails[0].url,
thumbnail: info.video_details.thumbnails[0].url || fallbackImage,
async engine() {
return (await playdl.stream(info.video_details.url, { discordPlayerCompatibility: true })).stream;
},
@ -170,9 +170,8 @@ module.exports = {
author: info.video_details.channel.name,
description: "",
url: info.video_details.url,
related_videos: info.related_videos,
raw: info,
source: "youtube-custom"
source: "youtube"
};
return resolve({ playlist: null, info: [track] });
} else if (playdl.yt_validate(query) === "playlist") {
@ -183,7 +182,7 @@ module.exports = {
return {
title: track.title,
duration: track.durationInSec * 1000,
thumbnail: track.thumbnails[0].url,
thumbnail: track.thumbnails[0].url || fallbackImage,
async engine() {
return (await playdl.stream(`https://music.youtube.com/watch?v=${track.id}`, { discordPlayerCompatibility: true })).stream;
},
@ -192,18 +191,18 @@ module.exports = {
description: "",
url: track.url,
raw: info,
source: "youtube-music-playlist-custom"
source: "youtube"
};
});
const playlist = {
title: info.title,
description: "",
thumbnail: info.thumbnail ? info.thumbnail.url : null,
thumbnail: info.thumbnail ? info.thumbnail.url : fallbackImage,
type: "playlist",
author: info.channel.name,
id: info.id,
url: info.url,
source: "youtube-music-playlist-custom",
source: "youtube",
rawPlaylist: info
};
return resolve({ playlist: playlist, info: tracks });
@ -215,7 +214,7 @@ module.exports = {
return {
title: track.title,
duration: track.durationInSec * 1000,
thumbnail: track.thumbnails ? track.thumbnails[0] ? track.thumbnails[0].url : null : null,
thumbnail: track.thumbnails[0].url || fallbackImage,
async engine() {
return (await playdl.stream(track.url, { discordPlayerCompatibility: true })).stream;
},
@ -224,7 +223,7 @@ module.exports = {
description: "",
url: track.url,
raw: info,
source: "youtube-custom"
source: "youtube"
};
});
const playlist = {
@ -232,7 +231,7 @@ module.exports = {
description: "",
thumbnail: info.thumbnail ? info.thumbnail.url : null,
type: "playlist",
source: "youtube-custom",
source: "youtube",
author: info.channel.name,
id: info.id,
url: info.url,
@ -242,27 +241,27 @@ module.exports = {
}
// search on youtube
const search = await playdl.search(query, { limit: 1 });
const search = await playdl.search(query, { limit: 10 });
if (search) {
const found = search[0];
const track = {
title: found.title,
duration: found.durationInSec * 1000,
thumbnail: found.thumbnails[0].url,
const found = search.map(track => {
return {
title: track.title,
duration: track.durationInSec * 1000,
thumbnail: track.thumbnails[0].url || fallbackImage,
async engine() {
return (await playdl.stream(found.url, { discordPlayerCompatibility: true })).stream;
return (await playdl.stream(track.url, { discordPlayerCompatibility: true })).stream;
},
views: found.views,
author: found.channel.name,
description: "",
url: found.url,
related_videos: found.related_videos,
raw: found,
source: "youtube-custom"
views: track.views,
author: track.channel.name,
description: "search",
url: track.url,
raw: track,
source: "youtube"
};
});
return resolve({ playlist: null, info: [track] });
return resolve({ playlist: null, info: found });
}
return resolve({ playlist: null, info: null });

View file

@ -42,6 +42,7 @@
"REP": "Очки репутации",
"XP": "Опыт",
"VICTORY": "Победа",
"VIEWS": "Просмотры",
"DEFEAT": "Поражение",
"MODERATOR": "Модератор",
"DURATION": "Длительность",

View file

@ -5,14 +5,14 @@
"QUERY": "Название / Прямая ссылка / Ссылка на YouTube или SoundCloud",
"NO_VOICE_CHANNEL": "Вы должны находиться в голосовом канале!",
"VOICE_CHANNEL_CONNECT": "Я не могу присоедениться к вашему голосовому каналу!",
"RESULTS_FOOTER": "Укажите число от 1 до 10 (без префикса).",
"NO_RESULT": "По запросу `{{query}}` ничего не найдено!",
"SEARCH_RESULTS": "Я нашёл данные видео на просторах Youtube'а! Выберите что-то одно с помощью кнопок ниже.",
"RESULTS_TITLE": "Результаты поиска по \"{{query}}\"",
"NO_RESULT": "Произошла неизвестная ошибка при поиске `{{query}}`! Скорее всего это видео имеет ограничение по возрасту.",
"NOW_PLAYING": "Сейчас играет **{{songName}}**",
"NOT_PLAYING": "На сервере сейчас ничего не воспроизводится",
"QUEUE_ENDED": "Очередь окончена",
"ADDED_QUEUE": "**{{songName}}** добавлен в очередь!",
"ADDED_QUEUE": "**{{songName}}** добавлен в очередь",
"STOP_DISCONNECTED": "Воспроизведение окончено, т.к. я вышел из голосового канала.",
"STOP_EMPTY": "Воспроизведение окончено, т.к. все вышли из голосового канала.",
"RESULTS_CANCEL": "Поиск отменён!",
"ERR_OCCURRED": "Произошла ошибка, пропускаю...\n`{{error}}`"
"ERR_OCCURRED": "Произошла ошибка...\n`{{error}}`"
}

View file

@ -5,6 +5,7 @@
"QUEUE": "Очередь",
"PAGE": "Страница **{{current}}**/**{{length}}**",
"NO_QUEUE": "Пусто",
"DURATION": "Длительность очереди",
"ADDED": "Добавил",
"NEXT_PAGE": "След. страница",
"PREV_PAGE": "Пред. страница",

3231
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{
"name": "jaba",
"version": "4.1.1",
"version": "4.1.2",
"description": "My Discord Bot",
"main": "index.js",
"private": true,
@ -24,7 +24,7 @@
"cron": "^1.7.2",
"discord-api-types": "^0.37.5",
"discord-giveaways": "^6.0.1",
"discord-player": "^5.3.1",
"discord-player": "github:JonnyBro/discord-player",
"discord.js": "^14.3.0",
"ejs": "^3.1.3",
"express": "^4.17.1",
@ -40,8 +40,7 @@
"mongoose": "^5.13.14",
"ms": "^2.1.3",
"play-dl": "^1.9.5",
"prism-media": "^1.3.4",
"spotify-url-info": "^3.1.2"
"prism-media": "^1.3.4"
},
"devDependencies": {
"eslint": "^7.5.0"