From 4ba01ed72b5742d25fbd482c939a05a8783840a0 Mon Sep 17 00:00:00 2001 From: SocksOnHead Date: Tue, 13 Aug 2024 03:24:29 +0200 Subject: [PATCH] Small fix (#6) Co-authored-by: Myx --- Bot/Handler/MusicPlayer/MessageModule.cs | 20 +++++ .../MusicPlayer/PlayCommand/PlayHandler.cs | 87 +++++++++++-------- Bot/Handler/MusicPlayer/PlayCommand/readme.md | 2 +- README.md | 18 ++++ 4 files changed, 89 insertions(+), 38 deletions(-) diff --git a/Bot/Handler/MusicPlayer/MessageModule.cs b/Bot/Handler/MusicPlayer/MessageModule.cs index 832e579..5aa1e78 100644 --- a/Bot/Handler/MusicPlayer/MessageModule.cs +++ b/Bot/Handler/MusicPlayer/MessageModule.cs @@ -38,6 +38,26 @@ public static class MessageModule throw; } } + + public static async Task RemoveMessages(this SocketSlashCommand context, DiscordSocketClient client) + { + var guildId = context.GetGuild(client).Id; + + if (GuildMessageIds.TryGetValue(guildId, out var value)) + { + if (value.Count <= 0) + return; + + foreach (var messageId in value) + { + var messageToDelete = await context.Channel.GetMessageAsync(messageId); + if (messageToDelete != null) + await messageToDelete.DeleteAsync(); + } + + value.Clear(); + } + } private static async Task StoreForRemoval(SocketSlashCommand context, DiscordSocketClient client) { diff --git a/Bot/Handler/MusicPlayer/PlayCommand/PlayHandler.cs b/Bot/Handler/MusicPlayer/PlayCommand/PlayHandler.cs index 03d046a..3f48676 100644 --- a/Bot/Handler/MusicPlayer/PlayCommand/PlayHandler.cs +++ b/Bot/Handler/MusicPlayer/PlayCommand/PlayHandler.cs @@ -5,7 +5,6 @@ using Lavalink4NET; using Lavalink4NET.Events.Players; using Lavalink4NET.Players.Queued; using Lavalink4NET.Rest.Entities.Tracks; -using System.Threading; namespace Lunaris2.Handler.MusicPlayer.PlayCommand; @@ -17,6 +16,7 @@ public class PlayHandler : IRequestHandler private readonly DiscordSocketClient _client; private readonly IAudioService _audioService; private SocketSlashCommand _context; + private const int MaxTrackDuration = 30; public PlayHandler( DiscordSocketClient client, @@ -28,7 +28,7 @@ public class PlayHandler : IRequestHandler _audioService = audioService; _audioService.TrackStarted += OnTrackStarted; } - + private async Task OnTrackStarted(object sender, TrackStartedEventArgs eventargs) { var player = sender as QueuedLavalinkPlayer; @@ -45,54 +45,67 @@ public class PlayHandler : IRequestHandler async void PlayMusic() { - var context = command.Message; - _context = context; - - if ((context.User as SocketGuildUser)?.VoiceChannel == null) + try { - await context.SendMessageAsync("You must be in a voice channel to use this command.", _client); - return; - } - - await _audioService.StartAsync(cancellationToken); + await _audioService.StartAsync(cancellationToken); + + var context = command.Message; + _context = context; + + if ((context.User as SocketGuildUser)?.VoiceChannel == null) + { + await context.SendMessageAsync("You must be in a voice channel to use this command.", _client); + return; + } + + var searchQuery = context.GetOptionValueByName(Option.Input); - var searchQuery = context.GetOptionValueByName(Option.Input); + if (string.IsNullOrWhiteSpace(searchQuery)) + { + await context.SendMessageAsync("Please provide search terms.", _client); + return; + } + + await context.SendMessageAsync("📻 Searching...", _client); + + var player = await _audioService.GetPlayerAsync(_client, context, connectToVoiceChannel: true); - if (string.IsNullOrWhiteSpace(searchQuery)) - { - await context.SendMessageAsync("Please provide search terms.", _client); - return; - } - - var player = await _audioService.GetPlayerAsync(_client, context, connectToVoiceChannel: true); + if (player is null) + return; - if (player is null) return; + var trackLoadOptions = new TrackLoadOptions { SearchMode = TrackSearchMode.YouTube, }; - var trackLoadOptions = new TrackLoadOptions { SearchMode = TrackSearchMode.YouTube, }; + var track = await _audioService.Tracks.LoadTrackAsync(searchQuery, trackLoadOptions, cancellationToken: cancellationToken); - var track = await _audioService.Tracks.LoadTrackAsync(searchQuery, trackLoadOptions, cancellationToken: cancellationToken); + if (track is null) + { + await context.SendMessageAsync("😖 No results.", _client); + return; + } + + if (player.CurrentTrack?.Duration.TotalMinutes > MaxTrackDuration) + { + await context.SendMessageAsync($"🔈 Sorry the track is longer than { MaxTrackDuration } minutes, to save resources this limit is active for now.", _client); + return; + } - if (track is null) await context.SendMessageAsync("😖 No results.", _client); + if (player.CurrentTrack is null) + { + await player.PlayAsync(track, cancellationToken: cancellationToken) + .ConfigureAwait(false); - if (player.CurrentTrack is null) - { - await player.PlayAsync(track, cancellationToken: cancellationToken) - .ConfigureAwait(false); - - await _musicEmbed.NowPlayingEmbed(track, context, _client); - } - else - { - if (track != null) + await _musicEmbed.NowPlayingEmbed(track, context, _client); + } + else { var queueTracks = new[] { new TrackQueueItem(track) }; await player.Queue.AddRangeAsync(queueTracks, cancellationToken); await context.SendMessageAsync($"🔈 Added to queue: {track.Title}", _client); } - else - { - await context.SendMessageAsync($"Couldn't read song information", _client); - } + } + catch (Exception error) + { + throw new Exception("Error occured in the Play handler!", error); } } } diff --git a/Bot/Handler/MusicPlayer/PlayCommand/readme.md b/Bot/Handler/MusicPlayer/PlayCommand/readme.md index d72c8ea..f975d6c 100644 --- a/Bot/Handler/MusicPlayer/PlayCommand/readme.md +++ b/Bot/Handler/MusicPlayer/PlayCommand/readme.md @@ -32,4 +32,4 @@ There is also OnTrackEnd, when it get called an attempt is made to play the next | `player` | `LavaPlayer` | An instance of the `LavaPlayer` class, representing a music player connected to a specific voice channel. Used to play, pause, skip, and queue tracks. | | `guildMessageIds` | `Dictionary>` | A dictionary that maps guild IDs to lists of message IDs. Used to keep track of messages sent by the bot in each guild, allowing the bot to delete its old messages when it sends new ones. | | `songName` | `string` | A string that represents the name or URL of a song to play. Used to search for and queue tracks. | -| `searchResponse` | `SearchResponse` | An instance of the `SearchResponse` class, representing the result of a search for tracks. Used to get the tracks that were found and queue them in the player. | +| `searchResponse` | `SearchResponse` | An instance of the `SearchResponse` class, representing the result of a search for tracks. Used to get the tracks that were found and queue them in the player. | \ No newline at end of file diff --git a/README.md b/README.md index ca909cd..35a3197 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,24 @@ Lunaris supports AI chat using a large language model, this is done by hosting t The LLM is run using Ollama see more about Ollama [here](https://ollama.com/). Running LLM locally requires much resources from your system, minimum requirements is at least 8GB of ram. If your don't have enought ram, select a LLM model in the [appsettings file](https://github.com/Myxelium/Lunaris2.0/blob/master/Bot/appsettings.json#L15) that requires less of your system. +*NOTE: you need to download the model from the Ollama ui, the model name which is preselected in the code is called ``gemma``.* + +## PM2 Setup +- Install PM2 and configure it following their setup guide +#### Lavalink +* Download Lavalink 4.X.X (.jar) +* Install Java 17 + +If using Linux run following command to start Lavalink with PM2: +``pm2 start "sudo java -Xmx1G -jar Lavalink.jar" --name Lavalink4.0.7`` + +For me I have Lavalink.jar downloaded in ``/opt`` folder from Linux root. By running Lavalink using PM2, you can monitor it and manage it from a page in your browser instead of having to access the server terminal. +#### Lunaris +* Install dotnet + +Register the Lunaris bot with PM2: +``pm2 start "dotnet Lunaris2.dll"`` + ## Usage - `/play `: Plays the specified song in the voice channel you're currently in.