Skip to content

Commit

Permalink
playコマンドのサブコマンドを削除し、プレイリストのサブコマンドにplayコマンドを追加
Browse files Browse the repository at this point in the history
  • Loading branch information
kosugikun committed Oct 20, 2024
1 parent cd7d370 commit 7ffea85
Show file tree
Hide file tree
Showing 3 changed files with 368 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.jagrosh.jdautilities.command.CommandEvent;
import com.jagrosh.jdautilities.command.SlashCommandEvent;
import com.jagrosh.jmusicbot.Bot;
import com.jagrosh.jmusicbot.audio.AudioHandler;
import com.jagrosh.jmusicbot.audio.QueuedTrack;
import com.jagrosh.jmusicbot.utils.FormatUtil;
import dev.cosgy.jmusicbot.playlist.MylistLoader;
import dev.cosgy.jmusicbot.slashcommands.DJCommand;
import dev.cosgy.jmusicbot.slashcommands.MusicCommand;
Expand All @@ -28,6 +31,7 @@ public MylistCmd(Bot bot) {
this.help = "自分専用の再生リストを管理";
this.aliases = bot.getConfig().getAliases(this.name);
this.children = new MusicCommand[]{
new PlayCmd(bot),
new MakelistCmd(bot),
new DeletelistCmd(bot),
new AppendlistCmd(bot),
Expand All @@ -49,6 +53,85 @@ public void doCommand(CommandEvent event) {
public void doCommand(SlashCommandEvent slashCommandEvent) {
}


public class PlayCmd extends MusicCommand {
public PlayCmd(Bot bot) {
super(bot);
this.name = "play";
this.aliases = new String[]{"pl"};
this.arguments = "<name>";
this.help = "マイリストを再生します";
this.beListening = true;
this.bePlaying = false;

List<OptionData> options = new ArrayList<>();
options.add(new OptionData(OptionType.STRING, "name", "マイリスト名", true));
this.options = options;
}

@Override
public void doCommand(CommandEvent event) {
String userId = event.getAuthor().getId();
if (event.getArgs().isEmpty()) {
event.reply(event.getClient().getError() + " マイリスト名を含めてください。");
return;
}
MylistLoader.Playlist playlist = bot.getMylistLoader().getPlaylist(userId, event.getArgs());
if (playlist == null) {
event.replyError("`" + event.getArgs() + ".txt `を見つけられませんでした ");
return;
}
event.getChannel().sendMessage(":calling: マイリスト**" + event.getArgs() + "**を読み込んでいます... (" + playlist.getItems().size() + " 曲)").queue(m ->
{
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
playlist.loadTracks(bot.getPlayerManager(), (at) -> handler.addTrack(new QueuedTrack(at, event.getAuthor())), () -> {
StringBuilder builder = new StringBuilder(playlist.getTracks().isEmpty()
? event.getClient().getWarning() + " 楽曲がロードされていません。"
: event.getClient().getSuccess() + "**" + playlist.getTracks().size() + "**曲、読み込みました。");
if (!playlist.getErrors().isEmpty())
builder.append("\n以下の楽曲をロードできませんでした:");
playlist.getErrors().forEach(err -> builder.append("\n`[").append(err.getIndex() + 1)
.append("]` **").append(err.getItem()).append("**: ").append(err.getReason()));
String str = builder.toString();
if (str.length() > 2000)
str = str.substring(0, 1994) + " (以下略)";
m.editMessage(FormatUtil.filter(str)).queue();
});
});
}

@Override
public void doCommand(SlashCommandEvent event) {
String userId = event.getUser().getId();

String name = event.getOption("name").getAsString();

MylistLoader.Playlist playlist = bot.getMylistLoader().getPlaylist(userId, name);
if (playlist == null) {
event.reply(event.getClient().getError() + "`" + name + ".txt `を見つけられませんでした ").queue();
return;
}
event.reply(":calling: マイリスト**" + name + "**を読み込んでいます... (" + playlist.getItems().size() + " 曲)").queue(m ->
{
AudioHandler handler = (AudioHandler) event.getGuild().getAudioManager().getSendingHandler();
playlist.loadTracks(bot.getPlayerManager(), (at) -> handler.addTrack(new QueuedTrack(at, event.getUser())), () -> {
StringBuilder builder = new StringBuilder(playlist.getTracks().isEmpty()
? event.getClient().getWarning() + " 楽曲がロードされていません。"
: event.getClient().getSuccess() + "**" + playlist.getTracks().size() + "**曲、読み込みました。");
if (!playlist.getErrors().isEmpty())
builder.append("\n以下の楽曲をロードできませんでした:");
playlist.getErrors().forEach(err -> builder.append("\n`[").append(err.getIndex() + 1)
.append("]` **").append(err.getItem()).append("**: ").append(err.getReason()));
String str = builder.toString();
if (str.length() > 2000)
str = str.substring(0, 1994) + " (以下略)";
m.editOriginal(FormatUtil.filter(str)).queue();
});
});
}
}


public static class MakelistCmd extends DJCommand {
public MakelistCmd(Bot bot) {
super(bot);
Expand Down
Loading

0 comments on commit 7ffea85

Please sign in to comment.