diff --git a/SonequaBot.Discord/SonequaBot.Discord.csproj b/SonequaBot.Discord/SonequaBot.Discord.csproj index 1121739..4cfa1c7 100644 --- a/SonequaBot.Discord/SonequaBot.Discord.csproj +++ b/SonequaBot.Discord/SonequaBot.Discord.csproj @@ -1,19 +1,19 @@ - - - netcoreapp5.0 - dotnet-SonequaBot.Discord-76E99DEC-8058-4822-A939-3CA6AC8614A8 - - - - - - - - - - - - Always - - + + + net6.0 + dotnet-SonequaBot.Discord-76E99DEC-8058-4822-A939-3CA6AC8614A8 + + + + + + + + + + + + Always + + \ No newline at end of file diff --git a/SonequaBot.Discord/SonequaDiscord.cs b/SonequaBot.Discord/SonequaDiscord.cs index cbcd18a..7809724 100644 --- a/SonequaBot.Discord/SonequaDiscord.cs +++ b/SonequaBot.Discord/SonequaDiscord.cs @@ -1,9 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Threading; -using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.CommandsNext; using DSharpPlus.Entities; @@ -14,150 +8,156 @@ using SonequaBot.Shared.Commands; using SonequaBot.Shared.Commands.Interfaces; using SonequaBot.Shared.Commands.Interfaces.Responses; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Threading; +using System.Threading.Tasks; namespace SonequaBot.Discord { - public class SonequaDiscord : BackgroundService - { - private DiscordClient _discordClient; - - public CommandsNextExtension _commands { get; set; } - - private readonly ILogger _logger; - - private readonly List BotCommands = new List(); - - private readonly string[] BotUsers = {"sonequabot", "streamelements"}; - - public SonequaDiscord(ILogger logger, SonequaSettings options) - { - _logger = logger; - - _discordClient = new DiscordClient(new DiscordConfiguration - { - Token = options.BotToken, - TokenType = TokenType.Bot, - MinimumLogLevel = LogLevel.Debug - }); - - _commands = _discordClient.UseCommandsNext(new CommandsNextConfiguration() - { - UseDefaultCommandHandler = false - }); - - InitializeBotCommands(); - - _discordClient.MessageCreated += CommandHandler; - - _discordClient.ConnectAsync().GetAwaiter().GetResult(); - Task.Delay(-1).GetAwaiter().GetResult(); - } - - private async Task CommandHandler(DiscordClient client, MessageCreateEventArgs e) - { - var source = new CommandSource - { - Channel = e.Channel.Name, - Message = e.Message.Content, - User = e.Author.Username - }; - - if (Array.Exists(BotUsers, element => element.ToLowerInvariant() == source.User.ToLowerInvariant())) return; - - try - { - foreach (var command in BotCommands) - { - if (command.IsActivated(source)) - { - if (command is IResponseMessage messageText) - { - await e.Channel.SendMessageAsync(messageText.GetMessageEvent(source)); - } - - if (command is IResponseImage messageImage) - { - var embed = new DiscordEmbedBuilder - { - ImageUrl = messageImage.GetImageEvent(source) - }; - await e.Channel.SendMessageAsync("", false, embed); - } - - if (command is IResponseVideo messageVideo) - { - var embed = new DiscordEmbedBuilder - { - Url = messageVideo.GetVideoEvent(source) - }; - await e.Channel.SendMessageAsync("", false, embed); - } - - if (command is IResponseAudio messageAudio) - { - var embed = new DiscordEmbedBuilder - { - Url = messageAudio.GetAudioEvent(source) - }; - await e.Channel.SendMessageAsync("", false, embed); - } - - if (command is IResponseImageCard messageCard) - { - var cardData = messageCard.GetImageCardEvent(source); - - var embed = new DiscordEmbedBuilder - { - Title = cardData.Title, - Description = cardData.Description, - ImageUrl = cardData.ImageUrl, - Url = cardData.Url - }; - await e.Channel.SendMessageAsync("", false, embed); - } - - return; - } - } - } - catch (Exception ex) - { - _logger.LogError(ex.Message); - - await e.Channel.SendMessageAsync(ex.Message); - } - } - - private void InitializeBotCommands() - { - List types = Assembly.Load("SonequaBot.Shared").GetTypes() - .Where(t => t.Namespace == "SonequaBot.Shared.Commands") - .ToList(); - - foreach (Type fqcnType in types) - { - if (fqcnType.BaseType != null && fqcnType.BaseType == typeof(CommandBase)) - { - _logger.LogInformation("Load BotCommand : " + fqcnType.ToString()); - BotCommands.Add((ICommand) Activator.CreateInstance(fqcnType)); - } - } - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - _logger.LogInformation("SonequaBot is starting."); - - stoppingToken.Register(() => _logger.LogInformation("SonequaBot is stopping.")); - - while (!stoppingToken.IsCancellationRequested) - { - _logger.LogInformation("SonequaBot is doing background work."); - - await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); - } - - _logger.LogInformation("SonequaBot background task is stopping."); - } - } + public class SonequaDiscord : BackgroundService + { + private DiscordClient _discordClient; + + public CommandsNextExtension _commands { get; set; } + + private readonly ILogger _logger; + + private readonly List BotCommands = new List(); + + private readonly string[] BotUsers = { "sonequabot", "streamelements" }; + + public SonequaDiscord(ILogger logger, SonequaSettings options) + { + _logger = logger; + + _discordClient = new DiscordClient(new DiscordConfiguration + { + Token = options.BotToken, + TokenType = TokenType.Bot, + MinimumLogLevel = LogLevel.Debug + }); + + _commands = _discordClient.UseCommandsNext(new CommandsNextConfiguration() + { + UseDefaultCommandHandler = false + }); + + InitializeBotCommands(); + + _discordClient.MessageCreated += CommandHandler; + + _discordClient.ConnectAsync().GetAwaiter().GetResult(); + Task.Delay(-1).GetAwaiter().GetResult(); + } + + private async Task CommandHandler(DiscordClient client, MessageCreateEventArgs e) + { + var source = new CommandSource + { + Channel = e.Channel.Name, + Message = e.Message.Content, + User = e.Author.Username + }; + + if (Array.Exists(BotUsers, element => element.ToLowerInvariant() == source.User.ToLowerInvariant())) return; + + try + { + foreach (var command in BotCommands) + { + if (command.IsActivated(source)) + { + if (command is IResponseMessage messageText) + { + await e.Channel.SendMessageAsync(messageText.GetMessageEvent(source)); + } + + if (command is IResponseImage messageImage) + { + var embed = new DiscordEmbedBuilder + { + ImageUrl = messageImage.GetImageEvent(source) + }; + await e.Channel.SendMessageAsync("", embed); + } + + if (command is IResponseVideo messageVideo) + { + var embed = new DiscordEmbedBuilder + { + Url = messageVideo.GetVideoEvent(source) + }; + await e.Channel.SendMessageAsync("", embed); + } + + if (command is IResponseAudio messageAudio) + { + var embed = new DiscordEmbedBuilder + { + Url = messageAudio.GetAudioEvent(source) + }; + await e.Channel.SendMessageAsync("", embed); + } + + if (command is IResponseImageCard messageCard) + { + var cardData = messageCard.GetImageCardEvent(source); + + var embed = new DiscordEmbedBuilder + { + Title = cardData.Title, + Description = cardData.Description, + ImageUrl = cardData.ImageUrl, + Url = cardData.Url + }; + await e.Channel.SendMessageAsync("", embed); + } + + return; + } + } + } + catch (Exception ex) + { + _logger.LogError(ex.Message); + + await e.Channel.SendMessageAsync(ex.Message); + } + } + + private void InitializeBotCommands() + { + List types = Assembly.Load("SonequaBot.Shared").GetTypes() + .Where(t => t.Namespace == "SonequaBot.Shared.Commands") + .ToList(); + + foreach (Type fqcnType in types) + { + if (fqcnType.BaseType != null && fqcnType.BaseType == typeof(CommandBase)) + { + _logger.LogInformation("Load BotCommand : " + fqcnType.ToString()); + BotCommands.Add((ICommand)Activator.CreateInstance(fqcnType)); + } + } + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + _logger.LogInformation("SonequaBot is starting."); + + stoppingToken.Register(() => _logger.LogInformation("SonequaBot is stopping.")); + + while (!stoppingToken.IsCancellationRequested) + { + _logger.LogInformation("SonequaBot is doing background work."); + + await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); + } + + _logger.LogInformation("SonequaBot background task is stopping."); + } + } } diff --git a/SonequaBot.Sentiment/SonequaBot.Sentiment.csproj b/SonequaBot.Sentiment/SonequaBot.Sentiment.csproj index 3596318..1bf6b95 100644 --- a/SonequaBot.Sentiment/SonequaBot.Sentiment.csproj +++ b/SonequaBot.Sentiment/SonequaBot.Sentiment.csproj @@ -1,16 +1,16 @@ - - net6.0 - + + net6.0 + - - - + + + - - - - + + + + diff --git a/SonequaBot.Shared/SonequaBot.Shared.csproj b/SonequaBot.Shared/SonequaBot.Shared.csproj index d0b2a85..43e8cfe 100644 --- a/SonequaBot.Shared/SonequaBot.Shared.csproj +++ b/SonequaBot.Shared/SonequaBot.Shared.csproj @@ -1,14 +1,14 @@ - - net6.0 - 8 - + + net6.0 + 8 + - - - - - + + + + + \ No newline at end of file diff --git a/SonequaBot.Tests/SonequaBot.Tests.csproj b/SonequaBot.Tests/SonequaBot.Tests.csproj index 1cab33c..9e2a501 100644 --- a/SonequaBot.Tests/SonequaBot.Tests.csproj +++ b/SonequaBot.Tests/SonequaBot.Tests.csproj @@ -1,23 +1,25 @@ - - netcoreapp5.0 + + net6.0 + false - false + SonequaBot.Sentiment.Tests + - SonequaBot.Sentiment.Tests - + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + - - - - - - - - - - - + + + + \ No newline at end of file diff --git a/SonequaBot.Web/SonequaBot.Web.csproj b/SonequaBot.Web/SonequaBot.Web.csproj index d089cac..b27c204 100644 --- a/SonequaBot.Web/SonequaBot.Web.csproj +++ b/SonequaBot.Web/SonequaBot.Web.csproj @@ -1,18 +1,18 @@ - - netcoreapp5.0 - 9a8e5620-ad6c-49cf-91c4-120ede2c1ab1 - Linux - + + net6.0 + 9a8e5620-ad6c-49cf-91c4-120ede2c1ab1 + Linux + - - - + + + - - - + + + diff --git a/SonequaBot/SonequaBot.csproj b/SonequaBot/SonequaBot.csproj index 7a39887..b6f10bd 100644 --- a/SonequaBot/SonequaBot.csproj +++ b/SonequaBot/SonequaBot.csproj @@ -1,22 +1,22 @@ - - net6.0 - dotnet-SonequaBot-FED8B3A1-E6A9-4072-908B-404D023B97AE - Linux - + + net6.0 + dotnet-SonequaBot-FED8B3A1-E6A9-4072-908B-404D023B97AE + Linux + - - - - - - - - + + + + + + + + - - - - + + + +