diff --git a/Bot_Discord_CSharp.sln b/Bot_Discord_CSharp.sln new file mode 100644 index 0000000..a225217 --- /dev/null +++ b/Bot_Discord_CSharp.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30717.126 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bot_Discord_CSharp", "Bot_Discord_CSharp\Bot_Discord_CSharp.csproj", "{644BDC0A-8F5D-44C7-A092-DFD762420943}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {644BDC0A-8F5D-44C7-A092-DFD762420943}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {644BDC0A-8F5D-44C7-A092-DFD762420943}.Debug|Any CPU.Build.0 = Debug|Any CPU + {644BDC0A-8F5D-44C7-A092-DFD762420943}.Release|Any CPU.ActiveCfg = Release|Any CPU + {644BDC0A-8F5D-44C7-A092-DFD762420943}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A6E38E62-98A2-4AE2-8C2F-6C4B88CB1A56} + EndGlobalSection +EndGlobal diff --git a/Bot_Discord_CSharp/Bot.cs b/Bot_Discord_CSharp/Bot.cs new file mode 100644 index 0000000..5e9f23c --- /dev/null +++ b/Bot_Discord_CSharp/Bot.cs @@ -0,0 +1,78 @@ +using DSharpPlus; +using DSharpPlus.CommandsNext; +using DSharpPlus.EventArgs; +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Bot_Discord_CSharp.Dto; +using Bot_Discord_CSharp.Commands; +using Microsoft.Extensions.Logging; +using DSharpPlus.Interactivity; +using DSharpPlus.Interactivity.Extensions; + +namespace Bot_Discord_CSharp +{ + public class Bot + { + public DiscordClient Client { get; private set; } + public InteractivityExtension Interactivity { get; private set; } + public CommandsNextExtension Commands { get; private set; } + + public async Task RunAsync() + { + var json = string.Empty; + + using (var fs = File.OpenRead(@"E:\Proyectos Visual Studio\Bot_Discord_CSharp\Bot_Discord_CSharp\config.json")) + using (var sr = new StreamReader(fs, new UTF8Encoding(false))) + json = await sr.ReadToEndAsync().ConfigureAwait(false); + + var configJson = JsonConvert.DeserializeObject(json); + + var config = new DiscordConfiguration + { + Token = configJson.Token, + TokenType = TokenType.Bot, + AutoReconnect = true, + MinimumLogLevel = LogLevel.Debug + }; + + Client = new DiscordClient(config); + + Client.Ready += OnClientReady; + + Client.UseInteractivity(new InteractivityConfiguration + { + Timeout = TimeSpan.FromMinutes(1) + }); + + var commandsConfig = new CommandsNextConfiguration + { + StringPrefixes = new string[] { configJson.Prefix }, + EnableDms = false, + EnableMentionPrefix = true, + DmHelp = false + }; + + Commands = Client.UseCommandsNext(commandsConfig); + + RegisterCommands(); + + await Client.ConnectAsync(); + + await Task.Delay(-1); + } + + private Task OnClientReady(DiscordClient client, ReadyEventArgs e) + { + return null; + } + + private void RegisterCommands() + { + Commands.RegisterCommands(); + } + } +} diff --git a/Bot_Discord_CSharp/Bot_Discord_CSharp.csproj b/Bot_Discord_CSharp/Bot_Discord_CSharp.csproj new file mode 100644 index 0000000..c9cb4d5 --- /dev/null +++ b/Bot_Discord_CSharp/Bot_Discord_CSharp.csproj @@ -0,0 +1,15 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + diff --git a/Bot_Discord_CSharp/Commands/TestCommand.cs b/Bot_Discord_CSharp/Commands/TestCommand.cs new file mode 100644 index 0000000..4427c43 --- /dev/null +++ b/Bot_Discord_CSharp/Commands/TestCommand.cs @@ -0,0 +1,203 @@ +using DSharpPlus.CommandsNext; +using DSharpPlus.CommandsNext.Attributes; +using DSharpPlus.Entities; +using DSharpPlus.Interactivity.Extensions; +using HtmlAgilityPack; +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using System.Xml; +using System.Xml.Linq; + +namespace Bot_Discord_CSharp.Commands +{ + class TestCommand : BaseCommandModule + { + [Command("ping")] + [Description("Dices ping y te responde pong")] + [RequireRoles(RoleCheckMode.Any, "Tester", "Senador")] + public async Task Ping(CommandContext ctx) + { + await ctx.Channel.SendMessageAsync("Pong").ConfigureAwait(false); + } + + [Command("response")] + public async Task Response(CommandContext ctx) + { + var interactivity = ctx.Client.GetInteractivity(); + + var message = await interactivity.WaitForMessageAsync(x => x.Channel == ctx.Channel && x.Author == ctx.User).ConfigureAwait(false); + + await ctx.Channel.SendMessageAsync(message.Result.Content); + + await ctx.Member.SendMessageAsync("Hijo de puta"); + } + + [Command("respondReaction")] + public async Task ResponseReaction(CommandContext ctx) + { + var interactivity = ctx.Client.GetInteractivity(); + + var message = await interactivity.WaitForReactionAsync(x => x.Channel == ctx.Channel).ConfigureAwait(false); + + await ctx.Channel.SendMessageAsync(message.Result.Emoji); + } + + [Command("acontecimientos")] + [Description("Muestra todos los acontecimientos históricos que han pasado hoy")] + public async Task GetDocumentWeb(CommandContext ctx) + { + var interactivity = ctx.Client.GetInteractivity(); + + Uri address = new Uri("https://es.wikipedia.org/wiki/" + Today().Replace(" ", "_")); + HtmlNode node = GetHtmlNodeFromPage(address); + + //< innertext > + int numH2 = 0; + List emojiKeyList = ctx.Guild.Emojis.Keys.Cast().ToList(); + string extractedData = ""; + + DiscordEmbedBuilder embedBuilder = new DiscordEmbedBuilder(); + List embedList = new List(); + embedBuilder.WithTitle("Acontecimientos del día: " + Today()); + + foreach (HtmlNode line in node.Descendants()) + { + if (line.Name.ToString() == "h2" && line.OuterHtml.Contains("mw-headline")) numH2++; + if (numH2 == 1 && line.Name == "li") + { + if (extractedData.Length + line.InnerText.Length > 2000) + { + embedList.Add(embedBuilder.WithDescription(extractedData).Build()); + extractedData = ""; + } + ulong randomEmoji = emojiKeyList[new Random().Next(0, emojiKeyList.Count)]; + extractedData += DiscordEmoji.FromGuildEmote(ctx.Client, randomEmoji) + line.InnerText + "\n"; + } + else if (numH2 > 1) break; + } + embedList.Add(embedBuilder.WithDescription(extractedData).Build()); + await SendEmbeds(ctx, embedList); + } + + [Command("fecha")] + public async Task GetCurrentDay(CommandContext ctx) + { + await ctx.Channel.SendMessageAsync(Today()); + } + + [Command("borrar")] + public async Task DeleteAllMessages(CommandContext ctx) + { + IReadOnlyCollection messages = await ctx.Channel.GetMessagesAsync(100); + await ctx.Channel.DeleteMessagesAsync(messages); + } + + [Command("random")] + [Description("Te da un número aleatorio entre 0 y X o entre X y Y")] + public async Task GetRandomNumber(CommandContext ctx, params string[] command) + { + if (!command.Any()) + { + await ctx.Channel.SendMessageAsync("Se necesita uno o dos valores para funcionar." + + "\nEj: `!random 5` o `!random 5 10`"); + } else + { + int length = command.Length; + await ctx.Channel.SendMessageAsync(new Random().Next(length > 1 ? Int32.Parse(command[0]) : 0, + Int32.Parse(command[length - 1]) + 1).ToString()); + } + } + + [Command("dice")] + [Description("Tira un dado de 6 caras")] + public async Task ThrowDice(CommandContext ctx) + { + await ctx.Channel.SendMessageAsync((new Random().Next(6) + 1).ToString()); + } + + [Command("coin")] + [Description("Tira una moneda")] + public async Task ThrowCoin(CommandContext ctx) + { + int randomValue = new Random().Next(2); + string ladoMoneda = randomValue == 0 ? "Cara" : "Cruz"; + await ctx.Channel.SendMessageAsync(ladoMoneda); + } + + private string Today() + { + DateTime currentTime = DateTime.Today; + IFormatProvider culture = new CultureInfo("es-ES", true); + string[] currentTimeToString = currentTime.GetDateTimeFormats('m', culture); + return currentTimeToString[0]; + } + + private HtmlNode GetHtmlNodeFromPage(Uri url) + { + WebClient webClient = new WebClient(); + webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; + + string sDocument = webClient.DownloadString(url); + + var document = new HtmlDocument(); + document.LoadHtml(sDocument); + + HtmlNode node = document.DocumentNode.SelectSingleNode("//div[@class=\"mw-parser-output\"]"); + + string output = node != null ? node.InnerText : "Error!!"; + System.Diagnostics.Debug.WriteLine(output); + + return node; + } + + private async Task SendEmbeds(CommandContext ctx, List embedList) + { + List colours = ProgresiveColour(embedList.Count()); + for (int i = 0; i < embedList.Count; i++) + { + DiscordEmbedBuilder embedBuilder = new DiscordEmbedBuilder(embedList[i]); + embedBuilder.WithTitle(embedBuilder.Title + " | Parte " + (i+1) + "/" + embedList.Count()); + embedBuilder.WithColor(new DiscordColor(colours[i])); + await ctx.Channel.SendMessageAsync("", false, embedBuilder.Build()); + } + } + + private List ProgresiveColour(int steps) + { + List colours = new List(); + for (int i = 1; i <= steps; i++) + { + int decimalHex = ColourCalculus(i, steps); + string hex = decimalHex.ToString("x"); + colours.Add(FormatHex(hex)); + } + return colours; + } + + private int ColourCalculus(int iteration, int max) + { + if (iteration == 1) + { + return (int)((15 * Math.Pow(16, 5)) + (15 * Math.Pow(16, 4))); + } + if (iteration != 1 && iteration != max) + { + return (int)((15 / (iteration - 1) * Math.Pow(16, 5)) + (15 / (iteration - 1) * Math.Pow(16, 4)) + + (15 / (max - iteration) * Math.Pow(16, 3)) + (15 / (max - iteration) * Math.Pow(16, 2))); + } + return (int)((15 * Math.Pow(16, 3)) + (15 * Math.Pow(16, 2))); + } + + private string FormatHex(string hex) + { + if (hex.Length == 4) return "#00" + hex; + return "#" + hex; + } + } +} diff --git a/Bot_Discord_CSharp/Dto/ConfigDto.cs b/Bot_Discord_CSharp/Dto/ConfigDto.cs new file mode 100644 index 0000000..9913bb7 --- /dev/null +++ b/Bot_Discord_CSharp/Dto/ConfigDto.cs @@ -0,0 +1,15 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Bot_Discord_CSharp.Dto +{ + class ConfigDto + { + [JsonProperty("token")] + public string Token { get; private set; } + [JsonProperty("Prefix")] + public string Prefix { get; private set; } + } +} diff --git a/Bot_Discord_CSharp/Program.cs b/Bot_Discord_CSharp/Program.cs new file mode 100644 index 0000000..6318961 --- /dev/null +++ b/Bot_Discord_CSharp/Program.cs @@ -0,0 +1,13 @@ +using System; + +namespace Bot_Discord_CSharp +{ + class Program + { + static void Main(string[] args) + { + Bot bot = new Bot(); + bot.RunAsync().GetAwaiter().GetResult(); + } + } +} diff --git a/Bot_Discord_CSharp/config.json b/Bot_Discord_CSharp/config.json new file mode 100644 index 0000000..a39ba92 --- /dev/null +++ b/Bot_Discord_CSharp/config.json @@ -0,0 +1,4 @@ +{ + "token": "NzgzODIwMzI4ODgyNDA1Mzk2.X8gTkQ.tqeS-1tkMHzGedgLWqw89GDNAHg", + "prefix": "!" +}