Skip to content

Commit

Permalink
Merge pull request #5 from Nich87/main
Browse files Browse the repository at this point in the history
おめでとうございます!あなたはこれマージすべきなのか...?と思うPRを受け取りました!
  • Loading branch information
MotiCAT authored Dec 3, 2023
2 parents d7955f8 + 2c21bd8 commit 0ff1dda
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 145 deletions.
11 changes: 3 additions & 8 deletions src/Events/onMessageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { commands } from '../commands';
import { Message, EmbedBuilder, Awaitable } from 'discord.js';
import { embeds } from '../embeds';
import { Message, Awaitable } from 'discord.js';

const prefix = 'ts!';

Expand Down Expand Up @@ -55,13 +56,7 @@ export async function onMessageCreate(message: Message): Promise<Awaitable<void>
commands.nowplaying(message);
break;
default:
message.reply({
embeds: [
new EmbedBuilder()
.addFields({ name: 'Error', value: '不明なコマンドかコマンドが指定されていません。' })
.setColor('Red')
]
});
message.reply(embeds.unknownCommand);
break;
}
}
15 changes: 15 additions & 0 deletions src/Utils/Builder.ts
Original file line number Diff line number Diff line change
@@ -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] };
}
}
6 changes: 3 additions & 3 deletions src/Utils/songResolver.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
Expand All @@ -31,5 +31,5 @@ export async function getSongInfo(url: string) {
)
.setColor('Green');

return { embeds: [embed] };
return embed.build();
}
5 changes: 3 additions & 2 deletions src/commands/debug.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
21 changes: 3 additions & 18 deletions src/commands/help.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { Message, EmbedBuilder } from 'discord.js';
import { embeds } from '../embeds';
import { Message } from 'discord.js';

export async function helpCommand(message: Message): Promise<void> {
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(embeds.help);
}
15 changes: 5 additions & 10 deletions src/commands/loop.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { queueManager, Queue } from '../classes/queue';
import { embeds } from '../embeds';
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: '動画が再生されていません。' });
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) {
Expand All @@ -20,16 +21,10 @@ 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(embeds.commandNotFound);
break;
}
}

message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Looping', value: queue.loop! }).setColor('Green')]
});
message.reply(embeds.embed.addFields({ name: 'Looping', value: queue.loop! }).setColor('Green').build());
}
5 changes: 3 additions & 2 deletions src/commands/nowplaying.ts
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 6 additions & 11 deletions src/commands/pause.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import { embeds } from '../embeds';
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 (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying);

if (player.player.state.status === AudioPlayerStatus.Playing) {
player.pause();
message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '動画を一時停止しました。' }).setColor('Green')]
});
message.reply(embeds.videoPaused);
} else if (player.player.state.status === AudioPlayerStatus.Paused) {
player.resume();
message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green')]
});
message.reply(embeds.videoResumed);
} else {
message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow')]
});
message.reply(embeds.videoNotPlaying);
}
}
88 changes: 31 additions & 57 deletions src/commands/play.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { YTPlayer } from '../classes/player';
import { Queue, queueManager } from '../classes/queue';
import { Message, EmbedBuilder, ChannelType, VoiceBasedChannel } from 'discord.js';
import { embeds } from '../embeds';
import { Message, ChannelType, VoiceBasedChannel } from 'discord.js';
import ytdl from 'ytdl-core';

export let player: YTPlayer | undefined;
Expand All @@ -21,70 +22,43 @@ 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')]
});

if (!ytdl.validateURL(url))
return message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Error', value: '無効なURLです。' }).setColor('Red')]
});

if (!channel)
return message.reply({
embeds: [
new EmbedBuilder()
.addFields({ name: 'Error', value: 'ボイスチャンネルに参加してから実行してください。' })
.setColor('Red')
]
});

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({
embeds: [
new EmbedBuilder().addFields({ name: 'Error', value: 'このチャンネルに参加できません。' }).setColor('Red')
]
});

if (!channel.speakable)
return message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Error', value: 'このチャンネルで喋れません。' }).setColor('Red')]
});
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({
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(
embeds.embed
.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(
embeds.embed
.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()
);
}
}
16 changes: 7 additions & 9 deletions src/commands/queue.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { songResolver } from '../Utils/songResolver';
import { Queue, queueManager } from '../classes/queue';
import { embeds } from '../embeds';
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: '動画が再生されていません。' });
if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying);
const queue = queueManager.getQueue(message.guildId!) as Queue;
if (!queue.length) {
return message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow')]
});
}
const embed = new EmbedBuilder().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);
Expand All @@ -25,5 +23,5 @@ export async function queueCommand(message: Message) {
text: `Queue: ${queue.store.length} songs`
});
}
message.reply({ embeds: [embed] });
message.reply(embed.build());
}
19 changes: 6 additions & 13 deletions src/commands/resume.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import { embeds } from '../embeds';
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 (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying);
if (player.player.state.status === AudioPlayerStatus.Paused) {
player.resume();
message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Success', value: '動画を再開しました。' }).setColor('Green')]
});
message.reply(embeds.videoResumed);
} else if (player.player.state.status === AudioPlayerStatus.Playing) {
message.reply({
embeds: [
new EmbedBuilder().addFields({ name: 'Info', value: '動画が一時停止されていません。' }).setColor('Yellow')
]
});
message.reply(embeds.videoNotPaused);
} else {
message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Info', value: '動画が再生されていません。' }).setColor('Yellow')]
});
message.reply(embeds.videoNotPlaying);
}
}
12 changes: 4 additions & 8 deletions src/commands/skip.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Queue, queueManager } from '../classes/queue';
import { embeds } from '../embeds';
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: '動画が再生されていません。' });
if (typeof player === 'undefined') return message.reply(embeds.videoNotPlaying);
const queue = queueManager.queues.get(message.guildId!) as Queue;

if (!queue.length) {
return message.reply({
embeds: [new EmbedBuilder().addFields({ name: 'Info', value: 'キューが空です。' }).setColor('Yellow')]
});
}
if (!queue.length) return message.reply(embeds.queueEmpty);
player.skip();
}
7 changes: 3 additions & 4 deletions src/commands/stop.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { queueManager } from '../classes/queue';
import { Message, EmbedBuilder } from 'discord.js';
import { embeds } from '../embeds';
import { Message } from 'discord.js';

export async function stopCommand(message: Message): Promise<void> {
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(embeds.videoStopped);
}

queueManager.queues.delete(message.guildId!);
Expand Down
Loading

0 comments on commit 0ff1dda

Please sign in to comment.