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 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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("Combot-IRC-Bot"));
  32. if (GetOptionValue("Token").ToString() != string.Empty)
  33. {
  34. string token = GetOptionValue("Token").ToString();
  35. Credentials creds = new Credentials(token);
  36. github.Credentials = creds;
  37. }
  38. SearchUsersResult foundUser = await github.Search.SearchUsers(new SearchUsersRequest(username));
  39. if (foundUser.TotalCount > 0)
  40. {
  41. User user = await github.User.Get(foundUser.Items.First().Login);
  42. if (command.Arguments.ContainsKey("Repository"))
  43. {
  44. string repo = command.Arguments["Repository"];
  45. try
  46. {
  47. Repository foundRepo = await github.Repository.Get(user.Login, repo);
  48. if (foundRepo != null)
  49. {
  50. 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);
  51. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repoMessage);
  52. if (foundRepo.Description != string.Empty)
  53. {
  54. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, foundRepo.Description);
  55. }
  56. }
  57. else
  58. {
  59. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Invalid Repository Name");
  60. }
  61. }
  62. catch (Octokit.NotFoundException ex)
  63. {
  64. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Invalid Repository Name");
  65. }
  66. }
  67. else
  68. {
  69. 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);
  70. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, userMessage);
  71. }
  72. }
  73. else
  74. {
  75. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Github user \u0002{0}\u0002 does not exist.", username));
  76. }
  77. }
  78. catch (Octokit.RateLimitExceededException ex)
  79. {
  80. OnError(ex.Message);
  81. }
  82. catch (Octokit.ApiValidationException ex)
  83. {
  84. OnError(ex.Message);
  85. }
  86. }
  87. private async void GetRepositoryInformation(CommandMessage command)
  88. {
  89. try
  90. {
  91. GitHubClient github = new GitHubClient(new ProductHeaderValue("CombotIRCBot"));
  92. if (GetOptionValue("Token").ToString() != string.Empty)
  93. {
  94. string token = GetOptionValue("Token").ToString();
  95. github.Credentials = new Credentials(token);
  96. }
  97. SearchRepositoryResult foundRepo = await github.Search.SearchRepo(new SearchRepositoriesRequest(command.Arguments["Repository"]));
  98. if (foundRepo.TotalCount > 0)
  99. {
  100. Repository repo = foundRepo.Items.First();
  101. 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);
  102. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repoMessage);
  103. if (repo.Description != string.Empty)
  104. {
  105. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repo.Description);
  106. }
  107. }
  108. else
  109. {
  110. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Repository \u0002{0}\u0002 does not exist.", command.Arguments["Repository"]));
  111. }
  112. }
  113. catch (Octokit.RateLimitExceededException ex)
  114. {
  115. OnError(ex.Message);
  116. }
  117. catch (Octokit.ApiValidationException ex)
  118. {
  119. OnError(ex.Message);
  120. return;
  121. }
  122. }
  123. }
  124. }