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

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