mirror of
https://github.com/Myxelium/Lunaris2.0.git
synced 2026-04-13 08:00:37 +00:00
Compare commits
13 Commits
default
...
1717277891
| Author | SHA1 | Date | |
|---|---|---|---|
| 54c5c68ba6 | |||
| e16ff9cfaf | |||
| a1d20fd732 | |||
| 3d7655a902 | |||
| 8ddcf31da7 | |||
| 80a7c19b20 | |||
| 713715901b | |||
| 1a3a00f4ed | |||
|
|
0673653491 | ||
| a650fd431e | |||
| f6a763589e | |||
| b7198bd01e | |||
| f0678a4244 |
14
.github/workflows/dotnet.yml
vendored
14
.github/workflows/dotnet.yml
vendored
@@ -26,17 +26,25 @@ jobs:
|
||||
- name: Publish
|
||||
run: dotnet publish ./Bot/Lunaris2.csproj --configuration Release --output ./out
|
||||
|
||||
- name: Zip the build
|
||||
run: 7z a -tzip ./out/Bot.zip ./out/*
|
||||
|
||||
- name: Get the tag name
|
||||
id: get_tag
|
||||
run: echo "::set-output name=tag::${GITHUB_REF#refs/tags/}"
|
||||
|
||||
- name: Get the version
|
||||
id: get_version
|
||||
run: echo "::set-output name=version::$(date +%s).${{ github.run_id }}"
|
||||
|
||||
- name: Create Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
|
||||
with:
|
||||
tag_name: ${{ steps.get_tag.outputs.tag || 'default' }}
|
||||
release_name: Release ${{ github.ref }}
|
||||
tag_name: ${{ steps.get_version.outputs.version }}
|
||||
release_name: Release v${{ steps.get_version.outputs.version }}
|
||||
draft: false
|
||||
prerelease: false
|
||||
|
||||
@@ -46,7 +54,7 @@ jobs:
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
|
||||
upload_url: ${{ steps.create_release.outputs.upload_url }}
|
||||
asset_path: ./out/Bot.zip
|
||||
asset_name: Bot.zip
|
||||
asset_content_type: application/zip
|
||||
|
||||
62
Bot/Handler/ChatCommand/ChatHandler.cs
Normal file
62
Bot/Handler/ChatCommand/ChatHandler.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using System.Text;
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
using Microsoft.Extensions.Options;
|
||||
using OllamaSharp;
|
||||
|
||||
namespace Lunaris2.Handler.ChatCommand
|
||||
{
|
||||
public record ChatCommand(SocketMessage Message, string FilteredMessage) : IRequest;
|
||||
|
||||
public class ChatHandler : IRequestHandler<ChatCommand>
|
||||
{
|
||||
private readonly OllamaApiClient _ollama;
|
||||
private readonly Dictionary<ulong, Chat?> _chatContexts = new();
|
||||
|
||||
public ChatHandler(IOptions<ChatSettings> chatSettings)
|
||||
{
|
||||
var uri = new Uri(chatSettings.Value.Url);
|
||||
|
||||
_ollama = new OllamaApiClient(uri)
|
||||
{
|
||||
SelectedModel = chatSettings.Value.Model
|
||||
};
|
||||
}
|
||||
|
||||
public async Task Handle(ChatCommand command, CancellationToken cancellationToken)
|
||||
{
|
||||
var channelId = command.Message.Channel.Id;
|
||||
_chatContexts.TryAdd(channelId, null);
|
||||
|
||||
var userMessage = command.FilteredMessage;
|
||||
|
||||
using var setTyping = command.Message.Channel.EnterTypingState();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(userMessage))
|
||||
{
|
||||
await command.Message.Channel.SendMessageAsync("Am I expected to read your mind?");
|
||||
setTyping.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
var response = await GenerateResponse(userMessage, channelId, cancellationToken);
|
||||
await command.Message.Channel.SendMessageAsync(response);
|
||||
|
||||
setTyping.Dispose();
|
||||
}
|
||||
|
||||
private async Task<string> GenerateResponse(string userMessage, ulong channelId, CancellationToken cancellationToken)
|
||||
{
|
||||
var response = new StringBuilder();
|
||||
|
||||
if (_chatContexts[channelId] == null)
|
||||
{
|
||||
_chatContexts[channelId] = _ollama.Chat(stream => response.Append(stream.Message?.Content ?? ""));
|
||||
}
|
||||
|
||||
await _chatContexts[channelId].Send(userMessage, cancellationToken);
|
||||
|
||||
return response.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
Bot/Handler/ChatCommand/ChatSettings.cs
Normal file
7
Bot/Handler/ChatCommand/ChatSettings.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Lunaris2.Handler.ChatCommand;
|
||||
|
||||
public class ChatSettings
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public string Model { get; set; }
|
||||
}
|
||||
@@ -1,34 +1,37 @@
|
||||
using Lunaris2.Handler.GoodByeCommand;
|
||||
using Lunaris2.Handler.MusicPlayer.JoinCommand;
|
||||
using Lunaris2.Handler.MusicPlayer.PlayCommand;
|
||||
using Lunaris2.Handler.MusicPlayer.SkipCommand;
|
||||
using System.Text.RegularExpressions;
|
||||
using Discord.WebSocket;
|
||||
using Lunaris2.Notification;
|
||||
using Lunaris2.SlashCommand;
|
||||
using MediatR;
|
||||
|
||||
namespace Lunaris2.Handler;
|
||||
|
||||
public class MessageReceivedHandler(ISender mediator) : INotificationHandler<MessageReceivedNotification>
|
||||
public class MessageReceivedHandler : INotificationHandler<MessageReceivedNotification>
|
||||
{
|
||||
private readonly DiscordSocketClient _client;
|
||||
private readonly ISender _mediatir;
|
||||
|
||||
public MessageReceivedHandler(DiscordSocketClient client, ISender mediatir)
|
||||
{
|
||||
_client = client;
|
||||
_mediatir = mediatir;
|
||||
}
|
||||
|
||||
public async Task Handle(MessageReceivedNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
switch (notification.Message.CommandName)
|
||||
await BotMentioned(notification, cancellationToken);
|
||||
}
|
||||
|
||||
private async Task BotMentioned(MessageReceivedNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
if (notification.Message.MentionedUsers.Any(user => user.Id == _client.CurrentUser.Id))
|
||||
{
|
||||
case Command.Hello.Name:
|
||||
await mediator.Send(new HelloCommand.HelloCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Goodbye.Name:
|
||||
await mediator.Send(new GoodbyeCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Join.Name:
|
||||
await mediator.Send(new JoinCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Play.Name:
|
||||
await mediator.Send(new PlayCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Skip.Name:
|
||||
await mediator.Send(new SkipCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
// The bot was mentioned
|
||||
const string pattern = "<.*?>";
|
||||
const string replacement = "";
|
||||
var regex = new Regex(pattern);
|
||||
var messageContent = regex.Replace(notification.Message.Content, replacement);
|
||||
|
||||
await _mediatir.Send(new ChatCommand.ChatCommand(notification.Message, messageContent), cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Bot/Handler/MusicPlayer/PlayCommand/readme.md
Normal file
35
Bot/Handler/MusicPlayer/PlayCommand/readme.md
Normal file
@@ -0,0 +1,35 @@
|
||||
```mermaid
|
||||
flowchart TD
|
||||
PlayHandler --> EnsureConnected
|
||||
EnsureConnected --> GetPlayer
|
||||
GetPlayer --> SearchAsync
|
||||
SearchAsync --> SearchResponse
|
||||
SearchResponse --> PlayTrack
|
||||
PlayTrack --> NowPlayingEmbed
|
||||
```
|
||||
|
||||
## Steps in the code
|
||||
|
||||
| Name | Description |
|
||||
|--|--|
|
||||
| PlayHandler | Holds the logic for playing songs |
|
||||
| GetPlayer | Joins voice channel, produces chat resposne |
|
||||
| EnsureConnected | Makes sure the client is connected |
|
||||
| SearchAsync | Searches for songs information |
|
||||
| SearchResponse | Handling possible errors from the response of SearchAsync |
|
||||
| PlayTrack | Plays the song |
|
||||
|
||||
There is also OnTrackEnd, when it get called an attempt is made to play the next song in queue.
|
||||
|
||||
## Short explaination for some of the variables used:
|
||||
|
||||
| Variable | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `_lavaNode` | `LavaNode` | An instance of the `LavaNode` class, used to interact with the LavaLink server for playing music in Discord voice channels. |
|
||||
| `_client` | `DiscordSocketClient` | An instance of the `DiscordSocketClient` class, used to interact with the Discord API for sending messages, joining voice channels, etc. |
|
||||
| `_musicEmbed` | `MusicEmbed` | An instance of a custom `MusicEmbed` class, used to create and send embed messages related to the music player's current status. |
|
||||
| `context` | `SocketSlashCommand` | An instance of the `SocketSlashCommand` class, representing a slash command received from Discord. Used to get information about the command and to respond to it. |
|
||||
| `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. |
|
||||
34
Bot/Handler/SlashCommandReceivedHandler.cs
Normal file
34
Bot/Handler/SlashCommandReceivedHandler.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
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;
|
||||
|
||||
namespace Lunaris2.Handler;
|
||||
|
||||
public class SlashCommandReceivedHandler(ISender mediator) : INotificationHandler<SlashCommandReceivedNotification>
|
||||
{
|
||||
public async Task Handle(SlashCommandReceivedNotification notification, CancellationToken cancellationToken)
|
||||
{
|
||||
switch (notification.Message.CommandName)
|
||||
{
|
||||
case Command.Hello.Name:
|
||||
await mediator.Send(new HelloCommand.HelloCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Goodbye.Name:
|
||||
await mediator.Send(new GoodbyeCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Join.Name:
|
||||
await mediator.Send(new JoinCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Play.Name:
|
||||
await mediator.Send(new PlayCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
case Command.Skip.Name:
|
||||
await mediator.Send(new SkipCommand(notification.Message), cancellationToken);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="OllamaSharp" Version="1.1.10" />
|
||||
<PackageReference Include="Victoria" Version="6.0.23.324" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -19,13 +19,19 @@ public class DiscordEventListener(DiscordSocketClient client, IServiceScopeFacto
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
client.SlashCommandExecuted += OnMessageReceivedAsync;
|
||||
client.SlashCommandExecuted += OnSlashCommandRecievedAsync;
|
||||
client.MessageReceived += OnMessageReceivedAsync;
|
||||
|
||||
await Task.CompletedTask;
|
||||
}
|
||||
|
||||
private async Task OnMessageReceivedAsync(SocketSlashCommand arg)
|
||||
private async Task OnMessageReceivedAsync(SocketMessage arg)
|
||||
{
|
||||
await Mediator.Publish(new MessageReceivedNotification(arg), _cancellationToken);
|
||||
}
|
||||
|
||||
private async Task OnSlashCommandRecievedAsync(SocketSlashCommand arg)
|
||||
{
|
||||
await Mediator.Publish(new SlashCommandReceivedNotification(arg), _cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using MediatR;
|
||||
|
||||
namespace Lunaris2.Notification;
|
||||
|
||||
public class MessageReceivedNotification(SocketSlashCommand message) : INotification
|
||||
public class MessageReceivedNotification(SocketMessage message) : INotification
|
||||
{
|
||||
public SocketSlashCommand Message { get; } = message ?? throw new ArgumentNullException(nameof(message));
|
||||
public SocketMessage Message { get; } = message ?? throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
9
Bot/Notification/SlashCommandReceivedNotification.cs
Normal file
9
Bot/Notification/SlashCommandReceivedNotification.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using Discord.WebSocket;
|
||||
using MediatR;
|
||||
|
||||
namespace Lunaris2.Notification;
|
||||
|
||||
public class SlashCommandReceivedNotification(SocketSlashCommand message) : INotification
|
||||
{
|
||||
public SocketSlashCommand Message { get; } = message ?? throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.Interactions;
|
||||
using Discord.WebSocket;
|
||||
using Lunaris2.Handler.ChatCommand;
|
||||
using Lunaris2.Handler.MusicPlayer;
|
||||
using Lunaris2.Notification;
|
||||
using Lunaris2.SlashCommand;
|
||||
@@ -54,7 +55,9 @@ public class Program
|
||||
nodeConfiguration.Authorization = configuration["LavaLinkPassword"];
|
||||
})
|
||||
.AddSingleton<LavaNode>()
|
||||
.AddSingleton<MusicEmbed>();
|
||||
.AddSingleton<MusicEmbed>()
|
||||
.AddSingleton<ChatSettings>()
|
||||
.Configure<ChatSettings>(configuration.GetSection("LLM"));
|
||||
|
||||
client.Ready += () => Client_Ready(client);
|
||||
client.Log += Log;
|
||||
|
||||
@@ -2,9 +2,16 @@
|
||||
```mermaid
|
||||
flowchart TD
|
||||
Program[Program] -->|Register| EventListener
|
||||
EventListener[DiscordEventListener] --> A
|
||||
EventListener[DiscordEventListener] --> A[MessageReceivedHandler]
|
||||
|
||||
A[MessageReceivedHandler] -->|Message| C{Send to correct command by
|
||||
EventListener[DiscordEventListener] --> A2[SlashCommandReceivedHandler]
|
||||
|
||||
A --> |Message| f{If bot is mentioned}
|
||||
f --> v[ChatHandler]
|
||||
v --> o[Ollama Server]
|
||||
o --> v
|
||||
|
||||
A2[SlashCommandReceivedHandler] -->|Message| C{Send to correct command by
|
||||
looking at commandName}
|
||||
|
||||
C -->|JoinCommand| D[JoinHandler]
|
||||
@@ -13,6 +20,17 @@ flowchart TD
|
||||
C -->|GoodbyeCommand| G[GoodbyeHandler]
|
||||
```
|
||||
|
||||
## Handler integrations
|
||||
```mermaid
|
||||
flowchart TD
|
||||
D[JoinHandler] --> Disc[Discord Api]
|
||||
E[PlayHandler] --> Disc[Discord Api]
|
||||
F[HelloHandler] --> Disc[Discord Api]
|
||||
G[GoodbyeHandler] --> Disc[Discord Api]
|
||||
v[ChatHandler] --> Disc[Discord Api]
|
||||
E --> Lava[Lavalink]
|
||||
```
|
||||
|
||||
Program registers an event listener ```DiscordEventListener``` which publish a message :
|
||||
|
||||
```c#
|
||||
|
||||
17
Bot/appsettings.json
Normal file
17
Bot/appsettings.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
},
|
||||
"Token": "discordToken",
|
||||
"LavaLinkPassword": "youshallnotpass",
|
||||
"LavaLinkHostname": "127.0.0.1",
|
||||
"LavaLinkPort": 2333
|
||||
"LLM": {
|
||||
"Url": "http://192.168.50.54:11434",
|
||||
"Model": "gemma"
|
||||
}
|
||||
}
|
||||
10
README.md
10
README.md
@@ -10,10 +10,12 @@ Lunaris2 is a Discord bot designed to play music in your server's voice channels
|
||||
|
||||
## Setup
|
||||
|
||||
1. Clone the repository.
|
||||
2. Install the required packages by running `dotnet restore`.
|
||||
3. Build the project using `dotnet build`.
|
||||
4. Run the bot using `dotnet run`.
|
||||
1. Clone the repo.
|
||||
2. Extract.
|
||||
3. If there isn't already a appsettings.json file in there, create one.
|
||||
4. Set the discord bot token. How the file should look (without token): [appsettings.json](https://github.com/Myxelium/Lunaris2.0/blob/master/Bot/appsettings.json)]
|
||||
5. Make sure you got docker installed. And run the file ``start-services.sh``, make sure you got git-bash installed.
|
||||
6. Now you can start the project and run the application.
|
||||
|
||||
## Usage
|
||||
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
server: # REST and WS server
|
||||
port: 2333
|
||||
address: 0.0.0.0
|
||||
http2:
|
||||
enabled: false # Whether to enable HTTP/2 support
|
||||
plugins:
|
||||
# name: # Name of the plugin
|
||||
# some_key: some_value # Some key-value pair for the plugin
|
||||
# another_key: another_value
|
||||
lavalink:
|
||||
plugins:
|
||||
# - dependency: "com.github.example:example-plugin:1.0.0" # required, the coordinates of your plugin
|
||||
# repository: "https://maven.example.com/releases" # optional, defaults to the Lavalink releases repository by default
|
||||
# snapshot: false # optional, defaults to false, used to tell Lavalink to use the snapshot repository instead of the release repository
|
||||
# pluginsDir: "./plugins" # optional, defaults to "./plugins"
|
||||
# defaultPluginRepository: "https://maven.lavalink.dev/releases" # optional, defaults to the Lavalink release repository
|
||||
# defaultPluginSnapshotRepository: "https://maven.lavalink.dev/snapshots" # optional, defaults to the Lavalink snapshot repository
|
||||
# - dependency: "group:artifact:version"
|
||||
# repository: "repository"
|
||||
server:
|
||||
password: "youshallnotpass"
|
||||
sources:
|
||||
youtube: true
|
||||
bandcamp: true
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
services:
|
||||
lavalink:
|
||||
# pin the image version to Lavalink v4
|
||||
image: ghcr.io/lavalink-devs/lavalink:3.7.10
|
||||
image: ghcr.io/lavalink-devs/lavalink:3.7.11
|
||||
container_name: lavalink
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user