The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Github.cs 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Octokit;
  5. namespace Combot.Modules.Plugins
  6. {
  7. public class Github : Module
  8. {
  9. public override void Initialize()
  10. {
  11. Bot.CommandReceivedEvent += HandleCommandEvent;
  12. }
  13. public override void ParseCommand(CommandMessage command)
  14. {
  15. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  16. switch (foundCommand.Name)
  17. {
  18. case "Github Information":
  19. GetGithubInformation(command);
  20. break;
  21. case "Repository Information":
  22. GetRepositoryInformation(command);
  23. break;
  24. }
  25. }
  26. private async void GetGithubInformation(CommandMessage command)
  27. {
  28. try
  29. {
  30. string username = (command.Arguments.ContainsKey("Username")) ? command.Arguments["Username"] : command.Nick.Nickname;
  31. GitHubClient github = new GitHubClient(new ProductHeaderValue("CombotIRCBot"));
  32. if (GetOptionValue("Username").ToString() != string.Empty && GetOptionValue("Password").ToString() != string.Empty)
  33. {
  34. github.Credentials = new Credentials(GetOptionValue("Username").ToString(), GetOptionValue("Password").ToString());
  35. NewAuthorization newAuth = new NewAuthorization();
  36. newAuth.Note = "Get Git Information";
  37. try
  38. {
  39. Authorization auth = await github.Authorization.Create(newAuth);
  40. }
  41. catch (Octokit.AuthorizationException ex)
  42. {
  43. OnError(ex.Message);
  44. return;
  45. }
  46. }
  47. SearchUsersResult foundUser = await github.Search.SearchUsers(new SearchUsersRequest(username));
  48. if (foundUser.TotalCount > 0)
  49. {
  50. User user = await github.User.Get(foundUser.Items.First().Login);
  51. if (command.Arguments.ContainsKey("Repository ID"))
  52. {
  53. string repo = command.Arguments["Repository ID"];
  54. int id;
  55. if (int.TryParse(repo, out id))
  56. {
  57. IReadOnlyList<Repository> repos = await github.Repository.GetAllForUser(user.Login);
  58. if (repos != null && repos.Count >= id && id > 0)
  59. {
  60. Repository foundRepo = repos[id - 1];
  61. string repoMessage = string.Format("\u0002{0}\u0002 | Created On \u0002{1}\u0002 | \u0002{2}\u0002 Open Issues | \u0002{3}\u0002 Forks | \u0002{4}\u0002 Stargazers | {5}", foundRepo.FullName, foundRepo.CreatedAt.ToString("d"), foundRepo.OpenIssuesCount, foundRepo.ForksCount, foundRepo.StargazersCount, foundRepo.HtmlUrl);
  62. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repoMessage);
  63. if (foundRepo.Description != string.Empty)
  64. {
  65. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, foundRepo.Description);
  66. }
  67. }
  68. else
  69. {
  70. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Invalid Repository ID");
  71. }
  72. }
  73. else
  74. {
  75. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Invalid Repository ID");
  76. }
  77. }
  78. else
  79. {
  80. string userMessage = string.Format("\u0002{0}\u0002 - \u0002{1}\u0002 Followers - Following \u0002{2}\u0002 Users - \u0002{3}\u0002 Repositories | {4}", user.Login, user.Followers, user.Following, user.PublicRepos, user.HtmlUrl);
  81. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, userMessage);
  82. }
  83. }
  84. else
  85. {
  86. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Github user \u0002{0}\u0002 does not exist.", username));
  87. }
  88. }
  89. catch (Octokit.RateLimitExceededException ex)
  90. {
  91. OnError(ex.Message);
  92. }
  93. }
  94. private async void GetRepositoryInformation(CommandMessage command)
  95. {
  96. try
  97. {
  98. GitHubClient github = new GitHubClient(new ProductHeaderValue("CombotIRCBot"));
  99. if (GetOptionValue("Username").ToString() != string.Empty && GetOptionValue("Password").ToString() != string.Empty)
  100. {
  101. github.Credentials = new Credentials(GetOptionValue("Username").ToString(), GetOptionValue("Password").ToString());
  102. NewAuthorization newAuth = new NewAuthorization();
  103. newAuth.Note = "Get Git Information";
  104. try
  105. {
  106. Authorization auth = await github.Authorization.Create(newAuth);
  107. }
  108. catch (Octokit.AuthorizationException ex)
  109. {
  110. OnError(ex.Message);
  111. return;
  112. }
  113. }
  114. SearchRepositoryResult foundRepo = await github.Search.SearchRepo(new SearchRepositoriesRequest(command.Arguments["Repository"]));
  115. if (foundRepo.TotalCount > 0)
  116. {
  117. Repository repo = foundRepo.Items.First();
  118. string repoMessage = string.Format("\u0002{0}\u0002 | Created On \u0002{1}\u0002 | \u0002{2}\u0002 Open Issues | \u0002{3}\u0002 Forks | \u0002{4}\u0002 Stargazers | {5}", repo.FullName, repo.CreatedAt.ToString("d"), repo.OpenIssuesCount, repo.ForksCount, repo.StargazersCount, repo.HtmlUrl);
  119. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repoMessage);
  120. if (repo.Description != string.Empty)
  121. {
  122. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repo.Description);
  123. }
  124. }
  125. else
  126. {
  127. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Repository \u0002{0}\u0002 does not exist.", command.Arguments["Repository"]));
  128. }
  129. }
  130. catch (Octokit.RateLimitExceededException ex)
  131. {
  132. OnError(ex.Message);
  133. }
  134. }
  135. }
  136. }