discord-bot/Program.cs

216 lines
9.0 KiB
C#
Raw Normal View History

//https://discord.com/api/oauth2/authorize?client_id={application id}&permissions=0&scope=bot%20applications.commands
using System;
2021-06-11 23:12:10 -04:00
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System.Threading.Tasks;
using Discord;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using System.Text;
2021-06-11 23:12:10 -04:00
namespace silverworker_discord
{
class Program
{
private DiscordSocketClient _client;
IConfigurationRoot config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
2021-08-29 00:21:09 -04:00
2021-06-11 23:31:37 -04:00
private ISocketMessageChannel botChatterChannel = null;
private ISocketMessageChannel announcementChannel = null;
2021-11-05 09:49:59 -04:00
private ISocketMessageChannel mtgChannel = null;
2021-06-11 23:12:10 -04:00
public static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
public async Task MainAsync()
{
_client = new DiscordSocketClient();
_client.Log += Log;
await _client.LoginAsync(TokenType.Bot, config["token"]);
await _client.StartAsync();
2021-08-29 00:21:09 -04:00
_client.Ready += () => Task.Run(() =>
{
Console.WriteLine("Bot is connected! going to sign up for message received and user joined in client ready");
2021-06-11 23:31:37 -04:00
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;
_client.MessageReceived += MessageReceived;
_client.UserJoined += UserJoined;
2021-08-29 00:21:09 -04:00
});
2021-06-11 23:12:10 -04:00
// Block this task until the program is closed.
await Task.Delay(-1);
}
private async Task MessageReceived(SocketMessage messageParam)
{
var message = messageParam as SocketUserMessage;
if (message == null) return;
if (message.Author.Id == _client.CurrentUser.Id) return;
Console.WriteLine($"{message.Channel}, {message.Content} (message id: {message.Id})");
2021-08-29 00:21:09 -04:00
if (message.Author.IsWebhook)
{
2021-08-29 00:21:09 -04:00
if (message.Author.Username == "greasemonkey reward watcher")
{
Console.WriteLine("heard greasemonkey, this is bananas");
var type = message.Content.Split("\n")[0].Substring("type: ".Length);
var subData = message.Content.Split("\n")[1].Substring("data: ".Length);
try
{
await twitchery.twitcherize(type, subData);
}
catch (Exception e)
{
await botChatterChannel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
}
}
}
else
2021-06-11 23:12:10 -04:00
{
if (message.Channel.Id == botChatterChannel.Id)
2021-06-11 23:12:10 -04:00
{
2021-08-29 00:21:09 -04:00
if (message.Attachments?.Count > 0)
2021-06-11 23:31:37 -04:00
{
Console.WriteLine(message.Attachments.Count);
foreach (var att in message.Attachments)
{
Console.WriteLine(att.Url);
await WebRequest.Create("http://192.168.1.151:3001/shortcuts?display_url=" + att.Url).GetResponseAsync();
}
2021-06-11 23:31:37 -04:00
}
2021-06-11 23:12:10 -04:00
}
2021-08-29 00:21:09 -04:00
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)
{
foreach (var link in links)
{
if (link.Host == "vm.tiktok.com")
{
2021-08-29 05:47:55 -04:00
detiktokify(link, message);
2021-08-29 00:21:09 -04:00
}
}
}
2021-11-17 21:28:26 -05:00
if (message.Attachments?.Count > 0)
{
Console.WriteLine($"{message.Attachments.Count} attachments");
var appleReactions = false;
foreach (var att in message.Attachments)
{
if (att.Filename?.EndsWith(".heic") == true)
{
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
2021-11-17 21:28:26 -05:00
message.AddReactionAsync(new Emoji("\U0001F34F"));
#pragma warning restore 4014
}
}
2021-08-29 00:21:09 -04:00
}
2021-06-11 23:12:10 -04:00
}
}
2021-11-17 21:28:26 -05:00
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"))
2021-11-17 21:28:26 -05:00
{
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 :(");
2021-11-17 21:28:26 -05:00
}
}
catch (Exception e)
{
await botChatterChannel.SendMessageAsync(JsonConvert.SerializeObject(e, Formatting.Indented));
Console.Error.WriteLine(JsonConvert.SerializeObject(e, Formatting.Indented));
2021-11-17 21:28:26 -05:00
}
}
2021-08-29 05:47:55 -04:00
private async void detiktokify(Uri link, SocketUserMessage message)
2021-08-29 00:21:09 -04:00
{
var ytdl = new YoutubeDLSharp.YoutubeDL();
2021-08-29 05:47:55 -04:00
ytdl.YoutubeDLPath = "youtube-dl";
2021-08-29 00:21:09 -04:00
ytdl.FFmpegPath = "ffmpeg";
2021-08-29 05:00:46 -04:00
ytdl.OutputFolder = "";
ytdl.OutputFileTemplate = "tiktokbad.%(ext)s";
2021-08-29 00:21:09 -04:00
var res = await ytdl.RunVideoDownload(link.ToString());
if (!res.Success)
2021-08-29 05:47:55 -04:00
{
2021-08-29 05:50:48 -04:00
Console.Error.WriteLine("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput));
2021-11-05 09:49:59 -04:00
await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>"));
await botChatterChannel.SendMessageAsync("tried to dl, failed. \n" + string.Join('\n', res.ErrorOutput));
2021-08-29 05:47:55 -04:00
}
else
{
string path = res.Data;
if (File.Exists(path))
2021-08-29 05:47:55 -04:00
{
2021-10-24 08:36:26 -04:00
try
{
await message.Channel.SendFileAsync(path);
}
catch (Exception e)
2021-10-24 08:36:26 -04:00
{
await botChatterChannel.SendMessageAsync($"aaaadam!\n{JsonConvert.SerializeObject(e)}");
2021-10-24 08:36:26 -04:00
}
2021-08-29 05:47:55 -04:00
File.Delete(path);
}
else
{
Console.Error.WriteLine("idgi but something happened.");
2021-11-05 09:49:59 -04:00
await message.AddReactionAsync(Emote.Parse("<:problemon:859453047141957643>"));
2021-08-29 05:47:55 -04:00
}
}
2021-08-29 00:21:09 -04:00
}
2021-06-11 23:12:10 -04:00
private Task UserJoined(SocketGuildUser arg)
{
Console.WriteLine($"user joined: {arg.Nickname}. Guid: {arg.Guild.Id}. Channel: {arg.Guild.DefaultChannel}");
var abbreviatedNickname = arg.Nickname;
2021-08-29 00:21:09 -04:00
if (arg.Nickname.Length > 3)
{
2021-06-11 23:12:10 -04:00
abbreviatedNickname = arg.Nickname.Substring(0, arg.Nickname.Length / 3);
}
Console.WriteLine($"imma call him {abbreviatedNickname}");
return arg.Guild.DefaultChannel.SendMessageAsync($"oh hey {abbreviatedNickname}- IPLAYTHESEALOFORICHALCOS <:ORICHALCOS:852749196633309194>");
}
}
}