mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-11 07:09:39 +00:00
Add Embed
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
|
||||
namespace Lunaris2.Handler.GoodByeCommand
|
||||
{
|
||||
public record GoodbyeCommand(SocketSlashCommand Message) : IRequest;
|
||||
namespace Lunaris2.Handler.GoodByeCommand;
|
||||
|
||||
public class GoodbyeHandler : IRequestHandler<GoodbyeCommand>
|
||||
public record GoodbyeCommand(SocketSlashCommand Message) : IRequest;
|
||||
|
||||
public class GoodbyeHandler : IRequestHandler<GoodbyeCommand>
|
||||
{
|
||||
public async Task Handle(GoodbyeCommand message, CancellationToken cancellationToken)
|
||||
{
|
||||
public async Task Handle(GoodbyeCommand message, CancellationToken cancellationToken)
|
||||
{
|
||||
await message.Message.RespondAsync($"Goodbye, {message.Message.User.Username}! :c");
|
||||
}
|
||||
await message.Message.RespondAsync($"Goodbye, {message.Message.User.Username}! :c");
|
||||
}
|
||||
}
|
||||
@@ -3,17 +3,16 @@ using Lunaris2.SlashCommand;
|
||||
using MediatR;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Lunaris2.Handler.HelloCommand
|
||||
{
|
||||
public record HelloCommand(SocketSlashCommand Message) : IRequest;
|
||||
namespace Lunaris2.Handler.HelloCommand;
|
||||
|
||||
public class HelloHandler : IRequestHandler<HelloCommand>
|
||||
public record HelloCommand(SocketSlashCommand Message) : IRequest;
|
||||
|
||||
public class HelloHandler : IRequestHandler<HelloCommand>
|
||||
{
|
||||
public async Task Handle(HelloCommand message, CancellationToken cancellationToken)
|
||||
{
|
||||
public async Task Handle(HelloCommand message, CancellationToken cancellationToken)
|
||||
{
|
||||
Console.WriteLine(JsonConvert.SerializeObject(Command.GetAllCommands()));
|
||||
Console.WriteLine(JsonConvert.SerializeObject(Command.GetAllCommands()));
|
||||
|
||||
await message.Message.RespondAsync($"Hello, {message.Message.User.Username}!");
|
||||
}
|
||||
await message.Message.RespondAsync($"Hello, {message.Message.User.Username}!");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
using Discord.Commands;
|
||||
using Lunaris2.Handler.GoodByeCommand;
|
||||
using Lunaris2.Handler.MusicPlayer.JoinCommand;
|
||||
using Lunaris2.Handler.MusicPlayer.PlayCommand;
|
||||
using Lunaris2.Handler.MusicPlayer.SkipCommand;
|
||||
using Lunaris2.Notification;
|
||||
using Lunaris2.SlashCommand;
|
||||
using MediatR;
|
||||
@@ -26,7 +26,8 @@ public class MessageReceivedHandler(ISender mediator) : INotificationHandler<Mes
|
||||
case Command.Play.Name:
|
||||
await mediator.Send(new PlayCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
default:
|
||||
case Command.Skip.Name:
|
||||
await mediator.Send(new SkipCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,59 +2,58 @@ using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Victoria.Node;
|
||||
|
||||
namespace Lunaris2.Handler.MusicPlayer
|
||||
{
|
||||
public static class Extensions
|
||||
{
|
||||
public static SocketGuild GetGuild(this SocketSlashCommand message, DiscordSocketClient client)
|
||||
{
|
||||
if (message.GuildId == null)
|
||||
{
|
||||
throw new Exception("Guild ID is null!");
|
||||
}
|
||||
|
||||
return client.GetGuild(message.GuildId.Value);
|
||||
}
|
||||
|
||||
public static IVoiceState GetVoiceState(this SocketSlashCommand message)
|
||||
{
|
||||
var voiceState = message.User as IVoiceState;
|
||||
|
||||
if (voiceState?.VoiceChannel == null)
|
||||
{
|
||||
throw new Exception("You must be connected to a voice channel!");
|
||||
}
|
||||
namespace Lunaris2.Handler.MusicPlayer;
|
||||
|
||||
return voiceState;
|
||||
}
|
||||
|
||||
public static async Task RespondAsync(this SocketSlashCommand message, string content)
|
||||
public static class Extensions
|
||||
{
|
||||
public static SocketGuild GetGuild(this SocketSlashCommand message, DiscordSocketClient client)
|
||||
{
|
||||
if (message.GuildId == null)
|
||||
{
|
||||
await message.RespondAsync(content, ephemeral: true);
|
||||
throw new Exception("Guild ID is null!");
|
||||
}
|
||||
|
||||
return client.GetGuild(message.GuildId.Value);
|
||||
}
|
||||
|
||||
public static async Task EnsureConnected(this LavaNode lavaNode)
|
||||
public static IVoiceState GetVoiceState(this SocketSlashCommand message)
|
||||
{
|
||||
var voiceState = message.User as IVoiceState;
|
||||
|
||||
if (voiceState?.VoiceChannel == null)
|
||||
{
|
||||
if(!lavaNode.IsConnected)
|
||||
await lavaNode.ConnectAsync();
|
||||
throw new Exception("You must be connected to a voice channel!");
|
||||
}
|
||||
|
||||
return voiceState;
|
||||
}
|
||||
|
||||
public static async Task JoinVoiceChannel(this SocketSlashCommand context, LavaNode lavaNode)
|
||||
public static async Task RespondAsync(this SocketSlashCommand message, string content)
|
||||
{
|
||||
await message.RespondAsync(content, ephemeral: true);
|
||||
}
|
||||
|
||||
public static async Task EnsureConnected(this LavaNode lavaNode)
|
||||
{
|
||||
if(!lavaNode.IsConnected)
|
||||
await lavaNode.ConnectAsync();
|
||||
}
|
||||
|
||||
public static async Task JoinVoiceChannel(this SocketSlashCommand context, LavaNode lavaNode)
|
||||
{
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
var textChannel = context.Channel as ITextChannel;
|
||||
await lavaNode.JoinAsync(context.GetVoiceState().VoiceChannel, textChannel);
|
||||
await context.RespondAsync($"Joined {context.GetVoiceState().VoiceChannel.Name}!");
|
||||
}
|
||||
catch (Exception exception) {
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
var textChannel = context.Channel as ITextChannel;
|
||||
await lavaNode.JoinAsync(context.GetVoiceState().VoiceChannel, textChannel);
|
||||
await context.RespondAsync($"Joined {context.GetVoiceState().VoiceChannel.Name}!");
|
||||
}
|
||||
|
||||
public static string GetOptionValueByName(this SocketSlashCommand command, string optionName)
|
||||
{
|
||||
return command.Data.Options.FirstOrDefault(option => option.Name == optionName)?.Value.ToString() ?? string.Empty;
|
||||
catch (Exception exception) {
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
|
||||
public static string GetOptionValueByName(this SocketSlashCommand command, string optionName)
|
||||
{
|
||||
return command.Data.Options.FirstOrDefault(option => option.Name == optionName)?.Value.ToString() ?? string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
using Victoria.Node;
|
||||
|
||||
52
Bot/Handler/MusicPlayer/MessageModule.cs
Normal file
52
Bot/Handler/MusicPlayer/MessageModule.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Lunaris2.Handler.MusicPlayer;
|
||||
|
||||
public static class MessageModule
|
||||
{
|
||||
private static Dictionary<ulong, List<ulong>> guildMessageIds = new Dictionary<ulong, List<ulong>>();
|
||||
|
||||
public static async Task SendMessageAsync(this SocketSlashCommand context, string message, DiscordSocketClient client)
|
||||
{
|
||||
var guildId = await StoreForRemoval(context, client);
|
||||
|
||||
await context.RespondAsync(message);
|
||||
var sentMessage = await context.GetOriginalResponseAsync();
|
||||
|
||||
guildMessageIds[guildId].Add(sentMessage.Id);
|
||||
}
|
||||
|
||||
public static async Task SendMessageAsync(this SocketSlashCommand context, Embed message, DiscordSocketClient client)
|
||||
{
|
||||
var guildId = await StoreForRemoval(context, client);
|
||||
|
||||
await context.RespondAsync(embed: message);
|
||||
|
||||
var sentMessage = await context.GetOriginalResponseAsync();
|
||||
|
||||
guildMessageIds[guildId].Add(sentMessage.Id);
|
||||
}
|
||||
|
||||
private static async Task<ulong> StoreForRemoval(SocketSlashCommand context, DiscordSocketClient client)
|
||||
{
|
||||
var guildId = context.GetGuild(client).Id;
|
||||
|
||||
if (guildMessageIds.ContainsKey(guildId))
|
||||
{
|
||||
foreach (var messageId in guildMessageIds[guildId])
|
||||
{
|
||||
var messageToDelete = await context.Channel.GetMessageAsync(messageId);
|
||||
if (messageToDelete != null)
|
||||
await messageToDelete.DeleteAsync();
|
||||
}
|
||||
|
||||
guildMessageIds[guildId].Clear();
|
||||
}
|
||||
else
|
||||
{
|
||||
guildMessageIds.Add(guildId, []);
|
||||
}
|
||||
|
||||
return guildId;
|
||||
}
|
||||
}
|
||||
44
Bot/Handler/MusicPlayer/MusicEmbed.cs
Normal file
44
Bot/Handler/MusicPlayer/MusicEmbed.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Discord;
|
||||
using Discord.WebSocket;
|
||||
using Victoria;
|
||||
using Victoria.Player;
|
||||
|
||||
namespace Lunaris2.Handler.MusicPlayer;
|
||||
|
||||
public class MusicEmbed
|
||||
{
|
||||
private Embed SendMusicEmbed(
|
||||
string imageUrl,
|
||||
string title,
|
||||
string length,
|
||||
string artist,
|
||||
string queuedBy,
|
||||
string? nextInQueue)
|
||||
{
|
||||
return new EmbedBuilder()
|
||||
.WithAuthor("Lunaris", "https://media.tenor.com/GqAwMt01UXgAAAAi/cd.gif")
|
||||
.WithTitle(title)
|
||||
.WithDescription($"Length: {length}\nArtist: {artist}\nQueued by: {queuedBy}\nNext in queue: {nextInQueue}")
|
||||
.WithColor(Color.Magenta)
|
||||
.WithThumbnailUrl(imageUrl)
|
||||
.Build();
|
||||
}
|
||||
|
||||
public async Task NowPlayingEmbed(
|
||||
LavaPlayer<LavaTrack> player,
|
||||
SocketSlashCommand context,
|
||||
DiscordSocketClient client)
|
||||
{
|
||||
var artwork = await player.Track.FetchArtworkAsync();
|
||||
var getNextTrack = player.Vueue.Count > 1 ? player.Vueue.ToArray()[1].Title : "No songs in queue.";
|
||||
var embed = SendMusicEmbed(
|
||||
artwork,
|
||||
player.Track.Title,
|
||||
player.Track.Duration.ToString(),
|
||||
player.Track.Author,
|
||||
context.User.Username,
|
||||
getNextTrack);
|
||||
|
||||
await context.SendMessageAsync(embed, client);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Lunaris2.SlashCommand;
|
||||
using MediatR;
|
||||
@@ -13,29 +14,36 @@ public record PlayCommand(SocketSlashCommand Message) : IRequest;
|
||||
|
||||
public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
{
|
||||
private readonly MusicEmbed _musicEmbed;
|
||||
private readonly LavaNode _lavaNode;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private SocketSlashCommand _context;
|
||||
|
||||
public PlayHandler(LavaNode lavaNode, DiscordSocketClient client)
|
||||
public PlayHandler(
|
||||
LavaNode lavaNode,
|
||||
DiscordSocketClient client,
|
||||
MusicEmbed musicEmbed)
|
||||
{
|
||||
_lavaNode = lavaNode;
|
||||
_client = client;
|
||||
_musicEmbed = musicEmbed;
|
||||
}
|
||||
|
||||
[Command(RunMode = RunMode.Async)]
|
||||
public async Task Handle(PlayCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var context = command.Message;
|
||||
_context = command.Message;
|
||||
|
||||
await _lavaNode.EnsureConnected();
|
||||
|
||||
var songName = context.GetOptionValueByName(Option.Input);
|
||||
var songName = _context.GetOptionValueByName(Option.Input);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(songName)) {
|
||||
await context.RespondAsync("Please provide search terms.");
|
||||
await _context.RespondAsync("Please provide search terms.");
|
||||
return;
|
||||
}
|
||||
|
||||
var player = await GetPlayer(context);
|
||||
var player = await GetPlayer();
|
||||
|
||||
if (player == null)
|
||||
return;
|
||||
@@ -45,10 +53,12 @@ public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
? SearchType.Direct
|
||||
: SearchType.YouTube, songName);
|
||||
|
||||
if (!await HandleSearchResponse(searchResponse, player, context, songName))
|
||||
if (!await SearchResponse(searchResponse, player, songName))
|
||||
return;
|
||||
|
||||
await PlayTrack(player);
|
||||
|
||||
await _musicEmbed.NowPlayingEmbed(player, _context, _client);
|
||||
|
||||
_lavaNode.OnTrackEnd += OnTrackEnd;
|
||||
}
|
||||
@@ -57,15 +67,14 @@ public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
{
|
||||
var player = arg.Player;
|
||||
if (!player.Vueue.TryDequeue(out var nextTrack))
|
||||
{
|
||||
await player.TextChannel.SendMessageAsync("Queue completed!");
|
||||
return;
|
||||
}
|
||||
|
||||
await player.PlayAsync(nextTrack);
|
||||
|
||||
await _musicEmbed.NowPlayingEmbed(player, _context, _client);
|
||||
}
|
||||
|
||||
private async Task PlayTrack(LavaPlayer<LavaTrack> player)
|
||||
private static async Task PlayTrack(LavaPlayer<LavaTrack> player)
|
||||
{
|
||||
if (player.PlayerState is PlayerState.Playing or PlayerState.Paused) {
|
||||
return;
|
||||
@@ -75,34 +84,34 @@ public class PlayHandler : IRequestHandler<PlayCommand>
|
||||
await player.PlayAsync(lavaTrack);
|
||||
}
|
||||
|
||||
private async Task<LavaPlayer<LavaTrack>?> GetPlayer(SocketSlashCommand context)
|
||||
private async Task<LavaPlayer<LavaTrack>?> GetPlayer()
|
||||
{
|
||||
var voiceState = context.User as IVoiceState;
|
||||
var voiceState = _context.User as IVoiceState;
|
||||
|
||||
if (voiceState?.VoiceChannel != null)
|
||||
return await _lavaNode.JoinAsync(voiceState.VoiceChannel, context.Channel as ITextChannel);
|
||||
return await _lavaNode.JoinAsync(voiceState.VoiceChannel, _context.Channel as ITextChannel);
|
||||
|
||||
await context.RespondAsync("You must be connected to a voice channel!");
|
||||
await _context.RespondAsync("You must be connected to a voice channel!");
|
||||
return null;
|
||||
}
|
||||
|
||||
private async Task<bool> HandleSearchResponse(SearchResponse searchResponse, LavaPlayer<LavaTrack> player, SocketSlashCommand context, string songName)
|
||||
private async Task<bool> SearchResponse(
|
||||
SearchResponse searchResponse, LavaPlayer<LavaTrack> player,
|
||||
string songName)
|
||||
{
|
||||
if (searchResponse.Status is SearchStatus.LoadFailed or SearchStatus.NoMatches) {
|
||||
await context.RespondAsync($"I wasn't able to find anything for `{songName}`.");
|
||||
await _context.RespondAsync($"I wasn't able to find anything for `{songName}`.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(searchResponse.Playlist.Name)) {
|
||||
player.Vueue.Enqueue(searchResponse.Tracks);
|
||||
|
||||
await context.RespondAsync($"Enqueued {searchResponse.Tracks.Count} songs.");
|
||||
await _context.RespondAsync($"Enqueued {searchResponse.Tracks.Count} songs.");
|
||||
}
|
||||
else {
|
||||
var track = searchResponse.Tracks.FirstOrDefault()!;
|
||||
player.Vueue.Enqueue(track);
|
||||
|
||||
await context.RespondAsync($"Enqueued {track?.Title}");
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
48
Bot/Handler/MusicPlayer/SkipCommand/SkipHandler.cs
Normal file
48
Bot/Handler/MusicPlayer/SkipCommand/SkipHandler.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
using Victoria.Node;
|
||||
using Victoria.Player;
|
||||
|
||||
namespace Lunaris2.Handler.MusicPlayer.SkipCommand;
|
||||
|
||||
public record SkipCommand(SocketSlashCommand Message) : IRequest;
|
||||
|
||||
public class SkipHandler : IRequestHandler<SkipCommand>
|
||||
{
|
||||
private readonly LavaNode _lavaNode;
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly MusicEmbed _musicEmbed;
|
||||
|
||||
public SkipHandler(LavaNode lavaNode, DiscordSocketClient client, MusicEmbed musicEmbed)
|
||||
{
|
||||
_lavaNode = lavaNode;
|
||||
_client = client;
|
||||
_musicEmbed = musicEmbed;
|
||||
}
|
||||
|
||||
public async Task Handle(SkipCommand message, CancellationToken cancellationToken)
|
||||
{
|
||||
var context = message.Message;
|
||||
|
||||
await _lavaNode.EnsureConnected();
|
||||
|
||||
if (!_lavaNode.TryGetPlayer(context.GetGuild(_client), out var player)) {
|
||||
await context.RespondAsync("I'm not connected to a voice channel.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (player.PlayerState != PlayerState.Playing) {
|
||||
await context.RespondAsync("Woaaah there, I can't skip when nothing is playing.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await player.SkipAsync();
|
||||
await _musicEmbed.NowPlayingEmbed(player, context, _client);
|
||||
}
|
||||
catch (Exception exception) {
|
||||
await context.RespondAsync("There is not more tracks to skip.");
|
||||
Console.WriteLine(exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user