diff --git a/src/Modix.Bot/Modules/DocumentationModule.cs b/src/Modix.Bot/Modules/DocumentationModule.cs deleted file mode 100644 index aca416b57..000000000 --- a/src/Modix.Bot/Modules/DocumentationModule.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System.Linq; -using System.Text; -using System.Text.RegularExpressions; -using System.Threading.Tasks; -using Discord; -using Discord.Interactions; -using Modix.Bot.Responders.AutoRemoveMessages; -using Modix.Services.AutoRemoveMessage; -using Modix.Services.CommandHelp; -using Modix.Services.Csharp; - -namespace Modix.Modules -{ - [ModuleHelp("Documentation", "Search for information within the .NET docs.")] - [HelpTags("docs")] - public class DocumentationModule : InteractionModuleBase - { - private readonly AutoRemoveMessageService _autoRemoveMessageService; - private readonly DocumentationService _documentationService; - - // lang=regex - private const string QueryPattern = "^[0-9A-Za-z.<>]$"; - - public DocumentationModule(DocumentationService documentationService, - AutoRemoveMessageService autoRemoveMessageService) - { - _documentationService = documentationService; - _autoRemoveMessageService = autoRemoveMessageService; - } - - [SlashCommand("docs", "Shows class/method reference from the new unified .NET reference.")] - public async Task GetDocumentationAsync( - [Summary(description: "The term to search for in the documentation.")] - string term) - { - var reg = new Regex(QueryPattern); - foreach (var c in term) - { - if(!reg.IsMatch(c.ToString())) - { - //Double the escape char so discord will print it as well - var s = (c == '\\') ? "\\\\" : c.ToString(); - await FollowupAsync($"'{s}' character is not allowed in the search, please try again. Query must match the following regex: `{QueryPattern}`", allowedMentions: AllowedMentions.None); - return; - } - } - - var response = await _documentationService.GetDocumentationResultsAsync(term); - - if (response.Count == 0) - { - await FollowupAsync("Could not find documentation for your requested term."); - return; - } - - var embedCount = 0; - var stringBuild = new StringBuilder(); - - foreach (var res in response.Results.Take(3)) - { - embedCount++; - - var urlText = $"{res.ItemType}: {res.DisplayName}"; - var url = $"https://docs.microsoft.com{res.Url.Replace("(", "%28").Replace(")", "%29")}"; - - stringBuild.AppendLine(Format.Bold($"❯ {Format.Url(urlText, url)}")); - stringBuild.AppendLine(res.Description); - stringBuild.AppendLine(); - - if (embedCount == 3) - { - stringBuild.Append( - $"{embedCount}/{response.Results.Count} results shown ~ [click here for more results](https://docs.microsoft.com/dotnet/api/?term={term})" - ); - } - } - - var buildEmbed = new EmbedBuilder() - { - Description = stringBuild.ToString() - }.WithColor(new Color(46, 204, 113)); - - var message = await FollowupAsync(embed: buildEmbed.Build()); - - await _autoRemoveMessageService.RegisterRemovableMessageAsync(Context.User, buildEmbed, async (e) => - { - await message.ModifyAsync(a => - { - a.Content = string.Empty; - a.Embed = e.Build(); - }); - return message; - }); - } - } -} diff --git a/src/Modix.Bot/Modules/WikipediaModule.cs b/src/Modix.Bot/Modules/WikipediaModule.cs deleted file mode 100644 index 1f4f89bab..000000000 --- a/src/Modix.Bot/Modules/WikipediaModule.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Discord; -using Discord.Interactions; -using Modix.Services.CommandHelp; -using Modix.Services.Wikipedia; - -namespace Modix.Modules -{ - [ModuleHelp("Wikipedia", "Search Wikipedia from Discord!")] - public class WikipediaModule : InteractionModuleBase - { - private readonly WikipediaService _wikipediaService; - - public WikipediaModule(WikipediaService wikipediaService) - { - _wikipediaService = wikipediaService; - } - - [SlashCommand("wiki", "Returns a Wikipedia page result matching the search phrase.")] - public async Task RunAsync( - [Summary(description: "The phrase to search on Wikipedia.")] - string phrase) - { - var response = await _wikipediaService.GetWikipediaResultsAsync(phrase); - - // Empty response. - if (response == null || response.Query == null || !response.Query.Pages.Any()) - { - await FollowupAsync($"Failed to find anything for {phrase}.", allowedMentions: AllowedMentions.None); - return; - } - - // Construct results into one string (use StringBuilder for concat speed). - var messageBuilder = new StringBuilder(); - - foreach (var pageValue in response.Query.Pages.Values) - { - messageBuilder.AppendLine(pageValue.Extract); - } - - var message = messageBuilder.ToString().Trim(); - - // Sometimes we get here and there's no message, just double check. - if (string.IsNullOrEmpty(message)) - { - await FollowupAsync($"Failed to find anything for {phrase}.", allowedMentions: AllowedMentions.None); - return; - } - - // Discord has a limit on channel message size... so this accounts for that. - if (message.Length > DiscordConfig.MaxMessageSize) - { - // How many batches do we need to send? - // IE: 5000 / 2000 = 2.5 - // Round up = 3 - var batchCount = Math.Ceiling(decimal.Divide(message.Length, DiscordConfig.MaxMessageSize)); - - // Keep track of how many characters we've sent to the channel. - // Use the cursor to see the diff between what we've sent, and what is remaining to send - // So we can satisfy the batch sending approach. - var cursor = 0; - - for (var i = 0; i < batchCount; i++) - { - var builder = new EmbedBuilder() - .WithColor(new Color(95, 186, 125)) - .WithTitle($"Results for {phrase} (pt {i + 1})") - .WithDescription(message.Substring(cursor, (i == batchCount - 1) ? message.Length - cursor : DiscordConfig.MaxMessageSize)); - - await FollowupAsync(embed: builder.Build()); - cursor += DiscordConfig.MaxMessageSize; - } - } - else - { - var builder = new EmbedBuilder() - .WithColor(new Color(95, 186, 125)) - .WithTitle($"Results for {phrase}") - .WithDescription(message); - - await FollowupAsync(embed: builder.Build()); - } - } - } -} diff --git a/src/Modix.Services/Csharp/DocumentationApiResponse.cs b/src/Modix.Services/Csharp/DocumentationApiResponse.cs deleted file mode 100644 index 65f4fc862..000000000 --- a/src/Modix.Services/Csharp/DocumentationApiResponse.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System.Collections.Generic; - -namespace Modix.Services.Csharp -{ - public class DocumentationApiResponse - { - public List Results { get; set; } = new List(); - - public int Count { get; set; } - } - - public class DocumentationMember - { - public string DisplayName { get; set; } - - public string Url { get; set; } - - public string ItemType { get; set; } - - public string ItemKind { get; set; } - - public string Description { get; set; } - } -} diff --git a/src/Modix.Services/Csharp/DocumentationService.cs b/src/Modix.Services/Csharp/DocumentationService.cs deleted file mode 100644 index 43eac8810..000000000 --- a/src/Modix.Services/Csharp/DocumentationService.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System.Net; -using System.Net.Http; -using System.Text.Json; -using System.Threading.Tasks; - -namespace Modix.Services.Csharp -{ - public class DocumentationService - { - private const string ApiReferenceUrl = "https://docs.microsoft.com/api/apibrowser/dotnet/search?api-version=0.2&search="; - private const string ApiFilter = "&locale=en-us&$filter=monikers/any(t:%20t%20eq%20%27net-7.0%27)%20or%20monikers/any(t:%20t%20eq%20%27netframework-4.8%27)%20or%20monikers/any(t:%20t%20eq%20%27win-comm-toolkit-dotnet-stable%27)"; - - public DocumentationService(IHttpClientFactory httpClientFactory) - { - HttpClientFactory = httpClientFactory; - } - - public async Task GetDocumentationResultsAsync(string term) - { - var client = HttpClientFactory.CreateClient(); - var response = await client.GetAsync($"{ApiReferenceUrl}{term}{ApiFilter}"); - - if (!response.IsSuccessStatusCode) - { - throw new WebException("Something failed while querying the .NET Api docs."); - } - var jsonResponse = await response.Content.ReadAsStringAsync(); - return JsonSerializer.Deserialize(jsonResponse, _deserializationOptions); - } - - protected IHttpClientFactory HttpClientFactory { get; } - - private static readonly JsonSerializerOptions _deserializationOptions = new() - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - }; - } -} diff --git a/src/Modix.Services/Extensions/CommandContextExtensions.cs b/src/Modix.Services/Extensions/CommandContextExtensions.cs deleted file mode 100644 index 0b1f64b8b..000000000 --- a/src/Modix.Services/Extensions/CommandContextExtensions.cs +++ /dev/null @@ -1,15 +0,0 @@ -#nullable enable -using System.Threading.Tasks; -using Discord; - -namespace Modix.Services.Extensions -{ - public static class CommandContextExtensions - { - public static Task ReplyAsync(this IMessage context, - string message, Embed? embed = null) - { - return context.Channel.SendMessageAsync(message, embed: embed); - } - } -} diff --git a/src/Modix.Services/Extensions/GuildChannelExtensions.cs b/src/Modix.Services/Extensions/GuildChannelExtensions.cs deleted file mode 100644 index 9a014f7ce..000000000 --- a/src/Modix.Services/Extensions/GuildChannelExtensions.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Discord; - -namespace Modix.Services.Extensions -{ - public static class GuildChannelExtensions - { - public static bool IsPublic(this IGuildChannel channel) - { - if (channel?.Guild is IGuild guild) - { - var permissions = channel.GetPermissionOverwrite(guild.EveryoneRole); - - return !permissions.HasValue || permissions.Value.ViewChannel != PermValue.Deny; - } - - return false; - } - } -} diff --git a/src/Modix.Services/Wikipedia/WikipediaResponse.cs b/src/Modix.Services/Wikipedia/WikipediaResponse.cs deleted file mode 100644 index 2a2f55ea8..000000000 --- a/src/Modix.Services/Wikipedia/WikipediaResponse.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; - -namespace Modix.Services.Wikipedia -{ - public class WikipediaResponse - { - public WikipediaQuery Query { get; set; } - } - - public class WikipediaQuery - { - public Dictionary Pages { get; set; } - } - - public class WikipediaPage - { - public string Extract { get; set; } - public string Title { get; set; } - } -} diff --git a/src/Modix.Services/Wikipedia/WikipediaService.cs b/src/Modix.Services/Wikipedia/WikipediaService.cs deleted file mode 100644 index b06fd3fb0..000000000 --- a/src/Modix.Services/Wikipedia/WikipediaService.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System.Net; -using System.Net.Http; -using System.Threading.Tasks; -using Newtonsoft.Json; - -namespace Modix.Services.Wikipedia -{ - public class WikipediaService - { - private const string WikipediaApiScheme = "https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exlimit=max&explaintext&exintro&titles={0}&redirects="; - - public WikipediaService(IHttpClientFactory httpClientFactory) - { - HttpClientFactory = httpClientFactory; - } - - public async Task GetWikipediaResultsAsync(string phrase) - { - var query = string.Join(" ", phrase); - query = WebUtility.UrlEncode(query); - var client = HttpClientFactory.CreateClient(); - var response = await client.GetAsync(string.Format(WikipediaApiScheme, query)); - - if (!response.IsSuccessStatusCode) - { - throw new WebException("Something failed while querying the Wikipedia API."); - } - - var jsonResponse = await response.Content.ReadAsStringAsync(); - return JsonConvert.DeserializeObject(jsonResponse); - } - - protected IHttpClientFactory HttpClientFactory { get; } - } -} diff --git a/src/Modix/Extensions/ServiceCollectionExtensions.cs b/src/Modix/Extensions/ServiceCollectionExtensions.cs index a2d8ba6a8..545105f44 100644 --- a/src/Modix/Extensions/ServiceCollectionExtensions.cs +++ b/src/Modix/Extensions/ServiceCollectionExtensions.cs @@ -8,12 +8,10 @@ using Discord.WebSocket; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Modix.Behaviors; using Modix.Bot; using Modix.Bot.Behaviors; -using Modix.Bot.Responders; using Modix.Bot.Responders.AutoRemoveMessages; using Modix.Bot.Responders.CommandErrors; using Modix.Bot.Responders.MessageQuotes; @@ -22,10 +20,8 @@ using Modix.Data.Models.Core; using Modix.Data.Repositories; using Modix.Services; -using Modix.Services.AutoRemoveMessage; using Modix.Services.CommandHelp; using Modix.Services.Core; -using Modix.Services.Csharp; using Modix.Services.EmojiStats; using Modix.Services.GuildStats; using Modix.Services.Images; @@ -34,7 +30,6 @@ using Modix.Services.Starboard; using Modix.Services.Tags; using Modix.Services.Utilities; -using Modix.Services.Wikipedia; using Polly; using Polly.Extensions.Http; @@ -169,9 +164,6 @@ public static IServiceCollection AddModix( services.AddMemoryCache(); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); services.AddScoped, PromotionLoggingHandler>();