Initial commit

This commit is contained in:
Mira 2025-01-27 18:36:22 +01:00
commit 79e7cffb4e
Signed by untrusted user who does not match committer: Xorog
GPG key ID: 983798ED9C3E7C36
49 changed files with 6399 additions and 0 deletions

View file

@ -0,0 +1,70 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
namespace ProjectMakoto.Plugins.Music;
internal sealed class AddToQueueCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForHeavy(ctx))
return;
var playlistId = (string)arguments["playlist"];
if (!MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Any(x => x.PlaylistId == playlistId))
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.NoPlaylist, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title)));
return;
}
var SelectedPlaylist = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.First(x => x.PlaylistId == playlistId);
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Play.Preparing, true),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title)));
try
{
await new JoinCommand().TransferCommand(ctx, null);
}
catch (CancelException)
{
this.DeleteOrInvalidate();
return;
}
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.AddToQueue.Adding, true,
new TVar("Name", SelectedPlaylist.PlaylistName),
new TVar("", SelectedPlaylist.List.Length)),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title)));
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.AddRange(SelectedPlaylist.List.Select(x => new GuildMusic.QueueInfo(x.Title, x.Url, x.Length.Value, ctx.Guild.Id, ctx.User.Id)));
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Play.QueuedMultiple, true,
new TVar("Count", SelectedPlaylist.List.Length),
new TVar("Playlist", SelectedPlaylist.PlaylistName))
}
.AddField(new DiscordEmbedField($"📜 {this.GetString(CommandKey.Play.QueuePositions)}", $"{(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length - SelectedPlaylist.List.Length + 1)} - {MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length}"))
.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title)));
});
}
}

View file

@ -0,0 +1,51 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
namespace ProjectMakoto.Plugins.Music;
internal sealed class DeleteCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var playlistId = (string)arguments["playlist"];
if (!MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Any(x => x.PlaylistId == playlistId))
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.NoPlaylist, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title)));
return;
}
var SelectedPlaylist = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.First(x => x.PlaylistId == playlistId);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Delete.Deleting, true, new TVar("Name", SelectedPlaylist.PlaylistName)),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title))));
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Remove(x => x.PlaylistId, SelectedPlaylist);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Delete.Deleted, true, new TVar("Name", SelectedPlaylist.PlaylistName)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
});
}
}

View file

@ -0,0 +1,45 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
namespace ProjectMakoto.Plugins.Music;
internal sealed class ExportCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var playlistId = (string)arguments["playlist"];
if (!MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Any(x => x.PlaylistId == playlistId))
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.NoPlaylist, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title)));
return;
}
var SelectedPlaylist = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.First(x => x.PlaylistId == playlistId);
using (MemoryStream stream = new(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(SelectedPlaylist, Formatting.Indented))))
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder()
{
Description = this.GetString(CommandKey.Playlists.Export.Exported, true, new TVar("Name", SelectedPlaylist.PlaylistName)),
}.AsInfo(ctx, this.GetString(CommandKey.Playlists.Title))).WithFile($"{Guid.NewGuid().ToString().Replace("-", "").ToLower()}.json", stream));
}
});
}
}

View file

@ -0,0 +1,224 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
using DisCatSharp.Lavalink.Entities;
using DisCatSharp.Lavalink.Enums;
using ProjectMakoto.Entities.Users;
namespace ProjectMakoto.Plugins.Music;
internal sealed class ImportCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
return;
}
var Link = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Import.Link), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("↘")));
var ExportedPlaylist = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Import.ExportedPlaylist), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("📂")));
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.ImportMethod, true),
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)))
.AddComponents(new List<DiscordComponent> { Link, ExportedPlaylist })
.AddComponents(MessageComponents.GetCancelButton(ctx.DbUser, ctx.Bot)));
var Menu = await ctx.WaitForButtonAsync();
if (Menu.TimedOut)
{
this.ModifyToTimedOut();
return;
}
if (Menu.GetCustomId() == Link.CustomId)
{
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.Import.ImportPlaylist), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "query", this.GetString(CommandKey.Playlists.Import.PlaylistUrl), "", 1, 100, true));
var ModalResult = await this.PromptModalWithRetry(Menu.Result.Interaction, modal, false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
return;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
var query = ModalResult.Result.Interaction.GetModalValueByCustomId("query");
var lava = ctx.Client.GetLavalink();
var node = lava.ConnectedSessions.Values.First(x => x.IsConnected);
if (Regex.IsMatch(query, "{jndi:(ldap[s]?|rmi):\\/\\/[^\n]+"))
throw new Exception();
var loadResult = await node.LoadTracksAsync(LavalinkSearchType.Plain, query);
if (loadResult.LoadType == LavalinkLoadResultType.Error)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.NotLoaded, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
return;
}
else if (loadResult.LoadType == LavalinkLoadResultType.Playlist)
{
var playlistResult = loadResult.GetResultAs<LavalinkPlaylist>();
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
return;
}
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.Creating, true),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title))));
var v = new UserPlaylist
{
PlaylistName = playlistResult.Info.Name,
List = playlistResult.Tracks.Select(x => new PlaylistEntry { Title = x.Info.Title, Url = x.Info.Uri.ToString(), Length = x.Info.Length }).Take(250).ToArray()
};
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Add(v);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.Created, true,
new TVar("Name", v.PlaylistName),
new TVar("Count", v.List.Length)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
}
else
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.NotLoaded, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
return;
}
}
else if (Menu.GetCustomId() == ExportedPlaylist.CustomId)
{
try
{
_ = Menu.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.UploadExport, true, new TVar("Command", $"{ctx.Prefix}upload")),
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title))));
Stream stream;
try
{
stream = (await this.PromptForFileUpload()).stream;
}
catch (AlreadyAppliedException)
{
return;
}
catch (ArgumentException)
{
this.ModifyToTimedOut();
return;
}
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.Importing, true),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title))));
var rawJson = new StreamReader(stream).ReadToEnd();
var ImportJson = JsonConvert.DeserializeObject<UserPlaylist>((rawJson is null or "null" or "" ? "[]" : rawJson), new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error });
ImportJson.List = ImportJson.List.Where(x => RegexTemplates.Url.IsMatch(x.Url)).Select(x => new PlaylistEntry { Title = x.Title, Url = x.Url, Length = x.Length }).Take(250).ToArray();
if (ImportJson.List.Length == 0)
throw new Exception();
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
return;
}
var v = new UserPlaylist
{
PlaylistName = ImportJson.PlaylistName,
List = ImportJson.List,
PlaylistColor = ImportJson.PlaylistColor
};
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Add(v);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.Created, true,
new TVar("Name", v.PlaylistName),
new TVar("Count", v.List.Length)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
}
catch (Exception ex)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Import.ImportFailed, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
MusicPlugin.Plugin!._logger.LogError(ex, "Failed to import a playlist");
return;
}
}
else if (Menu.GetCustomId() == MessageComponents.CancelButtonId)
{
_ = Menu.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
return;
}
});
}
}

View file

@ -0,0 +1,111 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
using ProjectMakoto.Entities.Users;
namespace ProjectMakoto.Plugins.Music;
internal sealed class LoadShareCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
var userid = ((DiscordUser)arguments["user"]).Id;
var id = ((string)arguments["id"])
.Replace("/", "")
.Replace("\\", "")
.Replace(">", "")
.Replace("<", "")
.Replace("|", "")
.Replace(":", "")
.Replace("&", "");
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var embed = new DiscordEmbedBuilder()
{
Description = this.GetString(CommandKey.Playlists.LoadShare.Loading, true)
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed);
if (!Directory.Exists("PlaylistShares"))
_ = Directory.CreateDirectory("PlaylistShares");
if (!Directory.Exists($"PlaylistShares/{userid}") || !File.Exists($"PlaylistShares/{userid}/{id}.json"))
{
embed.Description = this.GetString(CommandKey.Playlists.LoadShare.NotFound);
_ = embed.AsError(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed.Build());
return;
}
var user = await ctx.Client.GetUserAsync(userid);
var rawJson = File.ReadAllText($"PlaylistShares/{userid}/{id}.json");
var ImportJson = JsonConvert.DeserializeObject<UserPlaylist>((rawJson is null or "null" or "" ? "[]" : rawJson), new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error });
var pad = TranslationUtil.CalculatePadding(ctx.DbUser, CommandKey.Playlists.LoadShare.PlaylistName, CommandKey.Playlists.Tracks, CommandKey.Playlists.LoadShare.CreatedBy);
_ = embed.AsInfo(ctx, this.GetString(CommandKey.Playlists.Title));
embed.Thumbnail = new DiscordEmbedBuilder.EmbedThumbnail { Url = ImportJson.PlaylistThumbnail };
embed.Color = (ImportJson.PlaylistColor is "#FFFFFF" or null or "" ? EmbedColors.Info : new DiscordColor(ImportJson.PlaylistColor.IsValidHexColor()));
embed.Description = $"{this.GetString(CommandKey.Playlists.LoadShare.Found, true)}\n\n" +
$"`{this.GetString(CommandKey.Playlists.LoadShare.PlaylistName).PadRight(pad)}`: `{ImportJson.PlaylistName}`\n" +
$"`{this.GetString(CommandKey.Playlists.Tracks).PadRight(pad)}`: `{ImportJson.List.Length}`\n" +
$"`{this.GetString(CommandKey.Playlists.LoadShare.CreatedBy).PadRight(pad)}`: {user.Mention} `{user.GetUsernameWithIdentifier()} ({user.Id})`";
DiscordButtonComponent Confirm = new(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.LoadShare.ImportButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("📥")));
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed).AddComponents(new List<DiscordComponent> { Confirm, MessageComponents.GetCancelButton(ctx.DbUser, ctx.Bot) }));
var e = await ctx.WaitForButtonAsync(TimeSpan.FromMinutes(1));
if (e.TimedOut)
{
this.ModifyToTimedOut();
return;
}
_ = e.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
if (e.GetCustomId() == Confirm.CustomId)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.LoadShare.Importing, true),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title))));
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
return;
}
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Add(ImportJson);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.LoadShare.Imported, true, new TVar("Name", ImportJson.PlaylistName)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
}
else
{
this.DeleteOrInvalidate();
}
});
}
}

View file

@ -0,0 +1,253 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
namespace ProjectMakoto.Plugins.Music;
internal sealed class ManageCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var countInt = 0;
int GetCount()
{
countInt++;
return countInt;
}
var builder = new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder()
{
Description = $"{(MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length > 0 ? string.Join("\n", MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Select(x => $"**{GetCount()}**. `{x.PlaylistName.SanitizeForCode()}`: `{x.List.Length} {this.GetString(CommandKey.Playlists.Tracks)}`")) : this.GetString(CommandKey.Playlists.Manage.NoPlaylists, true))}"
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)));
var AddToQueue = new DiscordButtonComponent(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.AddToQueueButton), (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length <= 0), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("📤")));
var SharePlaylist = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.ShareButton), (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length <= 0), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("📎")));
var ExportPlaylist = new DiscordButtonComponent(ButtonStyle.Secondary, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.ExportButton), (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length <= 0), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("📋")));
var ImportPlaylist = new DiscordButtonComponent(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.ImportButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("📥")));
var SaveCurrent = new DiscordButtonComponent(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.SaveCurrentButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("💾")));
var NewPlaylist = new DiscordButtonComponent(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.CreateNewButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("")));
var ModifyPlaylist = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.ModifyButton), (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length <= 0), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("⚙")));
var DeletePlaylist = new DiscordButtonComponent(ButtonStyle.Danger, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.Manage.DeleteButton), (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length <= 0), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🗑")));
_ = await this.RespondOrEdit(builder
.AddComponents(new List<DiscordComponent> {
AddToQueue,
SharePlaylist,
ExportPlaylist
})
.AddComponents(new List<DiscordComponent>
{
ImportPlaylist,
SaveCurrent,
NewPlaylist
})
.AddComponents(new List<DiscordComponent>
{
ModifyPlaylist,
DeletePlaylist
})
.AddComponents(MessageComponents.GetCancelButton(ctx.DbUser, ctx.Bot)));
var e = await ctx.WaitForButtonAsync(TimeSpan.FromMinutes(1));
if (e.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
_ = e.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
if (e.GetCustomId() == AddToQueue.CustomId)
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Manage.PlaylistSelectorQueue, true)
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)));
var PlaylistResult = await this.PromptCustomSelection(GetPlaylistOptions());
if (PlaylistResult.TimedOut)
{
this.ModifyToTimedOut();
return;
}
else if (PlaylistResult.Cancelled)
{
this.DeleteOrInvalidate();
return;
}
else if (PlaylistResult.Errored)
{
throw PlaylistResult.Exception;
}
await new AddToQueueCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", PlaylistResult.Result }
});
return;
}
else if (e.GetCustomId() == SharePlaylist.CustomId)
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Manage.PlaylistSelectorShare, true)
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)));
var PlaylistResult = await this.PromptCustomSelection(GetPlaylistOptions());
if (PlaylistResult.TimedOut)
{
this.ModifyToTimedOut();
return;
}
else if (PlaylistResult.Cancelled)
{
this.DeleteOrInvalidate();
return;
}
else if (PlaylistResult.Errored)
{
throw PlaylistResult.Exception;
}
await new ShareCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", PlaylistResult.Result }
});
return;
}
else if (e.GetCustomId() == ExportPlaylist.CustomId)
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Manage.PlaylistSelectorExport, true)
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)));
var PlaylistResult = await this.PromptCustomSelection(GetPlaylistOptions());
if (PlaylistResult.TimedOut)
{
this.ModifyToTimedOut();
return;
}
else if (PlaylistResult.Cancelled)
{
this.DeleteOrInvalidate();
return;
}
else if (PlaylistResult.Errored)
{
throw PlaylistResult.Exception;
}
await new ExportCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", PlaylistResult.Result }
});
return;
}
else if (e.GetCustomId() == NewPlaylist.CustomId)
{
await new NewPlaylistCommand().TransferCommand(ctx, null);
return;
}
else if (e.GetCustomId() == SaveCurrent.CustomId)
{
await new SaveCurrentCommand().TransferCommand(ctx, null);
return;
}
else if (e.GetCustomId() == ImportPlaylist.CustomId)
{
await new ImportCommand().TransferCommand(ctx, null);
await this.ExecuteCommand(ctx, arguments);
return;
}
else if (e.GetCustomId() == ModifyPlaylist.CustomId)
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Manage.PlaylistSelectorModify, true)
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)));
var PlaylistResult = await this.PromptCustomSelection(GetPlaylistOptions());
if (PlaylistResult.TimedOut)
{
this.ModifyToTimedOut();
return;
}
else if (PlaylistResult.Cancelled)
{
this.DeleteOrInvalidate();
return;
}
else if (PlaylistResult.Errored)
{
throw PlaylistResult.Exception;
}
await new ModifyCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", PlaylistResult.Result }
});
return;
}
else if (e.GetCustomId() == DeletePlaylist.CustomId)
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Manage.PlaylistSelectorDelete, true)
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)));
var PlaylistResult = await this.PromptCustomSelection(GetPlaylistOptions());
if (PlaylistResult.TimedOut)
{
this.ModifyToTimedOut();
return;
}
else if (PlaylistResult.Cancelled)
{
this.DeleteOrInvalidate();
return;
}
else if (PlaylistResult.Errored)
{
throw PlaylistResult.Exception;
}
await new DeleteCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", PlaylistResult.Result }
});
await this.ExecuteCommand(ctx, arguments);
return;
}
else
{
this.DeleteOrInvalidate();
}
List<DiscordStringSelectComponentOption> GetPlaylistOptions()
=> MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Select(x => new DiscordStringSelectComponentOption($"{x.PlaylistName}", x.PlaylistId, $"{x.List.Length} {this.GetString(CommandKey.Playlists.Tracks)}")).ToList();
});
}
}

View file

@ -0,0 +1,413 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
using DisCatSharp.Interactivity.Extensions;
using ProjectMakoto.Entities.Users;
using Xorog.UniversalExtensions;
using Xorog.UniversalExtensions.Enums;
namespace ProjectMakoto.Plugins.Music;
internal sealed class ModifyCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var playlistId = (string)arguments["playlist"];
if (!MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Any(x => x.PlaylistId == playlistId))
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.NoPlaylist, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title)));
return;
}
var SelectedPlaylist = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.First(x => x.PlaylistId == playlistId);
var embed = new DiscordEmbedBuilder().AsInfo(ctx);
var LastInt = 0;
int GetInt()
{
LastInt++;
return LastInt;
}
var CurrentPage = 0;
async Task UpdateMessage()
{
LastInt = CurrentPage * 10;
var CurrentTracks = SelectedPlaylist.List.Skip(CurrentPage * 10).Take(10);
DiscordButtonComponent NextPage = new(ButtonStyle.Primary, "NextPage", this.GetString(this.t.Common.NextPage), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("▶")));
DiscordButtonComponent PreviousPage = new(ButtonStyle.Primary, "PreviousPage", this.GetString(this.t.Common.PreviousPage), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("◀")));
DiscordButtonComponent PlaylistName = new(ButtonStyle.Success, "ChangePlaylistName", this.GetString(CommandKey.Playlists.Modify.ChangeName), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("💬")));
DiscordButtonComponent ChangePlaylistColor = new(ButtonStyle.Secondary, "ChangeColor", this.GetString(CommandKey.Playlists.Modify.ChangeColor), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🎨")));
DiscordButtonComponent ChangePlaylistThumbnail = new(ButtonStyle.Secondary, "ChangeThumbnail", this.GetString(CommandKey.Playlists.Modify.ChangeThumbnail), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🖼")));
DiscordButtonComponent AddSong = new(ButtonStyle.Success, "AddSong", this.GetString(CommandKey.Playlists.Modify.AddTracks), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("")));
DiscordButtonComponent RemoveSong = new(ButtonStyle.Danger, "DeleteSong", this.GetString(CommandKey.Playlists.Modify.RemoveTracks), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🗑")));
DiscordButtonComponent RemoveDuplicates = new(ButtonStyle.Secondary, "RemoveDuplicates", this.GetString(CommandKey.Playlists.Modify.RemoveDuplicates), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("♻")));
var TotalTimespan = TimeSpan.Zero;
for (var i = 0; i < SelectedPlaylist.List.Length; i++)
{
TotalTimespan = TotalTimespan.Add(SelectedPlaylist.List[i].Length.Value);
}
var Description = $"**`{this.GetString(CommandKey.Playlists.Modify.CurrentTrackCount, new TVar("Count", SelectedPlaylist.List.Length), new TVar("Timespan", TotalTimespan.GetHumanReadable()))}`**\n\n";
Description += $"{string.Join("\n", CurrentTracks.Select(x => $"**{GetInt()}**. `{x.Length.Value.GetShortHumanReadable(TimeFormat.Hours)}` {this.GetString(CommandKey.Playlists.Modify.Track, new TVar("Track", $"**[`{x.Title}`]({x.Url})**"), new TVar("Timestamp", Formatter.Timestamp(x.AddedTime)))}"))}";
if (SelectedPlaylist.List.Length > 0)
Description += $"\n\n`{this.GetString(this.t.Common.Page)} {CurrentPage + 1}/{Math.Ceiling(SelectedPlaylist.List.Length / 10.0)}`";
if (CurrentPage <= 0)
PreviousPage = PreviousPage.Disable();
if ((CurrentPage * 10) + 10 >= SelectedPlaylist.List.Length)
NextPage = NextPage.Disable();
embed.Author.IconUrl = ctx.Guild.IconUrl;
embed.Color = (SelectedPlaylist.PlaylistColor is "#FFFFFF" or null or "" ? EmbedColors.Info : new DiscordColor(SelectedPlaylist.PlaylistColor.IsValidHexColor()));
embed.Title = $"{this.GetString(CommandKey.Playlists.Modify.ModifyingPlaylist)}: `{SelectedPlaylist.PlaylistName}`";
embed.Description = Description;
embed.Thumbnail = new DiscordEmbedBuilder.EmbedThumbnail { Url = (SelectedPlaylist.PlaylistThumbnail.IsNullOrWhiteSpace() ? "" : SelectedPlaylist.PlaylistThumbnail) };
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed)
.AddComponents(new List<DiscordComponent> { PreviousPage, NextPage })
.AddComponents(new List<DiscordComponent> { AddSong, RemoveSong, RemoveDuplicates })
.AddComponents(new List<DiscordComponent> { PlaylistName, ChangePlaylistColor, ChangePlaylistThumbnail })
.AddComponents(MessageComponents.GetCancelButton(ctx.DbUser, ctx.Bot)));
return;
}
await UpdateMessage();
CancellationTokenSource tokenSource = new();
_ = Task.Delay(120000, tokenSource.Token).ContinueWith(x =>
{
if (x.IsCompletedSuccessfully)
{
ctx.Client.ComponentInteractionCreated -= RunInteraction;
this.ModifyToTimedOut();
}
});
ctx.Client.ComponentInteractionCreated += RunInteraction;
async Task RunInteraction(DiscordClient s, ComponentInteractionCreateEventArgs e)
{
_ = Task.Run(async () =>
{
if (e.Message?.Id == ctx.ResponseMessage.Id && e.User.Id == ctx.User.Id)
{
tokenSource.Cancel();
tokenSource = new();
_ = Task.Delay(120000, tokenSource.Token).ContinueWith(x =>
{
if (x.IsCompletedSuccessfully)
{
ctx.Client.ComponentInteractionCreated -= RunInteraction;
this.ModifyToTimedOut();
}
});
switch (e.GetCustomId())
{
case "AddSong":
{
if (SelectedPlaylist.List.Length >= 250)
{
embed.Description = this.GetString(CommandKey.Playlists.Modify.TrackLimit, true);
_ = embed.AsError(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed.Build());
_ = Task.Delay(5000).ContinueWith(async x =>
{
await UpdateMessage();
});
return;
}
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.Modify.AddSong), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "query", this.GetString(CommandKey.Playlists.CreatePlaylist.SupportedAddType), "", 1, 100, true));
var ModalResult = await this.PromptModalWithRetry(e.Interaction, modal, false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
await UpdateMessage();
break;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
var (Tracks, oriResult, Continue) = await MusicModuleAbstractions.GetLoadResult(ctx, ModalResult.Result.Interaction.GetModalValueByCustomId("query"));
if (!Continue)
{
await UpdateMessage();
break;
}
if (SelectedPlaylist.List.Length >= 250)
{
embed.Description = this.GetString(CommandKey.Playlists.Modify.TrackLimit, true);
_ = embed.AsError(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed.Build());
_ = Task.Delay(5000).ContinueWith(async x =>
{
await UpdateMessage();
});
return;
}
SelectedPlaylist.List = SelectedPlaylist.List.AddRange(Tracks.Take(250 - SelectedPlaylist.List.Length).Select(x => new PlaylistEntry { Title = x.Info.Title, Url = x.Info.Uri.ToString(), Length = x.Info.Length }));
await UpdateMessage();
break;
}
case "ChangeThumbnail":
{
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
try
{
embed = new DiscordEmbedBuilder
{
Description = $"{this.GetString(CommandKey.Playlists.Modify.UploadThumbnail, true, new TVar("Command", $"{ctx.Prefix}upload"))}\n\n" +
$"⚠ {this.GetString(CommandKey.Playlists.ThumbnailModerationNote, true)}",
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed));
(Stream stream, int fileSize) stream;
try
{
stream = await this.PromptForFileUpload();
}
catch (AlreadyAppliedException)
{
return;
}
catch (ArgumentException)
{
this.ModifyToTimedOut();
return;
}
embed.Description = this.GetString(CommandKey.Playlists.Modify.ImportingThumbnail, true);
_ = embed.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed.Build());
if (stream.fileSize > ctx.Bot.status.SafeReadOnlyConfig.Discord.MaxUploadSize)
{
embed.Description = this.GetString(CommandKey.Playlists.Modify.ThumbnailSizeError, true, new TVar("Size", ctx.Bot.status.SafeReadOnlyConfig.Discord.MaxUploadSize.FileSizeToHumanReadable()));
_ = embed.AsError(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed.Build());
_ = Task.Delay(5000).ContinueWith(async x =>
{
await UpdateMessage();
});
return;
}
var asset = await (await ctx.Client.GetChannelAsync(ctx.Bot.status.SafeReadOnlyConfig.Channels.PlaylistAssets)).SendMessageAsync(new DiscordMessageBuilder().WithContent($"{ctx.User.Mention} `{ctx.User.GetUsernameWithIdentifier()} ({ctx.User.Id})`\n`{SelectedPlaylist.PlaylistName}`").WithFile($"{Guid.NewGuid()}.png", stream.stream));
SelectedPlaylist.PlaylistThumbnail = asset.Attachments[0].Url;
}
catch (Exception ex)
{
MusicPlugin.Plugin._logger.LogError(ex, "An exception occurred while trying to import thumbnail");
embed.Description = this.GetString(CommandKey.Playlists.Modify.ThumbnailError, true);
_ = embed.AsError(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(embed.Build());
_ = Task.Delay(5000).ContinueWith(async x =>
{
await UpdateMessage();
});
return;
}
await UpdateMessage();
break;
}
case "ChangeColor":
{
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.Modify.NewPlaylistColor), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "color", this.GetString(CommandKey.Playlists.Modify.NewPlaylistColor), "#FF0000", 1, 100, true, SelectedPlaylist.PlaylistColor));
var ModalResult = await this.PromptModalWithRetry(e.Interaction, modal, new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Modify.NewPlaylistColorPrompt, true,
new TVar("Hex", "#FF0000"),
new TVar("HelpUrl", $"` [`{this.GetString(CommandKey.Playlists.Modify.HexHelp)}`](https://g.co/kgs/jDHPp6)")),
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)), false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
await UpdateMessage();
break;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
SelectedPlaylist.PlaylistColor = ModalResult.Result.Interaction.GetModalValueByCustomId("color");
await UpdateMessage();
break;
}
case "ChangePlaylistName":
{
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.CreatePlaylist.SetPlaylistName), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "name", this.GetString(CommandKey.Playlists.CreatePlaylist.PlaylistName), "Playlist", 1, 100, true, SelectedPlaylist.PlaylistName));
var ModalResult = await this.PromptModalWithRetry(e.Interaction, modal, new DiscordEmbedBuilder
{
Description = $"⚠ {this.GetString(CommandKey.Playlists.NameModerationNote, true)}",
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)), false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
await UpdateMessage();
break;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
SelectedPlaylist.PlaylistName = ModalResult.Result.Interaction.GetModalValueByCustomId("name");
await UpdateMessage();
break;
}
case "RemoveDuplicates":
{
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
CurrentPage = 0;
SelectedPlaylist.List = SelectedPlaylist.List.GroupBy(x => x.Url).Select(y => y.FirstOrDefault()).ToArray();
await UpdateMessage();
break;
}
case "DeleteSong":
{
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
var TrackList = SelectedPlaylist.List.Skip(CurrentPage * 10).Take(10).Select(x => new DiscordStringSelectComponentOption($"{x.Title}", x.Url.MakeValidFileName(), $"Added {x.AddedTime.GetTimespanSince().GetHumanReadable()} ago")).ToList();
DiscordStringSelectComponent Tracks = new(this.GetString(CommandKey.Playlists.Modify.DeleteNote), TrackList, Guid.NewGuid().ToString(), 1, TrackList.Count);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed).AddComponents(Tracks));
var Response = await s.GetInteractivity().WaitForSelectAsync(ctx.ResponseMessage, x => x.User.Id == ctx.User.Id, ComponentType.StringSelect);
if (Response.TimedOut)
{
this.ModifyToTimedOut();
return;
}
_ = Response.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
foreach (var b in Response.Result.Values.Select(x => SelectedPlaylist.List.First(y => y.Url.MakeValidFileName() == x)))
{
SelectedPlaylist.List = SelectedPlaylist.List.Remove(x => x.Url, b);
}
if (SelectedPlaylist.List.Length <= 0)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.Delete.Deleted, true, new TVar("Name", SelectedPlaylist.PlaylistName)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Remove(x => x.PlaylistId, SelectedPlaylist);
await Task.Delay(5000);
return;
}
if (!SelectedPlaylist.List.Skip(CurrentPage * 10).Take(10).Any())
CurrentPage--;
await UpdateMessage();
break;
}
case "NextPage":
{
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
CurrentPage++;
await UpdateMessage();
break;
}
case "PreviousPage":
{
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
CurrentPage--;
await UpdateMessage();
break;
}
case "cancel":
{
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
ctx.Client.ComponentInteractionCreated -= RunInteraction;
if (!ctx.Transferred)
this.DeleteOrInvalidate();
else
_ = new ManageCommand().TransferCommand(ctx, null);
return;
}
}
}
}).Add(ctx.Bot, ctx);
}
return;
});
}
}

View file

@ -0,0 +1,180 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
using ProjectMakoto.Entities.Users;
namespace ProjectMakoto.Plugins.Music;
internal sealed class NewPlaylistCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var SelectedPlaylistName = "";
PlaylistEntry[] SelectedTracks = null;
while (true)
{
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
}
var SelectName = new DiscordButtonComponent((SelectedPlaylistName.IsNullOrWhiteSpace() ? ButtonStyle.Primary : ButtonStyle.Secondary), Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.CreatePlaylist.ChangeName), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🗯")));
var SelectFirstTracks = new DiscordButtonComponent((SelectedTracks is null ? ButtonStyle.Primary : ButtonStyle.Secondary), Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.CreatePlaylist.ChangeTracks), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🎵")));
var Finish = new DiscordButtonComponent(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.CreatePlaylist.CreatePlaylist), (SelectedPlaylistName.IsNullOrWhiteSpace()), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("✅")));
var pad = TranslationUtil.CalculatePadding(ctx.DbUser, CommandKey.Playlists.CreatePlaylist.PlaylistName, CommandKey.Playlists.CreatePlaylist.FirstTracks);
var embed = new DiscordEmbedBuilder
{
Description = $"`{this.GetString(CommandKey.Playlists.CreatePlaylist.PlaylistName).PadRight(pad)}`: `{(SelectedPlaylistName.IsNullOrWhiteSpace() ? this.GetString(this.t.Common.NotSelected) : SelectedPlaylistName)}`\n" +
$"`{this.GetString(CommandKey.Playlists.CreatePlaylist.FirstTracks).PadRight(pad)}`: {(SelectedTracks.IsNotNullAndNotEmpty() ? (SelectedTracks.Length > 1 ? $"`{SelectedTracks.Length} {this.GetString(CommandKey.Playlists.Tracks)}`" : $"[`{SelectedTracks[0].Title}`]({SelectedTracks[0].Url})") : this.GetString(this.t.Common.NotSelected, true))}"
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed)
.AddComponents(new List<DiscordComponent> { SelectName, SelectFirstTracks, Finish })
.AddComponents(MessageComponents.GetCancelButton(ctx.DbUser, ctx.Bot)));
var Menu = await ctx.WaitForButtonAsync();
if (Menu.TimedOut)
{
this.ModifyToTimedOut();
return;
}
if (Menu.GetCustomId() == SelectName.CustomId)
{
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.CreatePlaylist.SetPlaylistName), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "name", this.GetString(CommandKey.Playlists.CreatePlaylist.PlaylistName), this.GetString(CommandKey.Playlists.Title), 1, 100, true, (SelectedPlaylistName.IsNullOrWhiteSpace() ? "New Playlist" : SelectedPlaylistName)));
var ModalResult = await this.PromptModalWithRetry(Menu.Result.Interaction, modal, new DiscordEmbedBuilder
{
Description = $"⚠ {this.GetString(CommandKey.Playlists.NameModerationNote, true)}",
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)), false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
continue;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
SelectedPlaylistName = ModalResult.Result.Interaction.GetModalValueByCustomId("name");
continue;
}
else if (Menu.GetCustomId() == SelectFirstTracks.CustomId)
{
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.CreatePlaylist.SetFirstTracks), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "query", this.GetString(CommandKey.Playlists.CreatePlaylist.SupportedAddType), "Url", 1, 100, true));
var ModalResult = await this.PromptModalWithRetry(Menu.Result.Interaction, modal, false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
continue;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
var query = ModalResult.Result.Interaction.GetModalValueByCustomId("query");
var (Tracks, oriResult, Continue) = await MusicModuleAbstractions.GetLoadResult(ctx, query);
if (!Continue || !Tracks.IsNotNullAndNotEmpty())
continue;
SelectedTracks = Tracks.Select(x => new PlaylistEntry
{
Title = x.Info.Title,
Url = x.Info.Uri.ToString(),
Length = x.Info.Length
}).ToArray();
continue;
}
else if (Menu.GetCustomId() == Finish.CustomId)
{
_ = Menu.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
}
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.CreatePlaylist.Creating, true),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title))));
var v = new UserPlaylist
{
PlaylistName = SelectedPlaylistName,
List = SelectedTracks
};
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Add(v);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.CreatePlaylist.Created, true,
new TVar("Playlist", v.PlaylistName),
new TVar("Count", v.List.Length)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(2000);
await new ModifyCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", v.PlaylistId }
});
return;
}
else if (Menu.GetCustomId() == MessageComponents.CancelButtonId)
{
if (!ctx.Transferred)
this.DeleteOrInvalidate();
return;
}
return;
}
});
}
}

View file

@ -0,0 +1,152 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
using ProjectMakoto.Entities.Users;
namespace ProjectMakoto.Plugins.Music;
internal sealed class SaveCurrentCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
if (ctx.Member.VoiceState is null || ctx.Member.VoiceState.Channel.Id != (await ctx.Client.CurrentUser.ConvertToMember(ctx.Guild)).VoiceState?.Channel?.Id)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.NotSameChannel, true)
}.AsError(ctx)));
return;
}
var SelectedPlaylistName = "";
var SelectedTracks = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Select(x => new PlaylistEntry { Title = x.VideoTitle, Url = x.Url, Length = x.Length }).Take(250).ToArray();
while (true)
{
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
}
var SelectName = new DiscordButtonComponent((SelectedPlaylistName.IsNullOrWhiteSpace() ? ButtonStyle.Primary : ButtonStyle.Secondary), Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.CreatePlaylist.ChangeName), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🗯")));
var Finish = new DiscordButtonComponent(ButtonStyle.Success, Guid.NewGuid().ToString(), this.GetString(CommandKey.Playlists.CreatePlaylist.CreatePlaylist), (SelectedPlaylistName.IsNullOrWhiteSpace()), new DiscordComponentEmoji(DiscordEmoji.FromUnicode("✅")));
var pad = TranslationUtil.CalculatePadding(ctx.DbUser, CommandKey.Playlists.CreatePlaylist.PlaylistName, CommandKey.Playlists.CreatePlaylist.FirstTracks);
var embed = new DiscordEmbedBuilder
{
Description = $"`{this.GetString(CommandKey.Playlists.CreatePlaylist.PlaylistName).PadRight(pad)}`: `{(SelectedPlaylistName.IsNullOrWhiteSpace() ? this.GetString(this.t.Common.NotSelected) : SelectedPlaylistName)}`\n" +
$"`{this.GetString(CommandKey.Playlists.CreatePlaylist.FirstTracks).PadRight(pad)}`: {(SelectedTracks.IsNotNullAndNotEmpty() ? (SelectedTracks.Length > 1 ? $"`{SelectedTracks.Length} {this.GetString(CommandKey.Playlists.Tracks)}`" : $"[`{SelectedTracks[0].Title}`]({SelectedTracks[0].Url})") : this.GetString(this.t.Common.NotSelected, true))}"
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title));
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed)
.AddComponents(new List<DiscordComponent> { SelectName, Finish })
.AddComponents(MessageComponents.GetCancelButton(ctx.DbUser, ctx.Bot)));
var Menu = await ctx.WaitForButtonAsync();
if (Menu.TimedOut)
{
this.ModifyToTimedOut();
return;
}
if (Menu.GetCustomId() == SelectName.CustomId)
{
var modal = new DiscordInteractionModalBuilder(this.GetString(CommandKey.Playlists.CreatePlaylist.SetPlaylistName), Guid.NewGuid().ToString())
.AddTextComponent(new DiscordTextComponent(TextComponentStyle.Small, "name", this.GetString(CommandKey.Playlists.CreatePlaylist.PlaylistName), this.GetString(CommandKey.Playlists.Title), 1, 100, true, (SelectedPlaylistName.IsNullOrWhiteSpace() ? "New Playlist" : SelectedPlaylistName)));
var ModalResult = await this.PromptModalWithRetry(Menu.Result.Interaction, modal, new DiscordEmbedBuilder
{
Description = $"⚠ {this.GetString(CommandKey.Playlists.NameModerationNote, true)}",
}.AsAwaitingInput(ctx, this.GetString(CommandKey.Playlists.Title)), false);
if (ModalResult.TimedOut)
{
this.ModifyToTimedOut(true);
return;
}
else if (ModalResult.Cancelled)
{
continue;
}
else if (ModalResult.Errored)
{
throw ModalResult.Exception;
}
SelectedPlaylistName = ModalResult.Result.Interaction.GetModalValueByCustomId("name");
continue;
}
else if (Menu.GetCustomId() == Finish.CustomId)
{
_ = Menu.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
if (MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Length >= 10)
{
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.PlayListLimit, true, new TVar("Count", 10)),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(5000);
return;
}
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.CreatePlaylist.Creating, true),
}.AsLoading(ctx, this.GetString(CommandKey.Playlists.Title))));
var v = new UserPlaylist
{
PlaylistName = SelectedPlaylistName,
List = SelectedTracks
};
MusicPlugin.Plugin.Users[ctx.User.Id].Playlists = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Add(v);
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.CreatePlaylist.Created, true,
new TVar("Playlist", v.PlaylistName),
new TVar("Count", v.List.Length)),
}.AsSuccess(ctx, this.GetString(CommandKey.Playlists.Title))));
await Task.Delay(2000);
await new ModifyCommand().TransferCommand(ctx, new Dictionary<string, object>
{
{ "playlist", v.PlaylistId }
});
return;
}
else if (Menu.GetCustomId() == MessageComponents.CancelButtonId)
{
if (!ctx.Transferred)
this.DeleteOrInvalidate();
return;
}
return;
}
});
}
}

View file

@ -0,0 +1,53 @@
// Project Makoto
// Copyright (C) 2024 Fortunevale
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY
namespace ProjectMakoto.Plugins.Music;
internal sealed class ShareCommand : BaseCommand
{
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
{
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
return Task.Run(async () =>
{
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
return;
var playlistId = (string)arguments["playlist"];
if (!MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.Any(x => x.PlaylistId == playlistId))
{
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
{
Description = this.GetString(CommandKey.Playlists.NoPlaylist, true),
}.AsError(ctx, this.GetString(CommandKey.Playlists.Title)));
return;
}
var SelectedPlaylist = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists.First(x => x.PlaylistId == playlistId);
var ShareCode = $"{Guid.NewGuid()}";
if (!Directory.Exists("PlaylistShares"))
_ = Directory.CreateDirectory("PlaylistShares");
if (!Directory.Exists($"PlaylistShares/{ctx.User.Id}"))
_ = Directory.CreateDirectory($"PlaylistShares/{ctx.User.Id}");
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder()
{
Description = this.GetString(CommandKey.Playlists.Share.Shared, true,
new TVar("Command", $"{ctx.Prefix}playlists load-share {ctx.User.Id} {ShareCode}")),
}.AsInfo(ctx, this.GetString(CommandKey.Playlists.Title))));
File.WriteAllText($"PlaylistShares/{ctx.User.Id}/{ShareCode}.json", JsonConvert.SerializeObject(SelectedPlaylist, Formatting.Indented));
});
}
}