qr gen, and separate features

This commit is contained in:
Adam R. Grey 2021-11-30 21:17:33 -05:00
parent 010b40ab13
commit 0c165db536
4 changed files with 157 additions and 99 deletions

109
Features.cs Normal file
View File

@ -0,0 +1,109 @@
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 Shared.botChatterChannel.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 Shared.botChatterChannel.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 Shared.botChatterChannel.SendMessageAsync("convert failed :(");
Console.Error.WriteLine("convert failed :(");
}
}
catch (Exception e)
{
await Shared.botChatterChannel.SendMessageAsync(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.Substring(7), 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 Shared.botChatterChannel.SendMessageAsync("convert failed :(");
Console.Error.WriteLine("convert failed :(");
}
}
}
}

View File

@ -21,10 +21,6 @@ namespace silverworker_discord
.AddJsonFile("appsettings.json", true, true)
.Build();
private ISocketMessageChannel botChatterChannel = null;
private ISocketMessageChannel announcementChannel = null;
private ISocketMessageChannel mtgChannel = null;
public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private Task Log(LogMessage msg)
@ -44,9 +40,9 @@ namespace silverworker_discord
_client.Ready += () => Task.Run(() =>
{
Console.WriteLine("Bot is connected! going to sign up for message received and user joined in client ready");
botChatterChannel = _client.GetChannel(ulong.Parse(config["botChatterChannel"])) as ISocketMessageChannel;
announcementChannel = _client.GetChannel(ulong.Parse(config["announcementChannel"])) as ISocketMessageChannel;
mtgChannel = _client.GetChannel(ulong.Parse(config["mtgChannel"])) as ISocketMessageChannel;
Shared.botChatterChannel = _client.GetChannel(ulong.Parse(config["botChatterChannel"])) as ISocketMessageChannel;
Shared.announcementChannel = _client.GetChannel(ulong.Parse(config["announcementChannel"])) as ISocketMessageChannel;
Shared.mtgChannel = _client.GetChannel(ulong.Parse(config["mtgChannel"])) as ISocketMessageChannel;
_client.MessageReceived += MessageReceived;
_client.UserJoined += UserJoined;
@ -76,13 +72,13 @@ namespace silverworker_discord
}
catch (Exception e)
{
await botChatterChannel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
await Shared.botChatterChannel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
}
}
}
else
{
if (message.Channel.Id == botChatterChannel.Id)
if (message.Channel.Id == Shared.botChatterChannel.Id)
{
if (message.Attachments?.Count > 0)
{
@ -97,110 +93,48 @@ 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 botChatterChannel.SendMessageAsync("convert failed :(");
Console.Error.WriteLine("convert failed :(");
}
}
catch (Exception e)
{
await botChatterChannel.SendMessageAsync(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 botChatterChannel.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 botChatterChannel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
}
File.Delete(path);
}
else
{
Console.Error.WriteLine("idgi but something happened.");
await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>"));
}
}
}
private Task UserJoined(SocketGuildUser arg)
{
Console.WriteLine($"user joined: {arg.Nickname}. Guid: {arg.Guild.Id}. Channel: {arg.Guild.DefaultChannel}");

14
Shared.cs Normal file
View File

@ -0,0 +1,14 @@
using System;
using Discord.WebSocket;
namespace silverworker_discord
{
public class Shared
{
public static ISocketMessageChannel botChatterChannel = null;
public static ISocketMessageChannel announcementChannel = null;
public static ISocketMessageChannel mtgChannel = null;
public static Random r = new Random();
}
}

View File

@ -11,6 +11,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
<PackageReference Include="qrcoder" Version="1.4.2" />
<PackageReference Include="youtubedlsharp" Version="0.3.1" />
</ItemGroup>