forked from Uncled1023/Combot
Browse Source
Added error events for modules and database. Fixed command empty string exception.master
12 changed files with 502 additions and 26 deletions
Binary file not shown.
@ -0,0 +1,141 @@
@@ -0,0 +1,141 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using Octokit; |
||||
|
||||
namespace Combot.Modules.Plugins |
||||
{ |
||||
public class Github : Module |
||||
{ |
||||
public override void Initialize() |
||||
{ |
||||
Bot.CommandReceivedEvent += HandleCommandEvent; |
||||
} |
||||
|
||||
public override void ParseCommand(CommandMessage command) |
||||
{ |
||||
Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command)); |
||||
|
||||
switch (foundCommand.Name) |
||||
{ |
||||
case "Github Information": |
||||
GetGithubInformation(command); |
||||
break; |
||||
case "Repository Information": |
||||
GetRepositoryInformation(command); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
private async void GetGithubInformation(CommandMessage command) |
||||
{ |
||||
try |
||||
{ |
||||
string username = (command.Arguments.ContainsKey("Username")) ? command.Arguments["Username"] : command.Nick.Nickname; |
||||
GitHubClient github = new GitHubClient(new ProductHeaderValue("CombotIRCBot")); |
||||
if (GetOptionValue("Username").ToString() != string.Empty && GetOptionValue("Password").ToString() != string.Empty) |
||||
{ |
||||
github.Credentials = new Credentials(GetOptionValue("Username").ToString(), GetOptionValue("Password").ToString()); |
||||
NewAuthorization newAuth = new NewAuthorization(); |
||||
newAuth.Note = "Get Git Information"; |
||||
try |
||||
{ |
||||
Authorization auth = await github.Authorization.Create(newAuth); |
||||
} |
||||
catch (Octokit.AuthorizationException ex) |
||||
{ |
||||
OnError(ex.Message); |
||||
return; |
||||
} |
||||
} |
||||
SearchUsersResult foundUser = await github.Search.SearchUsers(new SearchUsersRequest(username)); |
||||
if (foundUser.TotalCount > 0) |
||||
{ |
||||
User user = await github.User.Get(foundUser.Items.First().Login); |
||||
if (command.Arguments.ContainsKey("Repository ID")) |
||||
{ |
||||
string repo = command.Arguments["Repository ID"]; |
||||
int id; |
||||
if (int.TryParse(repo, out id)) |
||||
{ |
||||
IReadOnlyList<Repository> repos = await github.Repository.GetAllForUser(user.Login); |
||||
if (repos != null && repos.Count >= id && id > 0) |
||||
{ |
||||
Repository foundRepo = repos[id - 1]; |
||||
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); |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repoMessage); |
||||
if (foundRepo.Description != string.Empty) |
||||
{ |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, foundRepo.Description); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Invalid Repository ID"); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, "Invalid Repository ID"); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
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); |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, userMessage); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Github user \u0002{0}\u0002 does not exist.", username)); |
||||
} |
||||
} |
||||
catch (Octokit.RateLimitExceededException ex) |
||||
{ |
||||
OnError(ex.Message); |
||||
} |
||||
} |
||||
|
||||
private async void GetRepositoryInformation(CommandMessage command) |
||||
{ |
||||
try |
||||
{ |
||||
GitHubClient github = new GitHubClient(new ProductHeaderValue("CombotIRCBot")); |
||||
if (GetOptionValue("Username").ToString() != string.Empty && GetOptionValue("Password").ToString() != string.Empty) |
||||
{ |
||||
github.Credentials = new Credentials(GetOptionValue("Username").ToString(), GetOptionValue("Password").ToString()); |
||||
NewAuthorization newAuth = new NewAuthorization(); |
||||
newAuth.Note = "Get Git Information"; |
||||
try |
||||
{ |
||||
Authorization auth = await github.Authorization.Create(newAuth); |
||||
} |
||||
catch (Octokit.AuthorizationException ex) |
||||
{ |
||||
OnError(ex.Message); |
||||
return; |
||||
} |
||||
} |
||||
SearchRepositoryResult foundRepo = await github.Search.SearchRepo(new SearchRepositoriesRequest(command.Arguments["Repository"])); |
||||
if (foundRepo.TotalCount > 0) |
||||
{ |
||||
Repository repo = foundRepo.Items.First(); |
||||
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); |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repoMessage); |
||||
if (repo.Description != string.Empty) |
||||
{ |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, repo.Description); |
||||
} |
||||
} |
||||
else |
||||
{ |
||||
SendResponse(command.MessageType, command.Location, command.Nick.Nickname, string.Format("Repository \u0002{0}\u0002 does not exist.", command.Arguments["Repository"])); |
||||
} |
||||
} |
||||
catch (Octokit.RateLimitExceededException ex) |
||||
{ |
||||
OnError(ex.Message); |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> |
||||
<PropertyGroup> |
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
||||
<ProjectGuid>{7D7B5FCB-C9CC-4CB1-B5EF-827BD3398E39}</ProjectGuid> |
||||
<OutputType>Library</OutputType> |
||||
<AppDesignerFolder>Properties</AppDesignerFolder> |
||||
<RootNamespace>Github</RootNamespace> |
||||
<AssemblyName>Github</AssemblyName> |
||||
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion> |
||||
<FileAlignment>512</FileAlignment> |
||||
<TargetFrameworkProfile /> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
||||
<DebugSymbols>true</DebugSymbols> |
||||
<DebugType>full</DebugType> |
||||
<Optimize>false</Optimize> |
||||
<OutputPath>..\..\Bin\Debug\Modules\Github\</OutputPath> |
||||
<DefineConstants>DEBUG;TRACE</DefineConstants> |
||||
<ErrorReport>prompt</ErrorReport> |
||||
<WarningLevel>4</WarningLevel> |
||||
</PropertyGroup> |
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
||||
<DebugType>pdbonly</DebugType> |
||||
<Optimize>true</Optimize> |
||||
<OutputPath>..\..\Bin\Release\Modules\Github\</OutputPath> |
||||
<DefineConstants>TRACE</DefineConstants> |
||||
<ErrorReport>prompt</ErrorReport> |
||||
<WarningLevel>4</WarningLevel> |
||||
</PropertyGroup> |
||||
<ItemGroup> |
||||
<Reference Include="Octokit"> |
||||
<HintPath>..\..\packages\Octokit.0.6.2\lib\net45\Octokit.dll</HintPath> |
||||
</Reference> |
||||
<Reference Include="System" /> |
||||
<Reference Include="System.Core" /> |
||||
<Reference Include="System.Net.Http" /> |
||||
<Reference Include="System.Xml.Linq" /> |
||||
<Reference Include="System.Data.DataSetExtensions" /> |
||||
<Reference Include="Microsoft.CSharp" /> |
||||
<Reference Include="System.Data" /> |
||||
<Reference Include="System.Xml" /> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<Compile Include="Github.cs" /> |
||||
<Compile Include="Properties\AssemblyInfo.cs" /> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<ProjectReference Include="..\..\Combot\Combot.csproj"> |
||||
<Project>{23e4c371-16e4-4fac-8b11-44288399bb55}</Project> |
||||
<Name>Combot</Name> |
||||
</ProjectReference> |
||||
<ProjectReference Include="..\..\IRCServices\IRCServices.csproj"> |
||||
<Project>{65fcbf1c-8c9e-4688-becc-185d9030899f}</Project> |
||||
<Name>IRCServices</Name> |
||||
</ProjectReference> |
||||
</ItemGroup> |
||||
<ItemGroup> |
||||
<None Include="Module.config"> |
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory> |
||||
<SubType>Designer</SubType> |
||||
</None> |
||||
<None Include="packages.config" /> |
||||
</ItemGroup> |
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
||||
<PropertyGroup> |
||||
<PostBuildEvent>md "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)" |
||||
copy /Y "$(TargetPath)" "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)" |
||||
copy /Y "$(TargetDir)Module.config" "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)" |
||||
copy /Y "$(TargetDir)Octokit.dll" "$(SolutionDir)$(ConfigurationName)\Modules\$(TargetName)"</PostBuildEvent> |
||||
</PropertyGroup> |
||||
<Import Project="..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets" Condition="Exists('..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets')" /> |
||||
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> |
||||
<PropertyGroup> |
||||
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText> |
||||
</PropertyGroup> |
||||
<Error Condition="!Exists('..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\GitVersionTask.2.0.1\Build\GitVersionTask.targets'))" /> |
||||
</Target> |
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. |
||||
Other similar extension points exist, see Microsoft.Common.targets. |
||||
<Target Name="BeforeBuild"> |
||||
</Target> |
||||
<Target Name="AfterBuild"> |
||||
</Target> |
||||
--> |
||||
</Project> |
@ -0,0 +1,114 @@
@@ -0,0 +1,114 @@
|
||||
{ |
||||
"Name": "Github", |
||||
"ClassName": "Github", |
||||
"Enabled": true, |
||||
"ChannelBlacklist": [], |
||||
"NickBlacklist": [], |
||||
"Commands": [ |
||||
{ |
||||
"Name": "Github Information", |
||||
"Description": "Gets a nicks Github information.", |
||||
"Enabled": true, |
||||
"ChannelBlacklist": [], |
||||
"NickBlacklist": [], |
||||
"Triggers": [ |
||||
"git", |
||||
"github" |
||||
], |
||||
"Arguments": [ |
||||
{ |
||||
"Name": "Username", |
||||
"Description": "The Github username for the information you want.", |
||||
"AllowedValues": [], |
||||
"DependentArguments": [], |
||||
"MessageTypes": [ |
||||
0, |
||||
1, |
||||
2 |
||||
], |
||||
"Required": false |
||||
}, |
||||
{ |
||||
"Name": "Repository ID", |
||||
"Description": "The repository you want information on.", |
||||
"AllowedValues": [], |
||||
"MessageTypes": [ |
||||
0, |
||||
1, |
||||
2 |
||||
], |
||||
"Required": false |
||||
} |
||||
], |
||||
"AllowedMessageTypes": [ |
||||
0, |
||||
1, |
||||
2 |
||||
], |
||||
"AllowedAccess": [ |
||||
0, |
||||
1, |
||||
2, |
||||
3, |
||||
4, |
||||
5, |
||||
6 |
||||
], |
||||
"ShowHelp": true, |
||||
"SpamCheck": true |
||||
}, |
||||
{ |
||||
"Name": "Repository Information", |
||||
"Description": "Search for a Github repository.", |
||||
"Enabled": true, |
||||
"ChannelBlacklist": [], |
||||
"NickBlacklist": [], |
||||
"Triggers": [ |
||||
"repo", |
||||
"repository" |
||||
], |
||||
"Arguments": [ |
||||
{ |
||||
"Name": "Repository", |
||||
"Description": "The name of the repository you want to view.", |
||||
"AllowedValues": [], |
||||
"DependentArguments": [], |
||||
"MessageTypes": [ |
||||
0, |
||||
1, |
||||
2 |
||||
], |
||||
"Required": true |
||||
} |
||||
], |
||||
"AllowedMessageTypes": [ |
||||
0, |
||||
1, |
||||
2 |
||||
], |
||||
"AllowedAccess": [ |
||||
0, |
||||
1, |
||||
2, |
||||
3, |
||||
4, |
||||
5, |
||||
6 |
||||
], |
||||
"ShowHelp": true, |
||||
"SpamCheck": true |
||||
} |
||||
], |
||||
"Options": [ |
||||
{ |
||||
"Name": "Username", |
||||
"Description": "The bot's github username", |
||||
"Value": "" |
||||
}, |
||||
{ |
||||
"Name": "Password", |
||||
"Description": "The bot's github password", |
||||
"Value": "" |
||||
} |
||||
] |
||||
} |
@ -0,0 +1,33 @@
@@ -0,0 +1,33 @@
|
||||
using System.Reflection; |
||||
using System.Runtime.CompilerServices; |
||||
using System.Runtime.InteropServices; |
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("Github Module")] |
||||
[assembly: AssemblyDescription("")] |
||||
[assembly: AssemblyConfiguration("")] |
||||
[assembly: AssemblyCompany("Teknik")] |
||||
[assembly: AssemblyProduct("Combot")] |
||||
[assembly: AssemblyCopyright("Copyright © 2015")] |
||||
[assembly: AssemblyTrademark("")] |
||||
[assembly: AssemblyCulture("")] |
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)] |
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("5c350b8e-5820-4034-9d67-49d2ddbfcc6f")] |
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
@ -0,0 +1,5 @@
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<packages> |
||||
<package id="GitVersionTask" version="2.0.1" targetFramework="net451" developmentDependency="true" /> |
||||
<package id="Octokit" version="0.6.2" targetFramework="net451" /> |
||||
</packages> |
Loading…
Reference in new issue