From b11fa82842db1bd1a091702d46e0a89312066f19 Mon Sep 17 00:00:00 2001 From: Can Karatepe Date: Tue, 6 Aug 2024 22:20:42 +0300 Subject: [PATCH] Implement selcuksports command Also move HttpClient to have a single shared static instance --- MedicBot/Commands/BaseCommands.cs | 3 --- MedicBot/Commands/MiscCommands.cs | 16 ++++++++++++++++ MedicBot/Controller/MiscController.cs | 16 ++++++++++++++++ MedicBot/Manager/AudioManager.cs | 3 +-- MedicBot/Manager/ImageManager.cs | 3 +-- MedicBot/Manager/ImportExportManager.cs | 3 +-- MedicBot/Manager/MiscManager.cs | 19 +++++++++++++++++++ MedicBot/MedicBot.csproj | 1 + MedicBot/Program.cs | 2 ++ 9 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 MedicBot/Commands/MiscCommands.cs create mode 100644 MedicBot/Controller/MiscController.cs create mode 100644 MedicBot/Manager/MiscManager.cs diff --git a/MedicBot/Commands/BaseCommands.cs b/MedicBot/Commands/BaseCommands.cs index a7cfd05..9ac9695 100644 --- a/MedicBot/Commands/BaseCommands.cs +++ b/MedicBot/Commands/BaseCommands.cs @@ -1,9 +1,6 @@ using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext.Attributes; -using DSharpPlus.Entities; -using DSharpPlus.Interactivity.Extensions; using MedicBot.Manager; -using MedicBot.Utils; using MongoDB.Bson; using MongoDB.Driver; using Serilog; diff --git a/MedicBot/Commands/MiscCommands.cs b/MedicBot/Commands/MiscCommands.cs new file mode 100644 index 0000000..094d875 --- /dev/null +++ b/MedicBot/Commands/MiscCommands.cs @@ -0,0 +1,16 @@ +using System.Xml; +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using HtmlAgilityPack; + +namespace MedicBot.Commands; + +public class MiscCommands : BaseCommandModule +{ + [Command("selçuk")] + [Aliases("selcuk", "selcuksports")] + public async Task SelcukSport(CommandContext ctx) + { + await ctx.RespondAsync(await MiscManager.GetSelcukSportsUrlAsync()); + } +} \ No newline at end of file diff --git a/MedicBot/Controller/MiscController.cs b/MedicBot/Controller/MiscController.cs new file mode 100644 index 0000000..0ee2bb3 --- /dev/null +++ b/MedicBot/Controller/MiscController.cs @@ -0,0 +1,16 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace MedicBot; + +[ApiController] +public class MiscController : ControllerBase +{ + [HttpGet("selcuksports")] + [Authorize(Policy = "CombinedPolicy")] + public async Task SelcukSports() + { + var url = await MiscManager.GetSelcukSportsUrlAsync(); + return Redirect(url); + } +} diff --git a/MedicBot/Manager/AudioManager.cs b/MedicBot/Manager/AudioManager.cs index 0e1d355..cf31fe7 100644 --- a/MedicBot/Manager/AudioManager.cs +++ b/MedicBot/Manager/AudioManager.cs @@ -60,8 +60,7 @@ public static async Task AddAsync(string audioName, ulong userId, string url) { Log.Information("Downloading: {DownloadUrl}", url); Log.Information("Downloading file to {FilePath}", filePath); - using var client = new HttpClient(); - await using var stream = await client.GetStreamAsync(url); + await using var stream = await Program.Client.GetStreamAsync(url); await using var fileStream = File.OpenWrite(filePath); await stream.CopyToAsync(fileStream); await fileStream.FlushAsync(); diff --git a/MedicBot/Manager/ImageManager.cs b/MedicBot/Manager/ImageManager.cs index cc65451..a1f8559 100644 --- a/MedicBot/Manager/ImageManager.cs +++ b/MedicBot/Manager/ImageManager.cs @@ -62,8 +62,7 @@ public static async Task AddAsync(string imageName, ulong userId, string url) { Log.Information("Downloading file to {FilePath}", filePath); - using var client = new HttpClient(); - await using var stream = await client.GetStreamAsync(url); + await using var stream = await Program.Client.GetStreamAsync(url); await using var fileStream = File.OpenWrite(filePath); await stream.CopyToAsync(fileStream); await fileStream.FlushAsync(); diff --git a/MedicBot/Manager/ImportExportManager.cs b/MedicBot/Manager/ImportExportManager.cs index 46b3252..e99ac5c 100644 --- a/MedicBot/Manager/ImportExportManager.cs +++ b/MedicBot/Manager/ImportExportManager.cs @@ -13,8 +13,7 @@ public static async Task Import(string url) var numberOfEntriesAdded = 0; var jsonString = ""; { - using var client = new HttpClient(); - await using var stream = await client.GetStreamAsync(url); + await using var stream = await Program.Client.GetStreamAsync(url); using var streamReader = new StreamReader(stream); jsonString = await streamReader.ReadToEndAsync(); } diff --git a/MedicBot/Manager/MiscManager.cs b/MedicBot/Manager/MiscManager.cs new file mode 100644 index 0000000..d2ba720 --- /dev/null +++ b/MedicBot/Manager/MiscManager.cs @@ -0,0 +1,19 @@ +using HtmlAgilityPack; + +namespace MedicBot; + +public class MiscManager +{ + public static async Task GetSelcukSportsUrlAsync() + { + var selcukUrl = "https://selcuksportshd78.biz"; + await using var stream = await Program.Client.GetStreamAsync(selcukUrl); + using var streamReader = new StreamReader(stream); + var responseString = await streamReader.ReadToEndAsync(); + var doc = new HtmlDocument(); + doc.LoadHtml(responseString); + var root = doc.DocumentNode ?? throw new FormatException("Could not find root node, probably error in HTML parsing."); + var selcukStreamUrl = root.SelectSingleNode("//div/div/a[1]").Attributes["href"].Value; + return selcukStreamUrl; + } +} diff --git a/MedicBot/MedicBot.csproj b/MedicBot/MedicBot.csproj index ca02b6c..7af310a 100644 --- a/MedicBot/MedicBot.csproj +++ b/MedicBot/MedicBot.csproj @@ -16,6 +16,7 @@ + diff --git a/MedicBot/Program.cs b/MedicBot/Program.cs index 8939be5..9f4d74a 100644 --- a/MedicBot/Program.cs +++ b/MedicBot/Program.cs @@ -23,6 +23,7 @@ namespace MedicBot; internal static class Program { + public static readonly HttpClient Client = new(); private static readonly CancellationTokenSource CancellationTokenSource = new(); private static DiscordClient? _client; @@ -287,6 +288,7 @@ private static async Task ConfigureAsync() commands.RegisterCommands(); commands.RegisterCommands(); commands.RegisterCommands(); + commands.RegisterCommands(); commands.RegisterConverter(new StringLowercaseConverter()); // Interactivity Init