Skip to content

Commit

Permalink
Merge pull request #76 from AngeloDotNet/update-solution
Browse files Browse the repository at this point in the history
Update target framework to .NET 6 and nuget packages
  • Loading branch information
kasuken authored Nov 20, 2023
2 parents 0325479 + f88d734 commit 24269f7
Show file tree
Hide file tree
Showing 7 changed files with 232 additions and 230 deletions.
36 changes: 18 additions & 18 deletions SonequaBot.Discord/SonequaBot.Discord.csproj
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk.Worker;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<UserSecretsId>dotnet-SonequaBot.Discord-76E99DEC-8058-4822-A939-3CA6AC8614A8</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="4.0.0-rc1" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-rc1" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.9" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SonequaBot.Shared\SonequaBot.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Settings.job">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<Project Sdk="Microsoft.NET.Sdk.Worker;Microsoft.NET.Sdk.Publish">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<UserSecretsId>dotnet-SonequaBot.Discord-76E99DEC-8058-4822-A939-3CA6AC8614A8</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="4.4.3" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SonequaBot.Shared\SonequaBot.Shared.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Settings.job">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
298 changes: 149 additions & 149 deletions SonequaBot.Discord/SonequaDiscord.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SonequaDiscord> _logger;

private readonly List<ICommand> BotCommands = new List<ICommand>();

private readonly string[] BotUsers = {"sonequabot", "streamelements"};
public SonequaDiscord(ILogger<SonequaDiscord> 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<Type> 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<SonequaDiscord> _logger;

private readonly List<ICommand> BotCommands = new List<ICommand>();

private readonly string[] BotUsers = { "sonequabot", "streamelements" };

public SonequaDiscord(ILogger<SonequaDiscord> 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<Type> 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.");
}
}
}
20 changes: 10 additions & 10 deletions SonequaBot.Sentiment/SonequaBot.Sentiment.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Folder Include="Processors\" />
</ItemGroup>
<ItemGroup>
<Folder Include="Processors\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.TextAnalytics" Version="5.0.0" />
<PackageReference Include="Azure.Core" Version="1.2.2" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Azure.AI.TextAnalytics" Version="5.3.0" />
<PackageReference Include="Azure.Core" Version="1.36.0" />
</ItemGroup>

</Project>
Loading

0 comments on commit 24269f7

Please sign in to comment.