2021-06-11 23:12:10 -04:00
|
|
|
|
using System;
|
|
|
|
|
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;
|
2021-07-16 03:49:37 -04:00
|
|
|
|
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-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(() =>
|
|
|
|
|
{
|
2021-07-14 09:59:00 -04:00
|
|
|
|
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;
|
2021-07-14 09:59:00 -04:00
|
|
|
|
|
|
|
|
|
_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;
|
|
|
|
|
|
2021-07-14 09:59:00 -04:00
|
|
|
|
Console.WriteLine($"{message.Channel}, {message.Content} (message id: {message.Id})");
|
|
|
|
|
|
2021-08-29 00:21:09 -04:00
|
|
|
|
if (message.Author.IsWebhook)
|
2021-07-14 09:59:00 -04:00
|
|
|
|
{
|
2021-08-29 00:21:09 -04:00
|
|
|
|
if (message.Author.Username == "greasemonkey reward watcher")
|
2021-07-14 09:59:00 -04:00
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("yep");
|
|
|
|
|
var redemptionData = message.Content.Split("\n")[1].Substring("data: ".Length);
|
|
|
|
|
|
2021-08-29 00:21:09 -04:00
|
|
|
|
if (message.Content.StartsWith("type: reward-request"))
|
2021-07-14 09:59:00 -04:00
|
|
|
|
{
|
|
|
|
|
var components = redemptionData.Split("•");
|
|
|
|
|
Console.WriteLine($"{components.Length} components:");
|
|
|
|
|
var rewardName = components[0].Trim();
|
|
|
|
|
var redeemer = components[1].Trim();
|
|
|
|
|
var textData = "";
|
2021-08-29 00:21:09 -04:00
|
|
|
|
if (components[1].Contains(":"))
|
2021-07-14 09:59:00 -04:00
|
|
|
|
{
|
|
|
|
|
redeemer = components[1].Substring(0, components[1].IndexOf(":")).Trim();
|
|
|
|
|
textData = components[1].Substring(components[1].IndexOf(":")).Trim();
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine($"user: {redeemer} redeems {rewardName}, text data? {textData}");
|
2021-08-29 00:21:09 -04:00
|
|
|
|
|
2021-07-16 03:49:37 -04:00
|
|
|
|
var redemptionSerialized = Encoding.ASCII.GetBytes(
|
2021-08-29 00:21:09 -04:00
|
|
|
|
JsonConvert.SerializeObject(new
|
|
|
|
|
{
|
2021-07-16 03:49:37 -04:00
|
|
|
|
redeemer = redeemer,
|
|
|
|
|
rewardName = rewardName,
|
|
|
|
|
textData = textData
|
|
|
|
|
}, Formatting.None));
|
|
|
|
|
var wr = WebRequest.Create("http://192.168.1.151:3001/shortcuts/redeemReward");
|
|
|
|
|
wr.Method = "POST";
|
|
|
|
|
wr.ContentType = "application/json";
|
|
|
|
|
wr.ContentLength = redemptionSerialized.Length;
|
2021-08-29 00:21:09 -04:00
|
|
|
|
using (var postStream = wr.GetRequestStream())
|
2021-07-16 03:49:37 -04:00
|
|
|
|
{
|
|
|
|
|
postStream.Write(redemptionSerialized);
|
|
|
|
|
}
|
|
|
|
|
await wr.GetResponseAsync();
|
2021-07-14 09:59:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-06-11 23:12:10 -04:00
|
|
|
|
{
|
2021-07-14 09:59:00 -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
|
|
|
|
{
|
2021-07-14 09:59:00 -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);
|
|
|
|
|
Console.WriteLine($"{wordLikes.Count()} word-like things");
|
|
|
|
|
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")
|
|
|
|
|
{
|
|
|
|
|
detiktokify(link, message.Channel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-11 23:12:10 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-29 00:21:09 -04:00
|
|
|
|
private async void detiktokify(Uri link, ISocketMessageChannel channel)
|
|
|
|
|
{
|
|
|
|
|
var ytdl = new YoutubeDLSharp.YoutubeDL();
|
|
|
|
|
ytdl.YoutubeDLPath = config["ytdl"];
|
|
|
|
|
ytdl.FFmpegPath = "ffmpeg";
|
|
|
|
|
ytdl.OutputFolder = "./";
|
|
|
|
|
var res = await ytdl.RunVideoDownload(link.ToString());
|
|
|
|
|
string path = res.Data;
|
|
|
|
|
await channel.SendFileAsync(path);
|
|
|
|
|
File.Delete(path);
|
|
|
|
|
}
|
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>");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|