Advertisement
Guest User

Untitled

a guest
Aug 9th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.33 KB | None | 0 0
  1. using Discord;
  2. using Google.Apis.Auth.OAuth2;
  3. using Google.Apis.Sheets.v4;
  4. using Google.Apis.Sheets.v4.Data;
  5. using Google.Apis.Services;
  6. using Google.Apis.Util.Store;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14.  
  15. namespace DiscordBot
  16. {
  17.     class Program
  18.     {
  19.         // If modifying these scopes, delete your previously saved credentials
  20.         // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json
  21.         static string[] Scopes = { SheetsService.Scope.SpreadsheetsReadonly };
  22.         static string ApplicationName = "Google Sheets Test";
  23.         static void Main(string[] args)
  24.         {
  25.             var bot = new DiscordClient();
  26.             string ProgramLocation = (System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace("file:///", "").Replace("DiscordBot.EXE", "") + "login.txt");
  27.             string Username;
  28.             string Password;
  29.  
  30.             UserCredential credential;
  31.  
  32.             using (var stream = new FileStream("client_id.json", FileMode.Open, FileAccess.Read))
  33.             {
  34.                 string credPath = System.Environment.GetFolderPath(
  35.                     System.Environment.SpecialFolder.Personal);
  36.                 credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
  37.  
  38.                 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
  39.                     Scopes,
  40.                     "user",
  41.                     CancellationToken.None,
  42.                     new FileDataStore(credPath, true)).Result;
  43.                 Console.WriteLine("Credential file saved to: " + credPath);
  44.             }
  45.  
  46.             var service = new SheetsService(new BaseClientService.Initializer()
  47.             {
  48.                 HttpClientInitializer = credential,
  49.                 ApplicationName = ApplicationName,
  50.             });
  51.  
  52.             string spreadsheetId = "1zT4trzM34tI56_YhFJfIiJYvj5OMCWXDE_WnBiCVMD0";
  53.             string range = "B6:B12";
  54.             SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, range);
  55.  
  56.             ValueRange response = request.Execute();
  57.             IList<IList<Object>> values = response.Values;
  58.             if (values != null && values.Count > 0)
  59.             {
  60.                 Console.WriteLine("SPECIAL, Value");
  61.                 foreach (var row in values)
  62.                 {
  63.                     Console.WriteLine("{0}, {1}", row[1], row[7]);
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 Console.WriteLine("No data found.");
  69.             }
  70.             Console.Read();
  71.             if (!File.Exists(ProgramLocation))
  72.             {
  73.                 if (!File.Exists("C:/login.txt"))
  74.                 {
  75.                     Console.WriteLine("No Login Files Found");
  76.                     Console.WriteLine("Enter Username");
  77.                     Username = Console.ReadLine();
  78.                     Console.WriteLine("Enter Password");
  79.                     Password = "";
  80.                     while (true)
  81.                     {
  82.                         var key = System.Console.ReadKey(true);
  83.                         if (key.Key == ConsoleKey.Enter)
  84.                         { break; }
  85.                         else if (key.Key != ConsoleKey.Backspace)
  86.                         { Console.Write("*"); }
  87.                         Password += key.KeyChar;
  88.                     }
  89.                     Console.WriteLine("Creating Login File in " + ProgramLocation);
  90.                     using (StreamWriter sw = File.CreateText(ProgramLocation))
  91.                     {
  92.                         sw.WriteLine("UserName: " + Username);
  93.                         sw.WriteLine("Password: " + Password);
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     Username = File.ReadLines("C:/login.txt").Skip(0).Take(1).First().Replace(" ", "").Replace("UserName:", "");
  99.                     Password = File.ReadLines("C:/login.txt").Skip(1).Take(1).First().Replace(" ", "").Replace("Password:", "");
  100.                 }
  101.  
  102.             }
  103.             else
  104.             {
  105.                 Username = File.ReadLines(ProgramLocation).Skip(0).Take(1).First().Replace(" ", "").Replace("UserName:", "");
  106.                 Password = File.ReadLines(ProgramLocation).Skip(1).Take(1).First().Replace(" ", "").Replace("Password:", "");
  107.             }
  108.  
  109.             bot.MessageReceived += bot_MessageReceived;
  110.  
  111.             try
  112.             {
  113.                 bot.ExecuteAndWait(async () =>
  114.                 {
  115.                     await bot.Connect(Username, Password);
  116.                     Console.WriteLine("Bot Online, Logged in as " + Username);
  117.                 });
  118.             }
  119.             catch
  120.             {
  121.                 Console.WriteLine("Username or password incorrect.");
  122.                 Console.WriteLine("Press Enter to exit and try again.");
  123.                 Console.WriteLine(Username);
  124.                 Console.WriteLine(Password);
  125.                 File.Delete(ProgramLocation);
  126.                 Console.ReadLine();
  127.                
  128.             }
  129.  
  130.          }
  131.  
  132.         private static void bot_MessageReceived(object sender, MessageEventArgs e)
  133.         {
  134.            
  135.             if (e.Message.IsAuthor) return;
  136.  
  137.             Console.WriteLine("{0} said {1} in {2}", e.User.Name, e.Message.Text, e.Channel.Name);
  138.  
  139.             if (e.Message.Text.ToLower().Contains("!roll"))
  140.             {
  141.                 string str = e.Message.Text;
  142.                 string[] tokens = str.Split(' ');
  143.                 double numTest;
  144.                 try
  145.                 {
  146.                     if (Double.TryParse(tokens[1], out numTest) && Double.TryParse(tokens[2], out numTest))
  147.                     {
  148.                         int DiceNumber = Convert.ToInt32(tokens[1]);
  149.                         int RollNumber = Convert.ToInt32(tokens[2]);
  150.                         string message = DND.rollDice(DiceNumber, RollNumber);
  151.                         e.Channel.SendMessage(message);
  152.                         //e.Channel.SendMessage("Roll a " + DiceNumber + " Sided Dice " + RollNumber + " Times");
  153.                     }
  154.                     else
  155.                     {
  156.                         e.Channel.SendMessage("Invalid arguments. Use '!roll (Dice Sides) (number of Rolls)' no parenthesis. 1");
  157.                     }
  158.                 }
  159.                 catch
  160.                 {
  161.                     e.Channel.SendMessage("Invalid arguments. Use '!roll (Dice Sides) (number of Rolls)' no parenthesis. ");
  162.                 }
  163.                
  164.                
  165.                
  166.             }
  167.             if (e.Message.Text.ToLower().Contains("!battle"))
  168.             {
  169.                 string battleOrder;
  170.                 string[] tokens = e.Message.Text.Split(' ');
  171.                 List<string> Validate = new List<string>(tokens);
  172.                 if (Validate.Count > 2)
  173.                 {
  174.                     battleOrder = DND.battlesequence(e.Message.Text);
  175.                 }
  176.                 else
  177.                 {
  178.                     battleOrder = "Please include at least 2 players/npcs";
  179.                 }
  180.                 e.Channel.SendMessage(battleOrder);
  181.             }          
  182.         }
  183.     }
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement