Initial commit
This commit is contained in:
commit
79e7cffb4e
49 changed files with 6399 additions and 0 deletions
58
Commands/AutocompleteProviders.cs
Normal file
58
Commands/AutocompleteProviders.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using DisCatSharp.ApplicationCommands.Attributes;
|
||||
using DisCatSharp.ApplicationCommands.Context;
|
||||
|
||||
namespace ProjectMakoto.Plugins.Music;
|
||||
|
||||
public class AutocompleteProviders
|
||||
{
|
||||
public sealed class SongQueueAutocompleteProvider : IAutocompleteProvider
|
||||
{
|
||||
public async Task<IEnumerable<DiscordApplicationCommandAutocompleteChoice>> Provider(AutocompleteContext ctx)
|
||||
{
|
||||
try
|
||||
{
|
||||
if ((ctx.Member?.VoiceState?.Channel?.Id ?? 0) != ((await ctx.Client.CurrentUser.ConvertToMember(ctx.Guild)).VoiceState?.Channel?.Id ?? 1))
|
||||
return new List<DiscordApplicationCommandAutocompleteChoice>().AsEnumerable();
|
||||
|
||||
var Queue = MusicPlugin.Plugin.Guilds![ctx.Guild.Id].SongQueue
|
||||
.Where(x => x.VideoTitle.StartsWith(ctx.FocusedOption.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)).Take(25);
|
||||
|
||||
var options = Queue.Select(x => new DiscordApplicationCommandAutocompleteChoice(x.VideoTitle, x.VideoTitle)).ToList();
|
||||
return options.AsEnumerable();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MusicPlugin.Plugin._logger.LogError(ex, "Failed to provide autocomplete for song queue");
|
||||
return new List<DiscordApplicationCommandAutocompleteChoice>().AsEnumerable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public sealed class PlaylistsAutoCompleteProvider : IAutocompleteProvider
|
||||
{
|
||||
public async Task<IEnumerable<DiscordApplicationCommandAutocompleteChoice>> Provider(AutocompleteContext ctx)
|
||||
{
|
||||
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
|
||||
|
||||
try
|
||||
{
|
||||
var bot = ((Bot)ctx.Services.GetService(typeof(Bot)));
|
||||
var Queue = MusicPlugin.Plugin.Users[ctx.User.Id].Playlists
|
||||
.Where(x => x.PlaylistName.Contains(ctx.FocusedOption.Value.ToString(), StringComparison.InvariantCultureIgnoreCase)).Take(25);
|
||||
|
||||
var options = Queue.Select(x => new DiscordApplicationCommandAutocompleteChoice($"{x.PlaylistName} ({x.List.Length} {CommandKey.Playlists.Tracks.Get(bot.Users[ctx.User.Id]).Build()})", x.PlaylistId)).ToList();
|
||||
return options.AsEnumerable();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MusicPlugin.Plugin._logger.LogError(ex, "Failed to provide autocomplete for playlists");
|
||||
return new List<DiscordApplicationCommandAutocompleteChoice>().AsEnumerable();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
127
Commands/Music/ClearQueueCommand.cs
Normal file
127
Commands/Music/ClearQueueCommand.cs
Normal file
|
|
@ -0,0 +1,127 @@
|
|||
// 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 ClearQueueCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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 lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Contains(ctx.User.Id))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.ClearQueue.AlreadyVoted, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Add(ctx.User.Id);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Count >= (conn.Channel.Users.Count - 1) * 0.51)
|
||||
{
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = [];
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Clear();
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.ClearQueue.Cleared, true),
|
||||
}.AsSuccess(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
var embed = new DiscordEmbedBuilder()
|
||||
{
|
||||
Description = $"`{this.GetGuildString(CommandKey.ClearQueue.VoteStarted)} ({MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Count}/{Math.Ceiling((conn.Channel.Users.Count - 1.0) * 0.51)})`",
|
||||
}.AsAwaitingInput(ctx);
|
||||
|
||||
var builder = new DiscordMessageBuilder().WithEmbed(embed);
|
||||
|
||||
DiscordButtonComponent DisconnectVote = new(ButtonStyle.Danger, Guid.NewGuid().ToString(), this.GetGuildString(CommandKey.ClearQueue.VoteButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🗑")));
|
||||
_ = builder.AddComponents(DisconnectVote);
|
||||
|
||||
_ = await this.RespondOrEdit(builder);
|
||||
|
||||
_ = Task.Delay(TimeSpan.FromMinutes(10)).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.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Contains(e.User.Id))
|
||||
{
|
||||
_ = e.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent($"❌ {this.GetString(CommandKey.ClearQueue.AlreadyVoted, true)}").AsEphemeral());
|
||||
return;
|
||||
}
|
||||
|
||||
var member = await e.User.ConvertToMember(ctx.Guild);
|
||||
|
||||
if (member.VoiceState is null || member.VoiceState.Channel.Id != conn.Channel.Id)
|
||||
{
|
||||
_ = e.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent($"❌ {this.GetString(CommandKey.NotSameChannel, true)}").AsEphemeral());
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Add(e.User.Id);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Count >= (conn.Channel.Users.Count - 1) * 0.51)
|
||||
{
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = [];
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Clear();
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.ClearQueue.Cleared, true),
|
||||
}.AsSuccess(ctx)));
|
||||
return;
|
||||
}
|
||||
|
||||
embed.Description = $"`{this.GetGuildString(CommandKey.ClearQueue.VoteStarted)} ({MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Count}/{Math.Ceiling((conn.Channel.Users.Count - 1.0) * 0.51)})`";
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed).AddComponents(DisconnectVote));
|
||||
}
|
||||
}).Add(ctx.Bot);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
131
Commands/Music/DisconnectCommand.cs
Normal file
131
Commands/Music/DisconnectCommand.cs
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
// 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 DisconnectCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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 lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Contains(ctx.User.Id))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Disconnect.AlreadyVoted, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Add(ctx.User.Id);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Count >= (conn.Channel.Users.Count - 1) * 0.51)
|
||||
{
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Dispose(ctx.Bot, ctx.Guild.Id, "Graceful Disconnect");
|
||||
|
||||
_ = await conn.StopAsync();
|
||||
await conn.DisconnectAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Disconnect.Disconnected, true),
|
||||
}.AsSuccess(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
var embed = new DiscordEmbedBuilder()
|
||||
{
|
||||
Description = $"`{this.GetGuildString(CommandKey.Disconnect.VoteStarted, true)} ({MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Count}/{Math.Ceiling((conn.Channel.Users.Count - 1.0) * 0.51)})`",
|
||||
}.AsAwaitingInput(ctx);
|
||||
|
||||
var builder = new DiscordMessageBuilder().WithEmbed(embed);
|
||||
|
||||
DiscordButtonComponent DisconnectVote = new(ButtonStyle.Danger, Guid.NewGuid().ToString(), this.GetGuildString(CommandKey.Disconnect.VoteButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("⛔")));
|
||||
_ = builder.AddComponents(DisconnectVote);
|
||||
|
||||
_ = await this.RespondOrEdit(builder);
|
||||
|
||||
_ = Task.Delay(TimeSpan.FromMinutes(10)).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.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Contains(e.User.Id))
|
||||
{
|
||||
_ = e.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent($"❌ {this.GetString(CommandKey.Disconnect.AlreadyVoted, true)}").AsEphemeral());
|
||||
return;
|
||||
}
|
||||
|
||||
var member = await e.User.ConvertToMember(ctx.Guild);
|
||||
|
||||
if (member.VoiceState is null || member.VoiceState.Channel.Id != conn.Channel.Id)
|
||||
{
|
||||
_ = e.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent($"❌ {this.GetString(CommandKey.NotSameChannel, true)}").AsEphemeral());
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Add(e.User.Id);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Count >= (conn.Channel.Users.Count - 1) * 0.51)
|
||||
{
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Dispose(ctx.Bot, ctx.Guild.Id, "Graceful Disconnect");
|
||||
|
||||
_ = await conn.StopAsync();
|
||||
await conn.DisconnectAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Disconnect.Disconnected, true)
|
||||
}.AsSuccess(ctx)));
|
||||
return;
|
||||
}
|
||||
|
||||
embed.Description = $"`{this.GetGuildString(CommandKey.Disconnect.VoteStarted, true)} ({MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedDisconnectVotes.Count}/{Math.Ceiling((conn.Channel.Users.Count - 1.0) * 0.51)})`";
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed).AddComponents(DisconnectVote));
|
||||
}
|
||||
}).Add(ctx.Bot);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
56
Commands/Music/ForceClearQueueCommand.cs
Normal file
56
Commands/Music/ForceClearQueueCommand.cs
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
// 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 ForceClearQueueCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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 lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ctx.Member.IsDJ(ctx.Bot.status))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.DjRole, true, new TVar("Role", "DJ")),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = [];
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedClearQueueVotes.Clear();
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.ForceClearQueue.Cleared, true),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
58
Commands/Music/ForceDisconnectCommand.cs
Normal file
58
Commands/Music/ForceDisconnectCommand.cs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
// 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 ForceDisconnectCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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 lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ctx.Member.IsDJ(ctx.Bot.status))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.DjRole, true, new TVar("Role", "DJ")),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Dispose(ctx.Bot, ctx.Guild.Id, "Graceful Disconnect");
|
||||
|
||||
_ = await conn.StopAsync();
|
||||
await conn.DisconnectAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.ForceDisconnect.Disconnected, true),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
55
Commands/Music/ForceSkipCommand.cs
Normal file
55
Commands/Music/ForceSkipCommand.cs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
// 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 ForceSkipCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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 lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ctx.Member.IsDJ(ctx.Bot.status))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.DjRole, true, new TVar("Role", "DJ")),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
_ = await conn.StopAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.ForceSkip.Skipped, true),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
77
Commands/Music/JoinCommand.cs
Normal file
77
Commands/Music/JoinCommand.cs
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
// 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 JoinCommand : BaseCommand
|
||||
{
|
||||
public override async Task<bool> BeforeExecution(SharedCommandContext ctx) => (await this.CheckVoiceState() && await this.CheckOwnPermissions(Permissions.UseVoice) && await this.CheckOwnPermissions(Permissions.UseVoiceDetection));
|
||||
|
||||
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
|
||||
{
|
||||
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
|
||||
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
var Announce = arguments?.ContainsKey("announce") ?? false;
|
||||
|
||||
if (Announce)
|
||||
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
|
||||
return;
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
|
||||
while (!lava.ConnectedSessions.Values.Any(x => x.IsConnected))
|
||||
await Task.Delay(1000);
|
||||
|
||||
var node = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = node.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null)
|
||||
{
|
||||
if (!lava.ConnectedSessions.Any())
|
||||
{
|
||||
throw new Exception("Lavalink connection isn't established.");
|
||||
}
|
||||
|
||||
conn = await node.ConnectAsync(ctx.Member.VoiceState.Channel);
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].QueueHandler(ctx.Bot, ctx.Client, node, conn);
|
||||
|
||||
if (Announce)
|
||||
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Join.Joined, true),
|
||||
}.AsSuccess(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (conn.Channel.Users.Count >= 2 && !(ctx.Member.VoiceState.Channel.Id == conn.Channel.Id))
|
||||
{
|
||||
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Join.AlreadyUsed, true),
|
||||
}.AsError(ctx));
|
||||
|
||||
throw new CancelException();
|
||||
}
|
||||
|
||||
if (ctx.Member.VoiceState.Channel.Id != conn.Channel.Id)
|
||||
{
|
||||
await conn.DisconnectAsync();
|
||||
conn = await node.ConnectAsync(ctx.Member.VoiceState.Channel);
|
||||
}
|
||||
|
||||
if (Announce)
|
||||
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Join.Joined, true),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
48
Commands/Music/PauseCommand.cs
Normal file
48
Commands/Music/PauseCommand.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// 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 PauseCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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.WaitForLight(ctx))
|
||||
return;
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].IsPaused = !MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].IsPaused;
|
||||
|
||||
_ = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].IsPaused ? conn.PauseAsync() : conn.ResumeAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].IsPaused ? this.GetString(CommandKey.Pause.Paused, true) : this.GetString(CommandKey.Pause.Resumed, true)),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
110
Commands/Music/PlayCommand.cs
Normal file
110
Commands/Music/PlayCommand.cs
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
// 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 Xorog.UniversalExtensions;
|
||||
using Xorog.UniversalExtensions.Enums;
|
||||
|
||||
namespace ProjectMakoto.Plugins.Music;
|
||||
|
||||
internal sealed class PlayCommand : BaseCommand
|
||||
{
|
||||
public override async Task<bool> BeforeExecution(SharedCommandContext ctx) => (await this.CheckVoiceState() && await this.CheckOwnPermissions(Permissions.UseVoice) && await this.CheckOwnPermissions(Permissions.UseVoiceDetection));
|
||||
|
||||
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
|
||||
{
|
||||
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
|
||||
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
var search = (string)arguments["search"];
|
||||
|
||||
if (await ctx.DbUser.Cooldown.WaitForModerate(ctx))
|
||||
return;
|
||||
|
||||
if (search.IsNullOrWhiteSpace())
|
||||
{
|
||||
this.SendSyntaxError();
|
||||
return;
|
||||
}
|
||||
|
||||
var embed = new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Play.Preparing, true),
|
||||
}.AsLoading(ctx);
|
||||
_ = await this.RespondOrEdit(embed);
|
||||
|
||||
try
|
||||
{
|
||||
await new JoinCommand().TransferCommand(ctx, null);
|
||||
}
|
||||
catch (CancelException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var (Tracks, oriResult, Continue) = await MusicModuleAbstractions.GetLoadResult(ctx, search);
|
||||
|
||||
|
||||
embed.Author.IconUrl = ctx.Guild.IconUrl;
|
||||
|
||||
if (!Continue || !Tracks.IsNotNullAndNotEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_ = await this.RespondOrEdit(embed);
|
||||
|
||||
try
|
||||
{
|
||||
await new JoinCommand().TransferCommand(ctx, null);
|
||||
}
|
||||
catch (CancelException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (Tracks.Count > 1)
|
||||
{
|
||||
var added = 0;
|
||||
|
||||
foreach (var b in Tracks)
|
||||
{
|
||||
added++;
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Add(new(b.Info.Title, b.Info.Uri.ToString(), b.Info.Length, ctx.Guild.Id, ctx.User.Id));
|
||||
}
|
||||
|
||||
embed.Description = this.GetString(CommandKey.Play.QueuedMultiple, true,
|
||||
new TVar("Count", added),
|
||||
new TVar("Playlist", new EmbeddedLink(search, oriResult.GetResultAs<LavalinkPlaylist>().Info.Name)));
|
||||
|
||||
_ = embed.AddField(new DiscordEmbedField($"📜 {this.GetString(CommandKey.Play.QueuePositions)}", $"{(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length - added + 1)} - {MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length}", true));
|
||||
|
||||
_ = embed.AsSuccess(ctx);
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed);
|
||||
}
|
||||
else if (Tracks.Count == 1)
|
||||
{
|
||||
var track = Tracks[0];
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Add(new(track.Info.Title, track.Info.Uri.ToString(), track.Info.Length, ctx.Guild.Id, ctx.User.Id));
|
||||
|
||||
embed.Description = this.GetString(CommandKey.Play.QueuedSingle, true,
|
||||
new TVar("Track", new EmbeddedLink(track.Info.Uri.ToString(), track.Info.Title)));
|
||||
|
||||
_ = embed.AddField(new DiscordEmbedField($"📜 {this.GetString(CommandKey.Play.QueuePosition)}", $"{MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length}", true));
|
||||
_ = embed.AddField(new DiscordEmbedField($"🔼 {this.GetString(CommandKey.Play.Uploader)}", $"{track.Info.Author}", true));
|
||||
_ = embed.AddField(new DiscordEmbedField($"🕒 {this.GetString(CommandKey.Play.Duration)}", $"{track.Info.Length.GetHumanReadable(TimeFormat.Minutes)}", true));
|
||||
|
||||
_ = embed.AsSuccess(ctx);
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed.Build());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
186
Commands/Music/QueueCommand.cs
Normal file
186
Commands/Music/QueueCommand.cs
Normal file
|
|
@ -0,0 +1,186 @@
|
|||
// 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 System.Diagnostics;
|
||||
using Xorog.UniversalExtensions;
|
||||
using Xorog.UniversalExtensions.Enums;
|
||||
|
||||
namespace ProjectMakoto.Plugins.Music;
|
||||
|
||||
internal sealed class QueueCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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.WaitForLight(ctx))
|
||||
return;
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
var LastInt = 0;
|
||||
int GetInt()
|
||||
{
|
||||
LastInt++;
|
||||
return LastInt;
|
||||
}
|
||||
|
||||
var CurrentPage = 0;
|
||||
|
||||
async Task UpdateMessage()
|
||||
{
|
||||
DiscordButtonComponent Refresh = new(ButtonStyle.Primary, "Refresh", this.GetString(this.t.Common.Refresh), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("🔁")));
|
||||
|
||||
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 PlayPause = new(ButtonStyle.Secondary, "Playback", this.GetString(CommandKey.Queue.Play), conn.Player.Track is null, (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].IsPaused ? EmojiTemplates.GetPaused(ctx.Bot) : (conn.Player.Track is not null ? "▶".UnicodeToEmoji() : EmojiTemplates.GetDisabledPlay(ctx.Bot))).ToComponent());
|
||||
DiscordButtonComponent Repeat = new(ButtonStyle.Secondary, "Repeat", this.GetString(CommandKey.Queue.Repeat), conn.Player.Track is null, (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Repeat ? "🔁".UnicodeToEmoji() : EmojiTemplates.GetDisabledRepeat(ctx.Bot)).ToComponent());
|
||||
DiscordButtonComponent Shuffle = new(ButtonStyle.Secondary, "Shuffle", this.GetString(CommandKey.Queue.Shuffle), conn.Player.Track is null, (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Shuffle ? "🔀".UnicodeToEmoji() : EmojiTemplates.GetDisabledShuffle(ctx.Bot)).ToComponent());
|
||||
|
||||
LastInt = CurrentPage * 10;
|
||||
|
||||
var TotalTimespan = TimeSpan.Zero;
|
||||
|
||||
for (var i = 0; i < MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length; i++)
|
||||
{
|
||||
TotalTimespan = TotalTimespan.Add(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue[i].Length);
|
||||
}
|
||||
|
||||
var Description = $"{this.GetString(CommandKey.Queue.QueueCount, true, new TVar("Count", MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length), new TVar("Timespan", TotalTimespan.GetHumanReadable())).Bold()}\n\n";
|
||||
Description += $"{string.Join("\n", MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Skip(CurrentPage * 10).Take(10).Select(x => $"**{GetInt()}**. `{x.Length.GetShortHumanReadable(TimeFormat.Hours)}` {this.GetString(CommandKey.Queue.Track, new TVar("Video", $"[`{x.VideoTitle}`]({x.Url})"), new TVar("Requester", $"<@{x.UserId}>"))}"))}\n\n";
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length > 0)
|
||||
Description += $"`{this.GetString(this.t.Common.Page)} {CurrentPage + 1}/{Math.Ceiling(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length / 10.0)}`\n\n";
|
||||
|
||||
Description += $"`{this.GetString(CommandKey.Queue.CurrentlyPlaying)}:` [`{(conn.Player.Track is not null ? conn.Player.Track.Info.Title : this.GetString(CommandKey.Queue.NoSong))}`]({(conn.Player.Track is not null ? conn.Player.Track.Info.Uri.ToString() : "")})\n";
|
||||
Description += $"{(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Repeat ? "🔁".UnicodeToEmoji() : EmojiTemplates.GetDisabledRepeat(ctx.Bot))}";
|
||||
Description += $"{(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Shuffle ? "🔀".UnicodeToEmoji() : EmojiTemplates.GetDisabledShuffle(ctx.Bot))}";
|
||||
Description += $" `|` {(MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].IsPaused ? EmojiTemplates.GetPaused(ctx.Bot) : $"{(conn.Player.Track is not null ? "▶".UnicodeToEmoji() : EmojiTemplates.GetDisabledPlay(ctx.Bot))} ")}";
|
||||
|
||||
if (conn.CurrentTrack is not null)
|
||||
{
|
||||
Description += $"`[{((long)Math.Round(conn.Player.PlayerState.Position.TotalSeconds, 0)).GetShortHumanReadable(TimeFormat.Minutes)}/{((long)Math.Round(conn.Player.Track.Info.Length.TotalSeconds, 0)).GetShortHumanReadable(TimeFormat.Minutes)}]` ";
|
||||
Description += $"`{StringTools.GenerateASCIIProgressbar(Math.Round(conn.Player.PlayerState.Position.TotalSeconds, 0), Math.Round(conn.Player.Track.Info.Length.TotalSeconds, 0))}`";
|
||||
}
|
||||
|
||||
if (CurrentPage <= 0)
|
||||
PreviousPage = PreviousPage.Disable();
|
||||
|
||||
if ((CurrentPage * 10) + 10 >= MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length)
|
||||
NextPage = NextPage.Disable();
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = Description
|
||||
}.AsInfo(ctx))
|
||||
.AddComponents(PreviousPage, NextPage, Refresh)
|
||||
.AddComponents(PlayPause, Repeat, Shuffle));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
await UpdateMessage();
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
while (sw.ElapsedMilliseconds < 120000)
|
||||
{
|
||||
await UpdateMessage();
|
||||
Thread.Sleep(10000);
|
||||
}
|
||||
});
|
||||
|
||||
_ = Task.Run(() =>
|
||||
{
|
||||
while (sw.ElapsedMilliseconds < 120000)
|
||||
Thread.Sleep(1000);
|
||||
|
||||
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)
|
||||
{
|
||||
sw.Restart();
|
||||
|
||||
switch (e.GetCustomId())
|
||||
{
|
||||
case "Playback":
|
||||
{
|
||||
await new Music.PauseCommand().ExecuteCommand(e, s, "pause", ctx.Bot).Add(ctx.Bot);
|
||||
|
||||
await UpdateMessage();
|
||||
break;
|
||||
}
|
||||
case "Repeat":
|
||||
{
|
||||
await new Music.RepeatCommand().ExecuteCommand(e, s, "repeat", ctx.Bot).Add(ctx.Bot);
|
||||
|
||||
await UpdateMessage();
|
||||
break;
|
||||
}
|
||||
case "Shuffle":
|
||||
{
|
||||
await new Music.ShuffleCommand().ExecuteCommand(e, s, "shuffle", ctx.Bot).Add(ctx.Bot);
|
||||
|
||||
await UpdateMessage();
|
||||
break;
|
||||
}
|
||||
case "Refresh":
|
||||
{
|
||||
_ = e.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}).Add(ctx.Bot, ctx);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
97
Commands/Music/RemoveQueueCommand.cs
Normal file
97
Commands/Music/RemoveQueueCommand.cs
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
// 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.Guilds;
|
||||
using Xorog.UniversalExtensions;
|
||||
|
||||
namespace ProjectMakoto.Plugins.Music;
|
||||
|
||||
internal sealed class RemoveQueueCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
public override Task ExecuteCommand(SharedCommandContext ctx, Dictionary<string, object> arguments)
|
||||
{
|
||||
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music;
|
||||
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
var selection = (string)arguments["video"];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(selection))
|
||||
{
|
||||
this.SendSyntaxError();
|
||||
return;
|
||||
}
|
||||
|
||||
if (await ctx.DbUser.Cooldown.WaitForLight(ctx))
|
||||
return;
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
GuildMusic.QueueInfo info = null;
|
||||
|
||||
if (selection.IsDigitsOnly())
|
||||
{
|
||||
var Index = Convert.ToInt32(selection) - 1;
|
||||
|
||||
if (Index < 0 || Index >= MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.RemoveQueue.OutOfRange, true, new TVar("Min", 1), new TVar("Max", MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Length)),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
info = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue[Index];
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Any(x => x.VideoTitle.ToLower() == selection.ToLower()))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.RemoveQueue.NoSong, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
info = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.First(x => x.VideoTitle.ToLower() == selection.ToLower());
|
||||
}
|
||||
|
||||
if (info is null)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.RemoveQueue.NoSong, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue = MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].SongQueue.Remove(x => x.UUID, info);
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.RemoveQueue.Removed, true, new TVar("Track", $"`[`{info.VideoTitle}`]({info.Url})`")),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
46
Commands/Music/RepeatCommand.cs
Normal file
46
Commands/Music/RepeatCommand.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// 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 RepeatCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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.WaitForLight(ctx))
|
||||
return;
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Repeat = !MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Repeat;
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Repeat ? this.GetString(CommandKey.Repeat.On, true) : this.GetString(CommandKey.Repeat.Off, true)),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
46
Commands/Music/ShuffleCommand.cs
Normal file
46
Commands/Music/ShuffleCommand.cs
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
// 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 ShuffleCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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.WaitForLight(ctx))
|
||||
return;
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Shuffle = !MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Shuffle;
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].Shuffle ? this.GetString(CommandKey.Shuffle.On, true) : this.GetString(CommandKey.Shuffle.Off, true)),
|
||||
}.AsSuccess(ctx));
|
||||
});
|
||||
}
|
||||
}
|
||||
125
Commands/Music/SkipCommand.cs
Normal file
125
Commands/Music/SkipCommand.cs
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
// 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 SkipCommand : BaseCommand
|
||||
{
|
||||
public override Task<bool> BeforeExecution(SharedCommandContext ctx) => this.CheckVoiceState();
|
||||
|
||||
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 lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
var conn = session.GetGuildPlayer(ctx.Member.VoiceState.Guild);
|
||||
|
||||
if (conn is null || conn.Channel.Id != ctx.Member.VoiceState.Channel.Id)
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.NotSameChannel, true),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Contains(ctx.User.Id))
|
||||
{
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Skip.AlreadyVoted),
|
||||
}.AsError(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Add(ctx.User.Id);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Count >= (conn.Channel.Users.Count - 1) * 0.51)
|
||||
{
|
||||
_ = await conn.StopAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(embed: new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Skip.Skipped, true),
|
||||
}.AsSuccess(ctx));
|
||||
return;
|
||||
}
|
||||
|
||||
var embed = new DiscordEmbedBuilder()
|
||||
{
|
||||
Description = $"`{this.GetGuildString(CommandKey.Skip.VoteStarted)} ({MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Count}/{Math.Ceiling((conn.Channel.Users.Count - 1.0) * 0.51)})`",
|
||||
}.AsAwaitingInput(ctx);
|
||||
|
||||
var builder = new DiscordMessageBuilder().WithEmbed(embed);
|
||||
|
||||
DiscordButtonComponent SkipSongVote = new(ButtonStyle.Danger, Guid.NewGuid().ToString(), this.GetGuildString(CommandKey.Skip.VoteButton), false, new DiscordComponentEmoji(DiscordEmoji.FromUnicode("⏩")));
|
||||
_ = builder.AddComponents(SkipSongVote);
|
||||
|
||||
_ = await this.RespondOrEdit(builder);
|
||||
|
||||
_ = Task.Delay(TimeSpan.FromMinutes(10)).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.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Contains(e.User.Id))
|
||||
{
|
||||
_ = e.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent($"❌ {this.GetString(CommandKey.Skip.AlreadyVoted, true)}").AsEphemeral());
|
||||
return;
|
||||
}
|
||||
|
||||
var member = await e.User.ConvertToMember(ctx.Guild);
|
||||
|
||||
if (member.VoiceState is null || member.VoiceState.Channel.Id != conn.Channel.Id)
|
||||
{
|
||||
_ = e.Interaction.CreateFollowupMessageAsync(new DiscordFollowupMessageBuilder().WithContent($"❌ {this.GetString(CommandKey.NotSameChannel, true)}").AsEphemeral());
|
||||
return;
|
||||
}
|
||||
|
||||
MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Add(e.User.Id);
|
||||
|
||||
if (MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Count >= (conn.Channel.Users.Count - 1) * 0.51)
|
||||
{
|
||||
_ = await conn.StopAsync();
|
||||
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(new DiscordEmbedBuilder
|
||||
{
|
||||
Description = this.GetString(CommandKey.Skip.Skipped, true),
|
||||
}.AsSuccess(ctx)));
|
||||
return;
|
||||
}
|
||||
|
||||
embed.Description = $"`{this.GetGuildString(CommandKey.Skip.VoteStarted)} ({MusicPlugin.Plugin!.Guilds![ctx.Guild.Id].collectedSkips.Count}/{Math.Ceiling((conn.Channel.Users.Count - 1.0) * 0.51)})`";
|
||||
_ = await this.RespondOrEdit(new DiscordMessageBuilder().WithEmbed(embed).AddComponents(SkipSongVote));
|
||||
}
|
||||
}).Add(ctx.Bot);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
192
Commands/MusicModuleAbstractions.cs
Normal file
192
Commands/MusicModuleAbstractions.cs
Normal file
|
|
@ -0,0 +1,192 @@
|
|||
// 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 Xorog.UniversalExtensions;
|
||||
using Xorog.UniversalExtensions.Enums;
|
||||
|
||||
namespace ProjectMakoto.Plugins.Music;
|
||||
|
||||
internal static class MusicModuleAbstractions
|
||||
{
|
||||
public static async Task<(List<LavalinkTrack> Tracks, LavalinkTrackLoadingResult oriResult, bool Continue)> GetLoadResult(SharedCommandContext ctx, string searchQuery)
|
||||
{
|
||||
var CommandKey = ((Entities.Translations)MusicPlugin.Plugin!.Translations).Commands.Music.Play;
|
||||
|
||||
if (Regex.IsMatch(searchQuery, "{jndi:(ldap[s]?|rmi):\\/\\/[^\n]+") || searchQuery.Contains("localhost", StringComparison.OrdinalIgnoreCase) || searchQuery.Contains("127.0.0.1", StringComparison.OrdinalIgnoreCase))
|
||||
throw new Exception();
|
||||
|
||||
List<LavalinkTrack> Tracks = new();
|
||||
|
||||
var lava = ctx.Client.GetLavalink();
|
||||
var session = lava.ConnectedSessions.Values.First(x => x.IsConnected);
|
||||
|
||||
var embed = new DiscordEmbedBuilder(ctx.ResponseMessage.Embeds[0]);
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed.WithDescription(CommandKey.LookingFor.Get(ctx.DbUser).Build(true, new TVar("Search", searchQuery))).AsLoading(ctx));
|
||||
|
||||
LavalinkTrackLoadingResult loadResult;
|
||||
|
||||
if (RegexTemplates.YouTubeUrl.IsMatch(searchQuery))
|
||||
{
|
||||
if (Regex.IsMatch(searchQuery, @"((\?|&)list=RDMM\w+)(&*)"))
|
||||
{
|
||||
Group group = Regex.Match(searchQuery, @"((\?|&)list=RDMM\w+)(&*)", RegexOptions.ExplicitCapture);
|
||||
var value = group.Value;
|
||||
|
||||
if (value.EndsWith('&'))
|
||||
value = value[..^1];
|
||||
|
||||
searchQuery = searchQuery.Replace(value, "");
|
||||
}
|
||||
|
||||
if (Regex.IsMatch(searchQuery, @"((\?|&)start_radio=\d+)(&*)"))
|
||||
{
|
||||
Group group = Regex.Match(searchQuery, @"((\?|&)start_radio=\d+)(&*)", RegexOptions.ExplicitCapture);
|
||||
var value = group.Value;
|
||||
|
||||
if (value.EndsWith('&'))
|
||||
value = value[..^1];
|
||||
|
||||
searchQuery = searchQuery.Replace(value, "");
|
||||
}
|
||||
|
||||
var AndIndex = searchQuery.IndexOf('&');
|
||||
|
||||
if (!searchQuery.Contains('?') && AndIndex != -1)
|
||||
{
|
||||
searchQuery = searchQuery.Remove(AndIndex, 1);
|
||||
searchQuery = searchQuery.Insert(AndIndex, "?");
|
||||
}
|
||||
|
||||
loadResult = await session.LoadTracksAsync(LavalinkSearchType.Plain, searchQuery);
|
||||
}
|
||||
else if (RegexTemplates.SoundcloudUrl.IsMatch(searchQuery))
|
||||
{
|
||||
loadResult = await session.LoadTracksAsync(LavalinkSearchType.Plain, searchQuery);
|
||||
}
|
||||
else if (RegexTemplates.BandcampUrl.IsMatch(searchQuery))
|
||||
{
|
||||
loadResult = await session.LoadTracksAsync(LavalinkSearchType.Plain, searchQuery);
|
||||
}
|
||||
else if (RegexTemplates.SpotifyUrl.IsMatch(searchQuery))
|
||||
{
|
||||
loadResult = await session.LoadTracksAsync(LavalinkSearchType.Plain, searchQuery);
|
||||
}
|
||||
else
|
||||
{
|
||||
embed.Description = CommandKey.PlatformSelect.Get(ctx.DbUser).Build(true);
|
||||
_ = embed.AsAwaitingInput(ctx);
|
||||
|
||||
var YouTube = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), "YouTube", false, EmojiTemplates.GetYouTube(ctx.Bot).ToComponent());
|
||||
var SoundCloud = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), "Soundcloud", false, EmojiTemplates.GetSoundcloud(ctx.Bot).ToComponent());
|
||||
var Spotify = new DiscordButtonComponent(ButtonStyle.Primary, Guid.NewGuid().ToString(), "Spotify", false, EmojiTemplates.GetSpotify(ctx.Bot).ToComponent());
|
||||
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(
|
||||
new DiscordMessageBuilder().WithEmbed(embed)
|
||||
.AddComponents(new List<DiscordComponent> { YouTube, SoundCloud, Spotify }));
|
||||
|
||||
var Menu1 = await ctx.WaitForButtonAsync(TimeSpan.FromMinutes(2));
|
||||
|
||||
if (Menu1.TimedOut)
|
||||
{
|
||||
ctx.BaseCommand.ModifyToTimedOut();
|
||||
return (null, null, false);
|
||||
}
|
||||
|
||||
_ = Menu1.Result.Interaction.CreateResponseAsync(InteractionResponseType.DeferredMessageUpdate);
|
||||
|
||||
var platformString = string.Empty;
|
||||
var searchType = LavalinkSearchType.Plain;
|
||||
|
||||
if (Menu1.GetCustomId() == YouTube.CustomId)
|
||||
{
|
||||
platformString = "YouTube";
|
||||
searchType = LavalinkSearchType.Youtube;
|
||||
}
|
||||
else if (Menu1.GetCustomId() == SoundCloud.CustomId)
|
||||
{
|
||||
platformString = "SoundCloud";
|
||||
searchType = LavalinkSearchType.SoundCloud;
|
||||
}
|
||||
else if (Menu1.GetCustomId() == Spotify.CustomId)
|
||||
{
|
||||
platformString = "Spotify";
|
||||
searchType = LavalinkSearchType.Spotify;
|
||||
}
|
||||
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed.WithDescription(CommandKey.LookingFor.Get(ctx.DbUser).Build(true,
|
||||
new TVar("Search", searchQuery),
|
||||
new TVar("Platform", platformString))).AsLoading(ctx));
|
||||
|
||||
loadResult = await session.LoadTracksAsync(searchType, searchQuery);
|
||||
}
|
||||
|
||||
if (loadResult.LoadType == LavalinkLoadResultType.Error)
|
||||
{
|
||||
MusicPlugin.Plugin._logger.LogError("An exception occurred while trying to load lavalink track.");
|
||||
embed.Description = CommandKey.FailedToLoad.Get(ctx.DbUser).Build(true,
|
||||
new TVar("Search", searchQuery));
|
||||
_ = embed.AsError(ctx);
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed.Build());
|
||||
return (null, loadResult, false);
|
||||
}
|
||||
else if (loadResult.LoadType == LavalinkLoadResultType.Empty)
|
||||
{
|
||||
embed.Description = CommandKey.NoMatches.Get(ctx.DbUser).Build(true,
|
||||
new TVar("Search", searchQuery));
|
||||
_ = embed.AsError(ctx);
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed.Build());
|
||||
return (null, loadResult, false);
|
||||
}
|
||||
else if (loadResult.LoadType == LavalinkLoadResultType.Playlist)
|
||||
{
|
||||
return (loadResult.GetResultAs<LavalinkPlaylist>().Tracks.ToList(), loadResult, true);
|
||||
}
|
||||
else if (loadResult.LoadType == LavalinkLoadResultType.Track)
|
||||
{
|
||||
Tracks.Add(loadResult.GetResultAs<LavalinkTrack>());
|
||||
return (Tracks, loadResult, true);
|
||||
}
|
||||
else if (loadResult.LoadType == LavalinkLoadResultType.Search)
|
||||
{
|
||||
var searchResults = loadResult.GetResultAs<List<LavalinkTrack>>();
|
||||
|
||||
embed.Description = CommandKey.SearchSuccess.Get(ctx.DbUser).Build(true,
|
||||
new TVar("Count", searchResults.Count));
|
||||
_ = embed.AsAwaitingInput(ctx);
|
||||
_ = await ctx.BaseCommand.RespondOrEdit(embed.Build());
|
||||
|
||||
var UriResult = await ctx.BaseCommand.PromptCustomSelection(searchResults
|
||||
.Select(x => new DiscordStringSelectComponentOption(x.Info.Title.TruncateWithIndication(100), x.Info.Uri.ToString(), $"🔼 {x.Info.Author} | 🕒 {x.Info.Length.GetHumanReadable(TimeFormat.Minutes)}")).ToList());
|
||||
|
||||
if (UriResult.TimedOut)
|
||||
{
|
||||
ctx.BaseCommand.ModifyToTimedOut();
|
||||
return (null, loadResult, false);
|
||||
}
|
||||
else if (UriResult.Cancelled)
|
||||
{
|
||||
return (null, loadResult, false);
|
||||
}
|
||||
else if (UriResult.Errored)
|
||||
{
|
||||
throw UriResult.Exception;
|
||||
}
|
||||
|
||||
Tracks.Add(searchResults.First(x => x.Info.Uri.ToString() == UriResult.Result));
|
||||
|
||||
return (Tracks, loadResult, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Unknown Load Result Type: {loadResult.LoadType}");
|
||||
}
|
||||
}
|
||||
}
|
||||
70
Commands/Playlists/AddToQueueCommand.cs
Normal file
70
Commands/Playlists/AddToQueueCommand.cs
Normal 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)));
|
||||
});
|
||||
}
|
||||
}
|
||||
51
Commands/Playlists/DeleteCommand.cs
Normal file
51
Commands/Playlists/DeleteCommand.cs
Normal 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
45
Commands/Playlists/ExportCommand.cs
Normal file
45
Commands/Playlists/ExportCommand.cs
Normal 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));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
224
Commands/Playlists/ImportCommand.cs
Normal file
224
Commands/Playlists/ImportCommand.cs
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
111
Commands/Playlists/LoadShareCommand.cs
Normal file
111
Commands/Playlists/LoadShareCommand.cs
Normal 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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
253
Commands/Playlists/ManageCommand.cs
Normal file
253
Commands/Playlists/ManageCommand.cs
Normal 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
413
Commands/Playlists/ModifyCommand.cs
Normal file
413
Commands/Playlists/ModifyCommand.cs
Normal 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;
|
||||
});
|
||||
}
|
||||
}
|
||||
180
Commands/Playlists/NewPlaylistCommand.cs
Normal file
180
Commands/Playlists/NewPlaylistCommand.cs
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
152
Commands/Playlists/SaveCurrentCommand.cs
Normal file
152
Commands/Playlists/SaveCurrentCommand.cs
Normal 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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
53
Commands/Playlists/ShareCommand.cs
Normal file
53
Commands/Playlists/ShareCommand.cs
Normal 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));
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue