Compare commits

..

1 Commits

Author SHA1 Message Date
Myx
cbef37c575 Fix bug with playback & add statuses 2024-10-25 21:06:00 +02:00
3 changed files with 8 additions and 236 deletions

View File

@@ -8,75 +8,6 @@ flowchart TD
PlayTrack --> NowPlayingEmbed
```
```mermaid
classDiagram
class PlayHandler {
-MusicEmbed _musicEmbed
-DiscordSocketClient _client
-IAudioService _audioService
-SocketSlashCommand _context
-const int MaxTrackDuration
-LavalinkTrack? _previousTrack
-static HashSet~ulong~ SubscribedGuilds
+PlayHandler(DiscordSocketClient client, MusicEmbed musicEmbed, IAudioService audioService)
+Task Handle(PlayCommand command, CancellationToken cancellationToken)
-void PlayMusic()
-Task OnTrackEnded(object sender, TrackEndedEventArgs eventargs)
-Task OnTrackStarted(object sender, TrackStartedEventArgs eventargs)
-void RegisterTrackStartedEventListerner(PlayCommand command)
-static Task ApplyFilters(CancellationToken cancellationToken, QueuedLavalinkPlayer player)
-static Task ConfigureSponsorBlock(CancellationToken cancellationToken, QueuedLavalinkPlayer player)
}
class PlayCommand {
+SocketSlashCommand Message
}
class TrackEndedEventArgs {
}
class TrackStartedEventArgs {
}
class QueuedLavalinkPlayer {
+LavalinkTrack? CurrentTrack
+Task PlayAsync(LavalinkTrack track, CancellationToken cancellationToken)
+Task Queue.AddRangeAsync(List~TrackQueueItem~ queueTracks, CancellationToken cancellationToken)
+Task Filters.SetFilter(NormalizationFilter normalizationFilter)
+Task Filters.CommitAsync(CancellationToken cancellationToken)
+Task UpdateSponsorBlockCategoriesAsync(ImmutableArray~SegmentCategory~ categories, CancellationToken cancellationToken)
}
class LavalinkTrack {
+string Identifier
}
class NormalizationFilter {
+NormalizationFilter(double gain, bool enabled)
}
class SegmentCategory {
+static SegmentCategory Intro
+static SegmentCategory Sponsor
+static SegmentCategory SelfPromotion
+static SegmentCategory Outro
+static SegmentCategory Filler
}
class TrackQueueItem {
+TrackQueueItem(LavalinkTrack track)
}
PlayHandler --> PlayCommand
PlayHandler --> TrackEndedEventArgs
PlayHandler --> TrackStartedEventArgs
PlayHandler --> QueuedLavalinkPlayer
PlayHandler --> LavalinkTrack
PlayHandler --> NormalizationFilter
PlayHandler --> SegmentCategory
PlayHandler --> TrackQueueItem
```
## Steps in the code
| Name | Description |
@@ -101,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<ulong, List<ulong>>` | 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. |

View File

@@ -1,139 +0,0 @@
### README.md
# Handlers
Handlers for the Lunaris2 bot, which is built using C#, Discord.Net, and Lavalink4NET. Below is a detailed description of each handler and their responsibilities.
## Handlers
### ClearQueueHandler
Handles the command to clear the music queue.
```csharp
public class ClearQueueHandler : IRequestHandler<ClearQueueCommand>
```
### DisconnectHandler
Handles the command to disconnect the bot from the voice channel.
```csharp
public class DisconnectHandler : IRequestHandler<DisconnectCommand>
```
### PauseHandler
Handles the command to pause the currently playing track.
```csharp
public class PauseHandler : IRequestHandler<PauseCommand>
```
### PlayHandler
Handles the command to play a track or playlist.
```csharp
public class PlayHandler : IRequestHandler<PlayCommand>
```
### ResumeHandler
Handles the command to resume the currently paused track.
```csharp
public class ResumeHandler : IRequestHandler<ResumeCommand>
```
### SkipHandler
Handles the command to skip the currently playing track.
```csharp
public class SkipHandler : IRequestHandler<SkipCommand>
```
### MessageReceivedHandler
Handles incoming messages and processes commands or statistics requests.
```csharp
public class MessageReceivedHandler : INotificationHandler<MessageReceivedNotification>
```
## Mermaid Diagrams
### Class Diagram
```mermaid
sequenceDiagram
participant User as User
participant DiscordSocketClient as DiscordSocketClient
participant MessageReceivedHandler as MessageReceivedHandler
participant MessageReceivedNotification as MessageReceivedNotification
participant EmbedBuilder as EmbedBuilder
participant Channel as Channel
User->>DiscordSocketClient: Send message "!LunarisStats"
DiscordSocketClient->>MessageReceivedHandler: MessageReceivedNotification
MessageReceivedHandler->>MessageReceivedNotification: Handle(notification, cancellationToken)
MessageReceivedNotification->>MessageReceivedHandler: BotMentioned(notification, cancellationToken)
MessageReceivedHandler->>DiscordSocketClient: Get guilds and voice channels
DiscordSocketClient-->>MessageReceivedHandler: List of guilds and voice channels
MessageReceivedHandler->>EmbedBuilder: Create embed with statistics
EmbedBuilder-->>MessageReceivedHandler: Embed
MessageReceivedHandler->>Channel: Send embed message
```
### Sequence Diagram for PlayHandler
```mermaid
sequenceDiagram
participant User
participant Bot
participant DiscordSocketClient
participant IAudioService
participant SocketSlashCommand
participant LavalinkPlayer
User->>Bot: /play [song]
Bot->>DiscordSocketClient: Get user voice channel
DiscordSocketClient-->>Bot: Voice channel info
Bot->>IAudioService: Get or create player
IAudioService-->>Bot: Player instance
Bot->>SocketSlashCommand: Get search query
SocketSlashCommand-->>Bot: Search query
Bot->>IAudioService: Load tracks
IAudioService-->>Bot: Track collection
Bot->>LavalinkPlayer: Play track
LavalinkPlayer-->>Bot: Track started
Bot->>User: Now playing embed
```
### Sequence Diagram for MessageReceivedHandler
```mermaid
sequenceDiagram
participant User
participant Bot
participant DiscordSocketClient
participant ISender
participant MessageReceivedNotification
User->>Bot: Send message
Bot->>MessageReceivedNotification: Create notification
Bot->>DiscordSocketClient: Check if bot is mentioned
DiscordSocketClient-->>Bot: Mention info
alt Bot is mentioned
Bot->>ISender: Send ChatCommand
end
Bot->>DiscordSocketClient: Check for statistics command
alt Statistics command found
Bot->>DiscordSocketClient: Get server and channel info
DiscordSocketClient-->>Bot: Server and channel info
Bot->>User: Send statistics embed
end
```
This README provides an overview of the handlers and their responsibilities, along with class and sequence diagrams to illustrate the interactions and relationships between the components.

View File

@@ -2,15 +2,11 @@
```mermaid
flowchart TD
Program[Program] -->|Register| EventListener
Program --> Intervals[VoiceChannelMonitorService]
Intervals --> SetStatus[SetStatus, Updates status with amount of playing bots]
Intervals --> LeaveChannel[LeaveOnAlone, Leaves channel when alone for a time]
EventListener[DiscordEventListener] --> A[MessageReceivedHandler]
EventListener[DiscordEventListener] --> A2[SlashCommandReceivedHandler]
A --> |Message| f{If bot is mentioned}
A --> |Message '!LunarisStats'| p[Responds with Server and Channel Statistics.]
f --> |ChatCommand| v[ChatHandler]
A2[SlashCommandReceivedHandler] -->|Message| C{Send to correct command by
@@ -18,11 +14,8 @@ flowchart TD
C -->|JoinCommand| D[JoinHandler]
C -->|PlayCommand| E[PlayHandler]
C -->|PauseCommand| F[PauseHandler]
C -->|DisconnectCommand| H[DisconnectHandler]
C -->|ResumeCommand| J[ResumeHandler]
C -->|SkipCommand| K[SkipHandler]
C -->|ClearQueueCommand| L[ClearQueueHandler]
C -->|HelloCommand| F[HelloHandler]
C -->|GoodbyeCommand| G[GoodbyeHandler]
```
Program registers an event listener ```DiscordEventListener``` which publish a message :
@@ -37,33 +30,20 @@ await Mediator.Publish(new MessageReceivedNotification(arg), _cancellationToken)
## Handler integrations
```mermaid
flowchart LR
flowchart TD
D[JoinHandler] --> Disc[Discord Api]
E[PlayHandler] --> Disc[Discord Api]
F[SkipHandler] --> Disc[Discord Api]
G[PauseHandler] --> Disc[Discord Api]
F[HelloHandler] --> Disc[Discord Api]
G[GoodbyeHandler] --> Disc[Discord Api]
v[ChatHandler] --> Disc[Discord Api]
ClearQueueHandler --> Disc
ClearQueuehandler --> Lava
DisconnectHandler --> Disc
Resumehandler --> Disc
v --> o[Ollama Server]
o --> v
E --> Lava[Lavalink]
F --> Lava
G --> Lava
```
|Name| Description |
|--|--|
| JoinHandler| Handles the logic for **just** joining a voice channel. |
| PlayHandler| Handles the logic for joining and playing music in a voice channel. |
| PauseHandler | Handles the logic for pausing currently playing track. |
| DisconnectHandler | Handles the logic for disconnecting from voicechannels. |
| ClearQueueHandler | Handles the logic for clearing the queued songs, except the currently playing one. |
| SkipHandler | Handles the logic for skipping tracks that are queued. If 0 trackS is in queue, it stops the current one.|
| Resumehandler | Resumes paused tracks. |
| HelloHandler| Responds with Hello. (Dummy handler, will be removed)|
| GoodbyeHandler| Responds with Goodbye. (Dummy handler, will be removed)|
| ChatHandler| Handles the logic for LLM chat with user. |