From 736567e110d446e55f0045c5aa681ce3e185c5c9 Mon Sep 17 00:00:00 2001 From: ysr9029 <56439343+Nich87@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:37:08 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=AA=A7=20added=20CustomEmbedBuilder?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Events/onMessageCreate.ts | 16 +++---- src/Utils/Builder.ts | 15 ++++++ src/Utils/songResolver.ts | 6 +-- src/commands/help.ts | 36 +++++++------- src/commands/loop.ts | 15 +++--- src/commands/pause.ts | 21 ++++---- src/commands/play.ts | 90 ++++++++++++++++------------------- src/commands/queue.ts | 13 ++--- src/commands/resume.ts | 23 +++++---- src/commands/skip.ts | 9 ++-- src/commands/stop.ts | 9 ++-- 11 files changed, 131 insertions(+), 122 deletions(-) create mode 100644 src/Utils/Builder.ts diff --git a/src/Events/onMessageCreate.ts b/src/Events/onMessageCreate.ts index 4c76efa..f7fc79d 100644 --- a/src/Events/onMessageCreate.ts +++ b/src/Events/onMessageCreate.ts @@ -1,5 +1,6 @@ +import { Builder } from '../Utils/Builder'; import { commands } from '../commands'; -import { Message, EmbedBuilder, Awaitable } from 'discord.js'; +import { Message, Awaitable } from 'discord.js'; const prefix = 'ts!'; @@ -55,13 +56,12 @@ export async function onMessageCreate(message: Message): Promise commands.nowplaying(message); break; default: - message.reply({ - embeds: [ - new EmbedBuilder() - .addFields({ name: 'Error', value: '不明なコマンドかコマンドが指定されていません。' }) - .setColor('Red') - ] - }); + message.reply( + new Builder() + .addFields({ name: 'Error', value: '不明なコマンドかコマンドが指定されていません。' }) + .setColor('Red') + .build() + ); break; } } diff --git a/src/Utils/Builder.ts b/src/Utils/Builder.ts new file mode 100644 index 0000000..caeb480 --- /dev/null +++ b/src/Utils/Builder.ts @@ -0,0 +1,15 @@ +import { EmbedBuilder } from 'discord.js'; + +export class Builder extends EmbedBuilder { + constructor() { + super(); + } + + public build() { + return { embeds: [this] }; + } + + public addEmbeds(embeds: Builder[]) { + return { embeds: [this, ...embeds] }; + } +} diff --git a/src/Utils/songResolver.ts b/src/Utils/songResolver.ts index 6c9cd25..1a5be4d 100644 --- a/src/Utils/songResolver.ts +++ b/src/Utils/songResolver.ts @@ -1,5 +1,5 @@ import { format_count, seconds_to_time } from '../Utils/NumberUtil'; -import { EmbedBuilder } from 'discord.js'; +import { Builder } from './Builder'; import ytdl from 'ytdl-core'; export function songResolver(info: ytdl.videoInfo, requestedBy?: string, requestedByAvatar?: string) { @@ -20,7 +20,7 @@ export async function getSongInfo(url: string) { const info = await ytdl.getInfo(url); const song = songResolver(info); - const embed = new EmbedBuilder() + const embed = new Builder() .setTitle(song.title) .setURL(song.url) .setThumbnail(song.thumbnail) @@ -31,5 +31,5 @@ export async function getSongInfo(url: string) { ) .setColor('Green'); - return { embeds: [embed] }; + return embed.build(); } diff --git a/src/commands/help.ts b/src/commands/help.ts index 76ef093..383c58c 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -1,21 +1,21 @@ -import { Message, EmbedBuilder } from 'discord.js'; +import { Builder } from '../Utils/Builder'; +import { Message } from 'discord.js'; export async function helpCommand(message: Message): Promise { - message.reply({ - embeds: [ - new EmbedBuilder() - .setTitle('Help') - .setColor('Blue') - .addFields( - { name: 'play', value: '音楽を再生します。' }, - { name: 'queue', value: 'キューを表示します。' }, - { name: 'loop', value: 'ループをオン/オフにします。' }, - { name: 'skip', value: '現在の曲をスキップします。' }, - { name: 'stop', value: '再生を停止します。' }, - { name: 'pause', value: '再生を一時停止します。' }, - { name: 'resume', value: '再生を再開します。' }, - { name: 'help', value: 'このメッセージを表示します。' } - ) - ] - }); + message.reply( + new Builder() + .setTitle('Help') + .setColor('Blue') + .addFields( + { name: 'play', value: '音楽を再生します。' }, + { name: 'queue', value: 'キューを表示します。' }, + { name: 'loop', value: 'ループをオン/オフにします。' }, + { name: 'skip', value: '現在の曲をスキップします。' }, + { name: 'stop', value: '再生を停止します。' }, + { name: 'pause', value: '再生を一時停止します。' }, + { name: 'resume', value: '再生を再開します。' }, + { name: 'help', value: 'このメッセージを表示します。' } + ) + .build() + ); } diff --git a/src/commands/loop.ts b/src/commands/loop.ts index 3d2419f..4dec818 100644 --- a/src/commands/loop.ts +++ b/src/commands/loop.ts @@ -1,6 +1,7 @@ +import { Builder } from '../Utils/Builder'; import { queueManager, Queue } from '../classes/queue'; import { player } from './play'; -import { Message, EmbedBuilder } from 'discord.js'; +import { Message } from 'discord.js'; export async function loopCommand(message: Message, args: string[]) { if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); @@ -20,16 +21,12 @@ export async function loopCommand(message: Message, args: string[]) { queue?.setLoop('track'); break; default: - message.reply({ - embeds: [ - new EmbedBuilder().addFields({ name: 'Error', value: 'コマンドが見つかりませんでした。' }).setColor('Red') - ] - }); + message.reply( + new Builder().addFields({ name: 'Error', value: 'コマンドが見つかりませんでした。' }).setColor('Red').build() + ); break; } } - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Looping', value: queue.loop! }).setColor('Green')] - }); + message.reply(new Builder().addFields({ name: 'Looping', value: queue.loop! }).setColor('Green').build()); } diff --git a/src/commands/pause.ts b/src/commands/pause.ts index 839f077..1d54057 100644 --- a/src/commands/pause.ts +++ b/src/commands/pause.ts @@ -1,23 +1,24 @@ +import { Builder } from '../Utils/Builder'; import { player } from './play'; import { AudioPlayerStatus } from '@discordjs/voice'; -import { Message, EmbedBuilder } from 'discord.js'; +import { Message } from 'discord.js'; export async function pauseCommand(message: Message) { if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); if (player.player.state.status === AudioPlayerStatus.Playing) { player.pause(); - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '動画を一時停止しました。' }).setColor('Green')] - }); + message.reply( + new Builder().addFields({ name: 'Success', value: '動画を一時停止しました。' }).setColor('Green').build() + ); } else if (player.player.state.status === AudioPlayerStatus.Paused) { player.resume(); - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green')] - }); + message.reply( + new Builder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green').build() + ); } else { - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow')] - }); + message.reply( + new Builder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow').build() + ); } } diff --git a/src/commands/play.ts b/src/commands/play.ts index 89e8b4e..305fb89 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,6 +1,7 @@ +import { Builder } from '../Utils/Builder'; import { YTPlayer } from '../classes/player'; import { Queue, queueManager } from '../classes/queue'; -import { Message, EmbedBuilder, ChannelType, VoiceBasedChannel } from 'discord.js'; +import { Message, ChannelType, VoiceBasedChannel } from 'discord.js'; import ytdl from 'ytdl-core'; export let player: YTPlayer | undefined; @@ -22,69 +23,62 @@ export async function playCommand(message: Message) { url = message.content.split(' ')[1]; const channel = message.member?.voice.channel; if (!url) - return message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Error', value: 'URLを指定してください' }).setColor('Red')] - }); + return message.reply( + new Builder().addFields({ name: 'Error', value: 'URLを指定してください' }).setColor('Red').build() + ); if (!ytdl.validateURL(url)) - return message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Error', value: '無効なURLです。' }).setColor('Red')] - }); + return message.reply(new Builder().addFields({ name: 'Error', value: '無効なURLです。' }).setColor('Red').build()); if (!channel) - return message.reply({ - embeds: [ - new EmbedBuilder() - .addFields({ name: 'Error', value: 'ボイスチャンネルに参加してから実行してください。' }) - .setColor('Red') - ] - }); + return message.reply( + new Builder() + .addFields({ name: 'Error', value: 'ボイスチャンネルに参加してから実行してください。' }) + .setColor('Red') + .build() + ); if (channel.type !== ChannelType.GuildVoice) return; if (!channel.joinable) - return message.reply({ - embeds: [ - new EmbedBuilder().addFields({ name: 'Error', value: 'このチャンネルに参加できません。' }).setColor('Red') - ] - }); + return message.reply( + new Builder().addFields({ name: 'Error', value: 'このチャンネルに参加できません。' }).setColor('Red').build() + ); if (!channel.speakable) - return message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Error', value: 'このチャンネルで喋れません。' }).setColor('Red')] - }); + return message.reply( + new Builder().addFields({ name: 'Error', value: 'このチャンネルで喋れません。' }).setColor('Red').build() + ); if (!queue.length || !player.isPlaying) { queue.addSong(url); const info = await ytdl.getInfo(url); - message.reply({ - embeds: [ - new EmbedBuilder() - .setTitle('Success') - .setDescription(`**[${info.videoDetails.title}](${info.videoDetails.video_url})を再生します。**`) - .addFields({ - name: info.videoDetails.title, - value: `投稿者: [${info.videoDetails.author.name}](${info.videoDetails.author.channel_url})` - }) - .setImage(info.videoDetails.thumbnails[0].url.split('?')[0]) - .setColor('Green') - ] - }); + message.reply( + new Builder() + .setTitle('Success') + .setDescription(`**[${info.videoDetails.title}](${info.videoDetails.video_url})を再生します。**`) + .addFields({ + name: info.videoDetails.title, + value: `投稿者: [${info.videoDetails.author.name}](${info.videoDetails.author.channel_url})` + }) + .setImage(info.videoDetails.thumbnails[0].url.split('?')[0]) + .setColor('Green') + .build() + ); if (queue.length === 1) return player.play(); } else { queue.addSong(url); const info = await ytdl.getInfo(url); - message.reply({ - embeds: [ - new EmbedBuilder() - .setTitle('Info') - .setDescription(`**[${info.videoDetails.title}](${info.videoDetails.video_url})をキューに追加しました。**`) - .addFields({ - name: info.videoDetails.title, - value: `投稿者: [${info.videoDetails.author.name}](${info.videoDetails.author.channel_url})` - }) - .setImage(info.videoDetails.thumbnails[0].url.split('?')[0]) - .setColor('Yellow') - ] - }); + message.reply( + new Builder() + .setTitle('Info') + .setDescription(`**[${info.videoDetails.title}](${info.videoDetails.video_url})をキューに追加しました。**`) + .addFields({ + name: info.videoDetails.title, + value: `投稿者: [${info.videoDetails.author.name}](${info.videoDetails.author.channel_url})` + }) + .setImage(info.videoDetails.thumbnails[0].url.split('?')[0]) + .setColor('Yellow') + .build() + ); } } diff --git a/src/commands/queue.ts b/src/commands/queue.ts index b8f94dd..fed15ab 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -1,18 +1,19 @@ +import { Builder } from '../Utils/Builder'; import { songResolver } from '../Utils/songResolver'; import { Queue, queueManager } from '../classes/queue'; import { player } from './play'; -import { Message, EmbedBuilder } from 'discord.js'; +import { Message } from 'discord.js'; import ytdl from 'ytdl-core'; export async function queueCommand(message: Message) { if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); const queue = queueManager.getQueue(message.guildId!) as Queue; if (!queue.length) { - return message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow')] - }); + return message.reply( + new Builder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow').build() + ); } - const embed = new EmbedBuilder().setTitle('Queue').setColor('Blue').setTimestamp(); + const embed = new Builder().setTitle('Queue').setColor('Blue').setTimestamp(); for (let i = 0; i < queue.length; i++) { const url = queue.store[i]; const info = await ytdl.getInfo(url); @@ -25,5 +26,5 @@ export async function queueCommand(message: Message) { text: `Queue: ${queue.store.length} songs` }); } - message.reply({ embeds: [embed] }); + message.reply(embed.build()); } diff --git a/src/commands/resume.ts b/src/commands/resume.ts index cad5bd7..0315715 100644 --- a/src/commands/resume.ts +++ b/src/commands/resume.ts @@ -1,23 +1,22 @@ +import { Builder } from '../Utils/Builder'; import { player } from './play'; import { AudioPlayerStatus } from '@discordjs/voice'; -import { Message, EmbedBuilder } from 'discord.js'; +import { Message } from 'discord.js'; export async function resumeCommand(message: Message) { if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); if (player.player.state.status === AudioPlayerStatus.Paused) { player.resume(); - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green')] - }); + message.reply( + new Builder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green').build() + ); } else if (player.player.state.status === AudioPlayerStatus.Playing) { - message.reply({ - embeds: [ - new EmbedBuilder().addFields({ name: 'Info', value: '動画が一時停止されていません。' }).setColor('Yellow') - ] - }); + message.reply( + new Builder().addFields({ name: 'Info', value: '動画が一時停止されていません。' }).setColor('Yellow').build() + ); } else { - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow')] - }); + message.reply( + new Builder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow').build() + ); } } diff --git a/src/commands/skip.ts b/src/commands/skip.ts index dd03afb..3b7ce6c 100644 --- a/src/commands/skip.ts +++ b/src/commands/skip.ts @@ -1,15 +1,16 @@ +import { Builder } from '../Utils/Builder'; import { Queue, queueManager } from '../classes/queue'; import { player } from './play'; -import { Message, EmbedBuilder } from 'discord.js'; +import { Message } from 'discord.js'; export async function skipCommand(message: Message) { if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); const queue = queueManager.queues.get(message.guildId!) as Queue; if (!queue.length) { - return message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow')] - }); + return message.reply( + new Builder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow').build() + ); } player.skip(); } diff --git a/src/commands/stop.ts b/src/commands/stop.ts index 98d7021..fd792fa 100644 --- a/src/commands/stop.ts +++ b/src/commands/stop.ts @@ -1,12 +1,13 @@ +import { Builder } from '../Utils/Builder'; import { queueManager } from '../classes/queue'; -import { Message, EmbedBuilder } from 'discord.js'; +import { Message } from 'discord.js'; export async function stopCommand(message: Message): Promise { if (message.guild?.members.me?.voice.channel) { message.guild.members.me.voice.disconnect(); - message.reply({ - embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '再生を停止しました。' }).setColor('Green')] - }); + message.reply( + new Builder().addFields({ name: 'Success', value: '再生を停止しました。' }).setColor('Green').build() + ); } queueManager.queues.delete(message.guildId!); From 2c21bd8dfa5807e02da653da9b20421bdf74310f Mon Sep 17 00:00:00 2001 From: ysr9029 <56439343+Nich87@users.noreply.github.com> Date: Sun, 3 Dec 2023 20:21:05 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=AA=A7=20added=20constant=20Embeds?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Events/onMessageCreate.ts | 9 ++---- src/commands/debug.ts | 5 +-- src/commands/help.ts | 19 ++--------- src/commands/loop.ts | 10 +++--- src/commands/nowplaying.ts | 5 +-- src/commands/pause.ts | 16 +++------- src/commands/play.ts | 36 +++++---------------- src/commands/queue.ts | 13 +++----- src/commands/resume.ts | 16 +++------- src/commands/skip.ts | 11 ++----- src/commands/stop.ts | 6 ++-- src/embeds/index.ts | 59 +++++++++++++++++++++++++++++++++++ 12 files changed, 101 insertions(+), 104 deletions(-) create mode 100644 src/embeds/index.ts diff --git a/src/Events/onMessageCreate.ts b/src/Events/onMessageCreate.ts index f7fc79d..1d904c8 100644 --- a/src/Events/onMessageCreate.ts +++ b/src/Events/onMessageCreate.ts @@ -1,5 +1,5 @@ -import { Builder } from '../Utils/Builder'; import { commands } from '../commands'; +import { embeds } from '../embeds'; import { Message, Awaitable } from 'discord.js'; const prefix = 'ts!'; @@ -56,12 +56,7 @@ export async function onMessageCreate(message: Message): Promise commands.nowplaying(message); break; default: - message.reply( - new Builder() - .addFields({ name: 'Error', value: '不明なコマンドかコマンドが指定されていません。' }) - .setColor('Red') - .build() - ); + message.reply(embeds.unknownCommand); break; } } diff --git a/src/commands/debug.ts b/src/commands/debug.ts index c7e6c14..6965d37 100644 --- a/src/commands/debug.ts +++ b/src/commands/debug.ts @@ -1,14 +1,15 @@ import { songResolver } from '../Utils/songResolver'; +import { embeds } from '../embeds'; import { Message } from 'discord.js'; import ytdl from 'ytdl-core'; export async function debugCommand(message: Message) { const url = message.content.split(' ')[1]; if (!url) { - return message.reply({ content: 'URLを指定してください。' }); + return message.reply(embeds.noUrl); } if (!ytdl.validateURL(url)) { - return message.reply({ content: '無効なURLです。' }); + return message.reply(embeds.invaildUrl); } const info = await ytdl.getInfo(url); console.table(songResolver(info)); diff --git a/src/commands/help.ts b/src/commands/help.ts index 383c58c..f911b48 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -1,21 +1,6 @@ -import { Builder } from '../Utils/Builder'; +import { embeds } from '../embeds'; import { Message } from 'discord.js'; export async function helpCommand(message: Message): Promise { - message.reply( - new Builder() - .setTitle('Help') - .setColor('Blue') - .addFields( - { name: 'play', value: '音楽を再生します。' }, - { name: 'queue', value: 'キューを表示します。' }, - { name: 'loop', value: 'ループをオン/オフにします。' }, - { name: 'skip', value: '現在の曲をスキップします。' }, - { name: 'stop', value: '再生を停止します。' }, - { name: 'pause', value: '再生を一時停止します。' }, - { name: 'resume', value: '再生を再開します。' }, - { name: 'help', value: 'このメッセージを表示します。' } - ) - .build() - ); + message.reply(embeds.help); } diff --git a/src/commands/loop.ts b/src/commands/loop.ts index 4dec818..67de376 100644 --- a/src/commands/loop.ts +++ b/src/commands/loop.ts @@ -1,10 +1,10 @@ -import { Builder } from '../Utils/Builder'; import { queueManager, Queue } from '../classes/queue'; +import { embeds } from '../embeds'; import { player } from './play'; import { Message } from 'discord.js'; export async function loopCommand(message: Message, args: string[]) { - if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); + if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying); const queue = queueManager.queues.get(message.guildId!) as Queue; args = args.filter((arg) => arg !== ''); if (args.length === 0) { @@ -21,12 +21,10 @@ export async function loopCommand(message: Message, args: string[]) { queue?.setLoop('track'); break; default: - message.reply( - new Builder().addFields({ name: 'Error', value: 'コマンドが見つかりませんでした。' }).setColor('Red').build() - ); + message.reply(embeds.commandNotFound); break; } } - message.reply(new Builder().addFields({ name: 'Looping', value: queue.loop! }).setColor('Green').build()); + message.reply(embeds.embed.addFields({ name: 'Looping', value: queue.loop! }).setColor('Green').build()); } diff --git a/src/commands/nowplaying.ts b/src/commands/nowplaying.ts index c2e72fa..f8c735c 100644 --- a/src/commands/nowplaying.ts +++ b/src/commands/nowplaying.ts @@ -1,12 +1,13 @@ import { getSongInfo } from '../Utils/songResolver'; import { queueManager, Queue } from '../classes/queue'; +import { embeds } from '../embeds'; import { player } from './play'; import { Message } from 'discord.js'; export async function nowplayingCommand(message: Message) { - if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); + if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying); const queue = queueManager.getQueue(message.guild?.id as string) as Queue; - if (!queue.currentSong) return message.reply({ content: 'キューに曲がありません。' }); + if (!queue.currentSong) return message.reply(embeds.queueEmpty); const info = await getSongInfo(queue.currentSong); return message.reply(info); } diff --git a/src/commands/pause.ts b/src/commands/pause.ts index 1d54057..8f793f6 100644 --- a/src/commands/pause.ts +++ b/src/commands/pause.ts @@ -1,24 +1,18 @@ -import { Builder } from '../Utils/Builder'; +import { embeds } from '../embeds'; import { player } from './play'; import { AudioPlayerStatus } from '@discordjs/voice'; import { Message } from 'discord.js'; export async function pauseCommand(message: Message) { - if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); + if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying); if (player.player.state.status === AudioPlayerStatus.Playing) { player.pause(); - message.reply( - new Builder().addFields({ name: 'Success', value: '動画を一時停止しました。' }).setColor('Green').build() - ); + message.reply(embeds.videoPaused); } else if (player.player.state.status === AudioPlayerStatus.Paused) { player.resume(); - message.reply( - new Builder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green').build() - ); + message.reply(embeds.videoResumed); } else { - message.reply( - new Builder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow').build() - ); + message.reply(embeds.videoNotPlaying); } } diff --git a/src/commands/play.ts b/src/commands/play.ts index 305fb89..c9c3fbc 100644 --- a/src/commands/play.ts +++ b/src/commands/play.ts @@ -1,6 +1,6 @@ -import { Builder } from '../Utils/Builder'; import { YTPlayer } from '../classes/player'; import { Queue, queueManager } from '../classes/queue'; +import { embeds } from '../embeds'; import { Message, ChannelType, VoiceBasedChannel } from 'discord.js'; import ytdl from 'ytdl-core'; @@ -22,38 +22,18 @@ export async function playCommand(message: Message) { } url = message.content.split(' ')[1]; const channel = message.member?.voice.channel; - if (!url) - return message.reply( - new Builder().addFields({ name: 'Error', value: 'URLを指定してください' }).setColor('Red').build() - ); - - if (!ytdl.validateURL(url)) - return message.reply(new Builder().addFields({ name: 'Error', value: '無効なURLです。' }).setColor('Red').build()); - - if (!channel) - return message.reply( - new Builder() - .addFields({ name: 'Error', value: 'ボイスチャンネルに参加してから実行してください。' }) - .setColor('Red') - .build() - ); - + if (!url) return message.reply(embeds.noUrl); + if (!ytdl.validateURL(url)) return message.reply(embeds.invaildUrl); + if (!channel) return message.reply(embeds.voiceChannelJoin); if (channel.type !== ChannelType.GuildVoice) return; - if (!channel.joinable) - return message.reply( - new Builder().addFields({ name: 'Error', value: 'このチャンネルに参加できません。' }).setColor('Red').build() - ); - - if (!channel.speakable) - return message.reply( - new Builder().addFields({ name: 'Error', value: 'このチャンネルで喋れません。' }).setColor('Red').build() - ); + if (!channel.joinable) return message.reply(embeds.voiceChannnelJoined); + if (!channel.speakable) return message.reply(embeds.voiceChannnelPermission); if (!queue.length || !player.isPlaying) { queue.addSong(url); const info = await ytdl.getInfo(url); message.reply( - new Builder() + embeds.embed .setTitle('Success') .setDescription(`**[${info.videoDetails.title}](${info.videoDetails.video_url})を再生します。**`) .addFields({ @@ -69,7 +49,7 @@ export async function playCommand(message: Message) { queue.addSong(url); const info = await ytdl.getInfo(url); message.reply( - new Builder() + embeds.embed .setTitle('Info') .setDescription(`**[${info.videoDetails.title}](${info.videoDetails.video_url})をキューに追加しました。**`) .addFields({ diff --git a/src/commands/queue.ts b/src/commands/queue.ts index fed15ab..c2e314e 100644 --- a/src/commands/queue.ts +++ b/src/commands/queue.ts @@ -1,19 +1,16 @@ -import { Builder } from '../Utils/Builder'; import { songResolver } from '../Utils/songResolver'; import { Queue, queueManager } from '../classes/queue'; +import { embeds } from '../embeds'; import { player } from './play'; import { Message } from 'discord.js'; import ytdl from 'ytdl-core'; export async function queueCommand(message: Message) { - if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); + if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying); const queue = queueManager.getQueue(message.guildId!) as Queue; - if (!queue.length) { - return message.reply( - new Builder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow').build() - ); - } - const embed = new Builder().setTitle('Queue').setColor('Blue').setTimestamp(); + if (!queue.length) return message.reply(embeds.queueEmpty); + + const embed = embeds.embed.setTitle('Queue').setColor('Blue').setTimestamp(); for (let i = 0; i < queue.length; i++) { const url = queue.store[i]; const info = await ytdl.getInfo(url); diff --git a/src/commands/resume.ts b/src/commands/resume.ts index 0315715..67d2ec1 100644 --- a/src/commands/resume.ts +++ b/src/commands/resume.ts @@ -1,22 +1,16 @@ -import { Builder } from '../Utils/Builder'; +import { embeds } from '../embeds'; import { player } from './play'; import { AudioPlayerStatus } from '@discordjs/voice'; import { Message } from 'discord.js'; export async function resumeCommand(message: Message) { - if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); + if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying); if (player.player.state.status === AudioPlayerStatus.Paused) { player.resume(); - message.reply( - new Builder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green').build() - ); + message.reply(embeds.videoResumed); } else if (player.player.state.status === AudioPlayerStatus.Playing) { - message.reply( - new Builder().addFields({ name: 'Info', value: '動画が一時停止されていません。' }).setColor('Yellow').build() - ); + message.reply(embeds.videoNotPaused); } else { - message.reply( - new Builder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow').build() - ); + message.reply(embeds.videoNotPlaying); } } diff --git a/src/commands/skip.ts b/src/commands/skip.ts index 3b7ce6c..c671ca4 100644 --- a/src/commands/skip.ts +++ b/src/commands/skip.ts @@ -1,16 +1,11 @@ -import { Builder } from '../Utils/Builder'; import { Queue, queueManager } from '../classes/queue'; +import { embeds } from '../embeds'; import { player } from './play'; import { Message } from 'discord.js'; export async function skipCommand(message: Message) { - if (typeof player === 'undefined') return message.reply({ content: '動画が再生されていません。' }); + if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying); const queue = queueManager.queues.get(message.guildId!) as Queue; - - if (!queue.length) { - return message.reply( - new Builder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow').build() - ); - } + if (!queue.length) return message.reply(embeds.queueEmpty); player.skip(); } diff --git a/src/commands/stop.ts b/src/commands/stop.ts index fd792fa..3cb76f2 100644 --- a/src/commands/stop.ts +++ b/src/commands/stop.ts @@ -1,13 +1,11 @@ -import { Builder } from '../Utils/Builder'; import { queueManager } from '../classes/queue'; +import { embeds } from '../embeds'; import { Message } from 'discord.js'; export async function stopCommand(message: Message): Promise { if (message.guild?.members.me?.voice.channel) { message.guild.members.me.voice.disconnect(); - message.reply( - new Builder().addFields({ name: 'Success', value: '再生を停止しました。' }).setColor('Green').build() - ); + message.reply(embeds.videoStopped); } queueManager.queues.delete(message.guildId!); diff --git a/src/embeds/index.ts b/src/embeds/index.ts new file mode 100644 index 0000000..2dd2776 --- /dev/null +++ b/src/embeds/index.ts @@ -0,0 +1,59 @@ +import { Builder } from '../Utils/Builder'; + +export const embeds = { + embed: new Builder(), + help: new Builder() + .setTitle('Help') + .setColor('Blue') + .addFields( + { name: 'play', value: '音楽を再生します。' }, + { name: 'queue', value: 'キューを表示します。' }, + { name: 'loop', value: 'ループをオン/オフにします。' }, + { name: 'skip', value: '現在の曲をスキップします。' }, + { name: 'stop', value: '再生を停止します。' }, + { name: 'pause', value: '再生を一時停止します。' }, + { name: 'resume', value: '再生を再開します。' }, + { name: 'help', value: 'このメッセージを表示します。' } + ) + .build(), + videoNotPlaying: new Builder() + .addFields({ name: 'Info', value: '動画が再生されていません。' }) + .setColor('Yellow') + .build(), + videoResumed: new Builder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green').build(), + videoPaused: new Builder() + .addFields({ name: 'Success', value: '動画を一時停止しました。' }) + .setColor('Green') + .build(), + videoNotPaused: new Builder() + .addFields({ name: 'Info', value: '動画は一時停止されていません。' }) + .setColor('Yellow') + .build(), + videoStopped: new Builder().addFields({ name: 'Success', value: '動画を停止しました。' }).setColor('Green').build(), + noUrl: new Builder().addFields({ name: 'Error', value: 'URLを指定してください。' }).setColor('Red').build(), + invaildUrl: new Builder() + .addFields({ name: 'Error', value: '有効なURLを指定してください。' }) + .setColor('Red') + .build(), + voiceChannnelPermission: new Builder() + .addFields({ name: 'Error', value: 'ボイスチャンネルに参加する権限がありません。' }) + .setColor('Red') + .build(), + voiceChannnelJoined: new Builder() + .addFields({ name: 'Error', value: 'ボイスチャンネルに既に参加しています。' }) + .setColor('Red') + .build(), + queueEmpty: new Builder().addFields({ name: 'Error', value: 'キューが空です。' }).setColor('Red').build(), + unknownCommand: new Builder() + .addFields({ name: 'Error', value: '不明なコマンドかコマンドが指定されていません。' }) + .setColor('Red') + .build(), + voiceChannelJoin: new Builder() + .addFields({ name: 'Error', value: 'ボイスチャンネルに参加してから実行してください。' }) + .setColor('Red') + .build(), + commandNotFound: new Builder() + .addFields({ name: 'Error', value: 'コマンドが見つかりませんでした。' }) + .setColor('Red') + .build() +};