Advertisement
Digitally_Neon

Program.cs - Quick Discord Bot

Jan 20th, 2021
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.39 KB | None | 0 0
  1. using Discord;
  2. using Discord.API;
  3. using Discord.WebSocket;
  4. using HtmlAgilityPack;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Net.Http;
  11. using System.Runtime.InteropServices;
  12. using System.Text;
  13. using System.Threading;
  14. using System.Threading.Tasks;
  15. using System.Reflection;
  16. using System.Diagnostics;
  17. using Discord.Audio;
  18.  
  19. namespace MathProof
  20. {
  21.     class Program
  22.     {
  23.         static void Main(string[] args)
  24.         {
  25.             MainAsync().GetAwaiter().GetResult();
  26.             Task.Delay(-1).GetAwaiter().GetResult();
  27.         }
  28.         public static DiscordSocketClient API;
  29.         static async Task MainAsync()
  30.         {
  31.             var handle = GetConsoleWindow();
  32.  
  33.             // Hide
  34.             //ShowWindow(handle, SW_HIDE);
  35.             // Show
  36.             //ShowWindow(handle, SW_SHOW);
  37.             API = new DiscordSocketClient();
  38.             await API.LoginAsync(TokenType.Bot, "redacted lol");
  39.             await API.StartAsync();
  40.             API.MessageReceived += API_MessageReceived;
  41.         }
  42.         public static string[] CursedTimes = new string[]
  43.         {
  44.             "national-kill-all-anime-pfps-day",
  45.             DateTime.UtcNow.DayOfWeek.ToString(),
  46.             "acab day! :revolving_hearts:"
  47.         };
  48.         private static bool StopAll = false;
  49.         [DllImport("kernel32.dll")]
  50.         static extern IntPtr GetConsoleWindow();
  51.  
  52.         [DllImport("user32.dll")]
  53.         static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  54.  
  55.         const int SW_HIDE = 0;
  56.         const int SW_SHOW = 5;
  57.         private static async Task API_MessageReceived(SocketMessage arg)
  58.         {
  59.             try
  60.             {
  61.                 if (arg.Content.ToLower().StartsWith("alexa, order "))
  62.                 {
  63.                     string WhatToOrder = arg.Content.ToLower().Split(new[] { "alexa, order " }, StringSplitOptions.None)[1];
  64.                     var Dict = new Dictionary<string, string>()
  65.                     {
  66.                         {"q", WhatToOrder},
  67.                         {"tbm", "isch"}
  68.                     };
  69.                     string Q = $"https://www.google.com/search?{ await new FormUrlEncodedContent(Dict).ReadAsStringAsync() }";
  70.                     using (HttpClient Cl = new HttpClient())
  71.                     {
  72.                         var Resp = await Cl.GetAsync(Q);
  73.                         var Data = await Resp.Content.ReadAsStringAsync();
  74.                         HtmlDocument Doc = new HtmlDocument();
  75.                         Doc.LoadHtml(Data);
  76.                         await orderpls(WhatToOrder, arg, Doc, new Random().Next(29, 45));
  77.                     }
  78.                 }
  79.                 else if (arg.Content.ToLower().StartsWith("alexa,") && arg.Content.ToLower().Contains("play"))
  80.                 {
  81.                     bool VCFound = false;
  82.                     foreach(var Vc in ((SocketGuildChannel)arg.Channel).Guild.VoiceChannels)
  83.                     {
  84.                         if(Vc.Users.Contains((SocketGuildUser)arg.Author))
  85.                         {
  86.                             VCFound = true;
  87.                             var client = await Vc.ConnectAsync();
  88.                             string path = @"\ffmpeg.exe";
  89.                             using (var ffmpeg = CreateStream(path))
  90.                             using (var output = ffmpeg.StandardOutput.BaseStream)
  91.                             using (var discord = client.CreatePCMStream(AudioApplication.Mixed))
  92.                             {
  93.                                 try { await output.CopyToAsync(discord); }
  94.                                 finally { await discord.FlushAsync(); }
  95.                             }
  96.                             break;
  97.                         }
  98.                     }
  99.                     if (!VCFound)
  100.                         await arg.Channel.SendMessageAsync("ure not in a vc wtf");
  101.                 }
  102.                 else if (arg.Content.ToLower().StartsWith("alexa,") && (arg.Content.ToLower().Contains("time") || arg.Content.ToLower().Contains("date") || arg.Content.ToLower().Contains("day")))
  103.                 {
  104.                     await arg.Channel.SendMessageAsync($"today is {CursedTimes[new Random().Next(0, CursedTimes.Length - 1)]}");
  105.                 }
  106.                 else if (arg.Content.ToLower().StartsWith("alexa,") && arg.Content.ToLower().Contains("kill me"))
  107.                 {
  108.                     await arg.Channel.SendMessageAsync($"here u go loser; https://en.wikipedia.org/wiki/Suicide_methods");
  109.                 }
  110.                 else if(arg.Content.ToLower().StartsWith("alexa,") && (arg.Content.ToLower().Contains("live") && arg.Content.ToLower().Contains("why")))
  111.                 {
  112.                     await arg.Channel.SendMessageAsync("bc dying is illegal");
  113.                 }
  114.                 else if (arg.Content.ToLower().StartsWith("alexa,") && arg.Content.ToLower().Contains("kpid"))
  115.                 {
  116.                     await arg.Channel.SendMessageAsync($"process of arch {Assembly.GetExecutingAssembly().GetName().ProcessorArchitecture} // {Assembly.GetExecutingAssembly().GetName().Name} killed");
  117.                     Environment.Exit(-1);
  118.                 }
  119.                 else if (arg.Content.ToLower().StartsWith("alexa,") && arg.Content.ToLower().Contains("stop"))
  120.                 {
  121.                     if (arg.Author.Id != 528750326107602965)
  122.                     {
  123.                         await arg.Channel.SendMessageAsync("no u");
  124.                     }
  125.                     else
  126.                     {
  127.                         StopAll = true;
  128.                         new Thread(x =>
  129.                         {
  130.                             Thread.Sleep(10000);
  131.                             StopAll = false;
  132.                         }).Start();
  133.                         await arg.Channel.SendMessageAsync($"ugh fine ill stop, gimme 10 seconds before starting another counting session yeah?\n\n```\nsent message to stop all thread pumps, 10000ms to thread lock release.\n```");
  134.                     }
  135.                 }
  136.                 else if (arg.Content.ToLower().StartsWith("alexa,") && (arg.Content.ToLower().Contains("count") || (arg.Content.ToLower().Contains("go") && arg.Content.ToLower().Contains("to"))))
  137.                 {
  138.                     int Res = 0;
  139.                     for(int i = 0; i < arg.Content.Split(' ').Length; i++)
  140.                     {
  141.                         if(int.TryParse(arg.Content.Split(' ')[i], out Res))
  142.                             break;
  143.                     }
  144.                     if (Res < 0)
  145.                         return;
  146.                     new Thread(async x =>
  147.                     {
  148.                         int i = 0;
  149.                         while (!StopAll && i < Res)
  150.                         {
  151.                             try
  152.                             {
  153.                                 i++;
  154.                                 await arg.Channel.SendMessageAsync(i.ToString());
  155.                             }
  156.                             catch
  157.                             {
  158.                                 Thread.Sleep(15000);
  159.                             }
  160.                             Thread.Sleep(2000);
  161.                         }
  162.                     }).Start();
  163.                 }
  164.             }
  165.             catch(Exception e)
  166.             {
  167.                 await arg.Channel.SendMessageAsync($"i couldnt do that for some reason, sorry :pensive:\n\n```\n{e}\n```");
  168.             }
  169.         }
  170.         public static int Exhaust = 10;
  171.         private static async Task orderpls(string WhatToOrder, SocketMessage arg, HtmlDocument Doc, int StartInd)
  172.         {
  173.             var __Img = (from S in Doc.DocumentNode.SelectNodes(".//a/@href | .//img/@src") where S.InnerLength > 1 select S);
  174.             var _Img = __Img.ElementAt(StartInd);
  175.             try
  176.             {
  177.                 string Img = _Img.InnerHtml.Split(new[] { "src" }, StringSplitOptions.None)[1].Split(new[] { @"""" }, StringSplitOptions.None)[1].Split(new[] { @"""" }, StringSplitOptions.None)[0];
  178.                 await arg.Channel.SendMessageAsync($"ordered {WhatToOrder}.. will be here around {(DateTime.Now + TimeSpan.FromDays(new Random().Next(2, 7))).ToShortDateString()}");
  179.                 using (WebClient WCl = new WebClient())
  180.                 {
  181.                     WCl.DownloadFile(Img, @".\uwu.png");
  182.                     await arg.Channel.SendFileAsync(@".\uwu.png", "ur product:");
  183.                 }
  184.                 File.Delete(@".\uwu.png");
  185.                 Exhaust = 10;
  186.             }
  187.             catch
  188.             {
  189.  
  190.                 if (Exhaust > 0)
  191.                 {
  192.                     Exhaust -= 1;
  193.                     await orderpls(WhatToOrder, arg, Doc, StartInd + 1);
  194.                 }
  195.                 else
  196.                 {
  197.                     Exhaust = 10;
  198.                     await arg.Channel.SendMessageAsync("couldnt order that sorry, must be out of stock - u can always retry tho");
  199.                 }
  200.             }
  201.         }
  202.         public static Process CreateStream(string path)
  203.         {
  204.             return Process.Start(new ProcessStartInfo
  205.             {
  206.                 FileName = "ffmpeg",
  207.                 Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
  208.                 UseShellExecute = false,
  209.                 RedirectStandardOutput = true,
  210.             });
  211.         }
  212.     }
  213. }
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement