Advertisement
Guest User

Untitled

a guest
Aug 7th, 2021
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using DiscordRPC;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace Chilong
  8. {
  9.     class Program
  10.  
  11.     {
  12.         private static DiscordRpcClient client;
  13.         private static Process cdisplayex;
  14.         private static string windowTitle;
  15.  
  16.         [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  17.         public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  18.  
  19.         [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  20.         public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
  21.  
  22.         public const int WM_GETTEXT = 0x000D;
  23.         public const int WM_GETTEXTLENGTH = 0x000E;
  24.  
  25.         [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  26.         public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, IntPtr lParam);
  27.  
  28.         [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  29.         public static extern int SendMessage(IntPtr hWnd, uint msg, int wParam, StringBuilder lParam);
  30.  
  31.         static void Main(string[] args)
  32.         {
  33.             Initialize();
  34.             if (Process.GetProcesses().Where(x => x.ProcessName.StartsWith("CDisplayEx")).Count() == 0)
  35.                 return;
  36.  
  37.             while (true)
  38.             {
  39.                 Update();
  40.                 if (Process.GetProcesses().Where(x => x.ProcessName.StartsWith("CDisplayEx")).Count() == 0)
  41.                 {
  42.                     Deinitialize();
  43.                     Console.WriteLine("Thanks for using Chilong!");
  44.                     return;
  45.                 }
  46.             }
  47.         }
  48.  
  49.         private static void Initialize()
  50.         {
  51.             client = new DiscordRpcClient("815595590515032074");
  52.  
  53.             if(Process.GetProcesses().Where(x => x.ProcessName.StartsWith("CDisplayEx")).Count() == 0)
  54.             {
  55.                 Console.WriteLine("SumatraPDF was not found! Is it open?");
  56.                 return;
  57.             }
  58.             cdisplayex = Process.GetProcesses().Where(x => x.ProcessName.StartsWith("CDisplayEx")).ToList()[0];
  59.             windowTitle = cdisplayex.MainWindowTitle;
  60.  
  61.             client.OnReady += (sender, e) =>
  62.             {
  63.                 Console.WriteLine("Received Ready from user {0}", e.User.Username);
  64.             };
  65.             client.OnPresenceUpdate += (sender, e) =>
  66.             {
  67.                 Console.WriteLine("Received Update! {0}", e.Presence);
  68.             };
  69.  
  70.             try
  71.             {
  72.                 client.Initialize();
  73.                 Console.WriteLine("Successfully connected to client!");
  74.             }
  75.             catch(Exception e)
  76.             {
  77.                 Console.WriteLine($"Connection to client was not successful!\nERROR: {e.Message}");
  78.                 return;
  79.             }
  80.  
  81.             try { SetNewPresence(); }
  82.             catch(Exception e)
  83.             {
  84.                 Console.WriteLine($"Setting presence was not successful!\nERROR: {e.Message}");
  85.                 return;
  86.             }
  87.         }
  88.         private static void Update()
  89.         {
  90.             client.OnPresenceUpdate += (sender, e) =>
  91.             {
  92.                 Console.WriteLine("Received Update! {0}", e.Presence);
  93.             };
  94.             client.Invoke();
  95.             OnUpdate();
  96.         }
  97.         private static void Deinitialize()
  98.         {
  99.             client.ClearPresence();
  100.             client.Dispose();
  101.         }
  102.  
  103.         private static void OnUpdate()
  104.         {
  105.             Process process;
  106.             try
  107.             {
  108.                 process = Process.GetProcesses().Where(x => x.ProcessName.StartsWith("CDisplayEx")).ToList()[0];
  109.             }
  110.             catch(Exception) { return; }
  111.  
  112.             if (process.MainWindowTitle != windowTitle)
  113.             {
  114.                 cdisplayex = process;
  115.                 windowTitle = cdisplayex.MainWindowTitle;
  116.                 SetNewPresence();
  117.             }
  118.         }
  119.  
  120.         private static void SetNewPresence()
  121.         {
  122.             string details = cdisplayex.MainWindowTitle;
  123.             if(details == " " || details == "")
  124.                 details = "Picking a New File";
  125.  
  126.             client.SetPresence(new RichPresence
  127.             {
  128.                 Details = details,
  129.                 State = "State Placeholder",
  130.                 Timestamps = new Timestamps(DateTime.UtcNow),
  131.                 Assets = new Assets()
  132.                 {
  133.                     LargeImageKey = "cdisplayex",
  134.                     LargeImageText = "cdisplayex"
  135.                 }
  136.             });
  137.             Console.WriteLine("Presence successfully set!");
  138.         }
  139.     }
  140.  
  141.     public class StringBuilder
  142.     {
  143.         public StringBuilder(int v)
  144.         {
  145.             V = v;
  146.         }
  147.  
  148.         public int V { get; }
  149.         public int Capacity { get; internal set; }
  150.     }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement