From 433b439f7b36aaccd947ac0635e52579b90c7acd Mon Sep 17 00:00:00 2001 From: "Adam R. Grey" Date: Tue, 30 Nov 2021 21:39:43 -0500 Subject: [PATCH] qr feature --- Features.cs | 108 +++++++++++++++++++++++++++++++ Program.cs | 122 +++++++++--------------------------- Shared.cs | 10 +++ silverworker-discord.csproj | 1 + 4 files changed, 149 insertions(+), 92 deletions(-) create mode 100644 Features.cs create mode 100644 Shared.cs diff --git a/Features.cs b/Features.cs new file mode 100644 index 0000000..a9656d8 --- /dev/null +++ b/Features.cs @@ -0,0 +1,108 @@ +using System; +using System.IO; +using System.Net; +using Discord; +using Discord.WebSocket; +using Newtonsoft.Json; +using QRCoder; + +namespace silverworker_discord +{ + public static class Features + { + public static async void detiktokify(Uri link, SocketUserMessage message) + { + var ytdl = new YoutubeDLSharp.YoutubeDL(); + ytdl.YoutubeDLPath = "youtube-dl"; + ytdl.FFmpegPath = "ffmpeg"; + ytdl.OutputFolder = ""; + ytdl.OutputFileTemplate = "tiktokbad.%(ext)s"; + var res = await ytdl.RunVideoDownload(link.ToString()); + if (!res.Success) + { + Console.Error.WriteLine("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput)); + await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>")); + await message.Channel.SendMessageAsync("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput)); + } + else + { + string path = res.Data; + if (File.Exists(path)) + { + try + { + await message.Channel.SendFileAsync(path); + } + catch (Exception e) + { + await message.Channel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}"); + } + File.Delete(path); + } + else + { + Console.Error.WriteLine("idgi but something happened."); + await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>")); + } + } + } + public static async void deheic(SocketUserMessage message, Attachment att) + { + try + { + var request = WebRequest.Create(att.Url); + HttpWebResponse response = request.GetResponse() as HttpWebResponse; + if(!Directory.Exists("tmp")) + { + Directory.CreateDirectory("tmp"); + } + using (Stream output = File.OpenWrite("tmp/" + att.Filename)) + using (Stream input = response.GetResponseStream()) + { + input.CopyTo(output); + } + if(ExternalProcess.GoPlz("convert", $"tmp/{att.Filename} tmp/{att.Filename}.jpg")) + { + await message.Channel.SendFileAsync($"tmp/{att.Filename}.jpg", "converted from jpeg-but-apple to jpeg"); + File.Delete($"tmp/{att.Filename}"); + File.Delete($"tmp/{att.Filename}.jpg"); + } + else + { + await message.Channel.SendMessageAsync("convert failed :("); + Console.Error.WriteLine("convert failed :("); + } + } + catch (Exception e) + { + await message.Channel.SendMessageAsync($"something failed. aaaadam! {JsonConvert.SerializeObject(e, Formatting.Indented)}"); + Console.Error.WriteLine(JsonConvert.SerializeObject(e, Formatting.Indented)); + } + } + public static async void qrify(string qrContent, SocketUserMessage message) + { + Console.WriteLine($"qring: {qrContent}"); + QRCodeGenerator qrGenerator = new QRCodeGenerator(); + QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrContent, QRCodeGenerator.ECCLevel.Q); + SvgQRCode qrCode = new SvgQRCode(qrCodeData); + string qrCodeAsSvg = qrCode.GetGraphic(20); + int todaysnumber = Shared.r.Next(); + if(!Directory.Exists("tmp")) + { + Directory.CreateDirectory("tmp"); + } + File.WriteAllText($"tmp/qr{todaysnumber}.svg", qrCodeAsSvg); + if(ExternalProcess.GoPlz("convert", $"tmp/qr{todaysnumber}.svg tmp/qr{todaysnumber}.png")) + { + await message.Channel.SendFileAsync($"tmp/qr{todaysnumber}.png"); + File.Delete($"tmp/qr{todaysnumber}.svg"); + File.Delete($"tmp/qr{todaysnumber}.png"); + } + else + { + await message.Channel.SendMessageAsync("convert failed :( aaaaaaadam!"); + Console.Error.WriteLine($"convert failed :( qr{todaysnumber}"); + } + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index a05e0f6..9b9c0b5 100644 --- a/Program.cs +++ b/Program.cs @@ -48,9 +48,9 @@ namespace silverworker_discord await Task.Delay(-1); } - #pragma warning disable 1998 //the "it's async but you're not awaiting anything". +#pragma warning disable 1998 //the "it's async but you're not awaiting anything". private async Task MessageReceived(SocketMessage messageParam) - #pragma warning restore 1998 +#pragma warning restore 1998 { var message = messageParam as SocketUserMessage; if (message == null) return; @@ -64,106 +64,44 @@ namespace silverworker_discord else { //any channel, from a user - var wordLikes = message.Content.Split(' ', StringSplitOptions.TrimEntries); - var links = wordLikes?.Where(wl => Uri.IsWellFormedUriString(wl, UriKind.Absolute)).Select(wl => new Uri(wl)); - if (links != null && links.Count() > 0) + if (message.Content.StartsWith("!qrplz ")) { - foreach (var link in links) + Features.qrify(message.Content.Substring("!qrplz ".Length), message); + } + else + { + var wordLikes = message.Content.Split(' ', StringSplitOptions.TrimEntries); + var links = wordLikes?.Where(wl => Uri.IsWellFormedUriString(wl, UriKind.Absolute)).Select(wl => new Uri(wl)); + if (links != null && links.Count() > 0) { - if (link.Host == "vm.tiktok.com") + foreach (var link in links) { - detiktokify(link, message); + if (link.Host == "vm.tiktok.com") + { + Features.detiktokify(link, message); + } } } - } - if (message.Attachments?.Count > 0) - { - Console.WriteLine($"{message.Attachments.Count} attachments"); - var appleReactions = false; - foreach (var att in message.Attachments) + if (message.Attachments?.Count > 0) { - if (att.Filename?.EndsWith(".heic") == true) + Console.WriteLine($"{message.Attachments.Count} attachments"); + var appleReactions = false; + foreach (var att in message.Attachments) { - deheic(message, att); - appleReactions = true; + if (att.Filename?.EndsWith(".heic") == true) + { + Features.deheic(message, att); + appleReactions = true; + } + } + if (appleReactions) + { +#pragma warning disable 4014 //the "you're not awaiting this" warning. yeah I know, that's the beauty of an async method lol + message.AddReactionAsync(new Emoji("\U0001F34F")); +#pragma warning restore 4014 } } - if (appleReactions) - { - #pragma warning disable 4014 //the "you're not awaiting this" warning. yeah I know, that's the beauty of an async method lol - message.AddReactionAsync(new Emoji("\U0001F34F")); - #pragma warning restore 4014 - } - } - } - } - private async void deheic(SocketUserMessage message, Attachment att) - { - try - { - var request = WebRequest.Create(att.Url); - HttpWebResponse response = request.GetResponse() as HttpWebResponse; - if(!Directory.Exists("tmp")) - { - Directory.CreateDirectory("tmp"); - } - using (Stream output = File.OpenWrite("tmp/" + att.Filename)) - using (Stream input = response.GetResponseStream()) - { - input.CopyTo(output); - } - if(ExternalProcess.GoPlz("convert", $"tmp/{att.Filename} tmp/{att.Filename}.jpg")) - { - await message.Channel.SendFileAsync($"tmp/{att.Filename}.jpg", "converted from jpeg-but-apple to jpeg"); - File.Delete($"tmp/{att.Filename}"); - File.Delete($"tmp/{att.Filename}.jpg"); - } - else - { - await message.Channel.SendMessageAsync("convert failed :("); - Console.Error.WriteLine("convert failed :("); - } - } - catch (Exception e) - { - await message.Channel.SendMessageAsync($"something failed. aaaadam! {JsonConvert.SerializeObject(e, Formatting.Indented)}"); - Console.Error.WriteLine(JsonConvert.SerializeObject(e, Formatting.Indented)); - } - } - private async void detiktokify(Uri link, SocketUserMessage message) - { - var ytdl = new YoutubeDLSharp.YoutubeDL(); - ytdl.YoutubeDLPath = "youtube-dl"; - ytdl.FFmpegPath = "ffmpeg"; - ytdl.OutputFolder = ""; - ytdl.OutputFileTemplate = "tiktokbad.%(ext)s"; - var res = await ytdl.RunVideoDownload(link.ToString()); - if (!res.Success) - { - Console.Error.WriteLine("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput)); - await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>")); - await message.Channel.SendMessageAsync("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput)); - } - else - { - string path = res.Data; - if (File.Exists(path)) - { - try - { - await message.Channel.SendFileAsync(path); - } - catch (Exception e) - { - await message.Channel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}"); - } - File.Delete(path); - } - else - { - Console.Error.WriteLine("idgi but something happened."); - await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>")); } } } diff --git a/Shared.cs b/Shared.cs new file mode 100644 index 0000000..73ff10d --- /dev/null +++ b/Shared.cs @@ -0,0 +1,10 @@ + +using System; + +namespace silverworker_discord +{ + public static class Shared + { + public static Random r = new Random(); + } +} \ No newline at end of file diff --git a/silverworker-discord.csproj b/silverworker-discord.csproj index 9b37191..60e065b 100644 --- a/silverworker-discord.csproj +++ b/silverworker-discord.csproj @@ -11,6 +11,7 @@ +