Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add discrod botV2 #139

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
25 changes: 20 additions & 5 deletions Bot.sln
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@


Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Platform.Bot", "csharp\Platform.Bot\Platform.Bot.csproj", "{81D55AD3-9698-4BEC-B7EC-26ED7B8E6E53}"
# Visual Studio Version 17
VisualStudioVersion = 17.0.31919.166
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Platform.Bot", "csharp\Platform.Bot\Platform.Bot.csproj", "{81D55AD3-9698-4BEC-B7EC-26ED7B8E6E53}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FileManager", "csharp\FileManager\FileManager.csproj", "{2F40CB1A-A1FD-4433-B998-BC3AEA2EFEB3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FileManager", "csharp\FileManager\FileManager.csproj", "{2F40CB1A-A1FD-4433-B998-BC3AEA2EFEB3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Interfaces", "csharp\Interfaces\Interfaces.csproj", "{CF4AF09D-456A-4F85-9879-99AE74839B35}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Interfaces", "csharp\Interfaces\Interfaces.csproj", "{CF4AF09D-456A-4F85-9879-99AE74839B35}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Storage", "csharp\Storage\Storage.csproj", "{E81C2E82-9F15-478C-8388-E9BD53686E36}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Storage", "csharp\Storage\Storage.csproj", "{E81C2E82-9F15-478C-8388-E9BD53686E36}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordBot", "csharp\DiscordBot\DiscordBot.csproj", "{3A87CAD8-A2F8-462A-B1F9-2AB6883B815C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -30,5 +35,15 @@ Global
{E81C2E82-9F15-478C-8388-E9BD53686E36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E81C2E82-9F15-478C-8388-E9BD53686E36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E81C2E82-9F15-478C-8388-E9BD53686E36}.Release|Any CPU.Build.0 = Release|Any CPU
{3A87CAD8-A2F8-462A-B1F9-2AB6883B815C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3A87CAD8-A2F8-462A-B1F9-2AB6883B815C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3A87CAD8-A2F8-462A-B1F9-2AB6883B815C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3A87CAD8-A2F8-462A-B1F9-2AB6883B815C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F6862B5F-1B9D-4EE5-92DA-19F07B85A78C}
EndGlobalSection
EndGlobal
14 changes: 14 additions & 0 deletions csharp/DiscordBot/DiscordBot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Storage\Storage.csproj" />
</ItemGroup>
</Project>
67 changes: 67 additions & 0 deletions csharp/DiscordBot/DiscrodBot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Discord;
using Discord.WebSocket;
using System;
using System.Threading.Tasks;

namespace LinksPlatformDiscordBot
{
public class BotStartup
{
private Storage.Local.FileStorage LinksStorage { get; set; }

private string Token { get; set; }

private DiscordSocketClient Client { get; set; }

public BotStartup(string token, Storage.Local.FileStorage storage)
{
LinksStorage = storage;
Client = new DiscordSocketClient();
Token = token;
}
public async Task StartupAsync()
{
Client.Log += Log;
Client.MessageReceived += MessageReceived;
await Client.LoginAsync(TokenType.Bot, Token);
await Client.StartAsync();

await Task.Delay(-1);
}
private async Task MessageReceived(SocketMessage message)
{
if (!message.Author.IsBot)
{
if (message.Content.Contains("https://github.com"))
{
if (message.Channel.Name == "main")

await message.Channel.SendMessageAsync($"<@214371962187808778> would we accept " + "<@" + message.Author.Id + "> as our new team member?");
}
if (message.Content.Contains("accept"))
{
if (message.Author.Username == "Konstantin Dyachenko" || message.Author.Username == "konard" || message.Author.Username == "FirstAfterGod")
{
string link = message.Channel.GetMessageAsync(message.Reference.MessageId.Value).Result.Content;
LinksStorage.AddLinkToIvite(link);
await message.Channel.SendMessageAsync("<@" + message.Channel.GetMessageAsync(message.Reference.MessageId.Value).Result.Author.Id + "> please accept invitation to our organization either by going to http://github.com/linksplatform or via email that was sent to you from GitHub.");
foreach (var a in LinksStorage.GetLinksToInvite())
{
Console.WriteLine(a);
}
}
}
}
}
public void CreateInvite(string link)
{

}

private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
}
}
36 changes: 18 additions & 18 deletions csharp/FileManager/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@

namespace FileManager
{
/// <summary>
/// <para>
/// Represents the context.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Represents the context.
/// </para>
/// <para></para>
/// </summary>
public class Context
{
/// <summary>
/// <para>
/// Gets or sets the args value.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Gets or sets the args value.
/// </para>
/// <para></para>
/// </summary>
public string[] Args { get; set; }

/// <summary>
/// <para>
/// Gets or sets the file storage value.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Gets or sets the file storage value.
/// </para>
/// <para></para>
/// </summary>
public FileStorage FileStorage { get; set; }
}
}
24 changes: 12 additions & 12 deletions csharp/FileManager/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@

namespace FileManager
{
/// <summary>
/// <para>
/// Represents the program.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Represents the program.
/// </para>
/// <para></para>
/// </summary>
internal class Program
{
/// <summary>
/// <para>
/// The get files by file set name trigger.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// The get files by file set name trigger.
/// </para>
/// <para></para>
/// </summary>
public static List<ITrigger<Context>> Handlers = new()
{
new CreateTrigger(),
Expand Down
3 changes: 2 additions & 1 deletion csharp/Platform.Bot/Platform.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
<TargetFramework>net6</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.14.0" />
<PackageReference Include="Octokit" Version="0.50.0" />
<PackageReference Include="Platform.Communication.Protocol.Lino" Version="0.4.0" />
<PackageReference Include="Platform.Data.Doublets.Sequences" Version="0.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DiscordBot\DiscordBot.csproj" />
<ProjectReference Include="..\Interfaces\Interfaces.csproj" />
<ProjectReference Include="..\Storage\Storage.csproj" />
</ItemGroup>
Expand Down
30 changes: 12 additions & 18 deletions csharp/Platform.Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using Platform.Bot.Trackers;
using Platform.Bot.Triggers;
using System.Threading.Tasks;

namespace Platform.Bot
{
Expand All @@ -29,28 +30,21 @@ private static void Main(string[] args)
var token = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "Token", args);
var appName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "App Name", args);
var databaseFileName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "Database file name", args);
var fileSetName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "File set name", args);
var minimumInteractionIntervalStringInputInSeconds = ConsoleHelpers.GetOrReadArgument(argumentIndex, "Minimum interaction interval in seconds", args);
var minimumInteractionInterval = TimeSpan.FromSeconds(Int32.Parse(minimumInteractionIntervalStringInputInSeconds));
var fileSetName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "File set name ", args);
var discordToken = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "Token for discord bot", args);
var OrgName = ConsoleHelpers.GetOrReadArgument(argumentIndex++, "Name of the organization",args);

var dbContext = new FileStorage(databaseFileName);
Console.WriteLine($"Bot has been started. {Environment.NewLine}Press CTRL+C to close");
new LinksPlatformDiscordBot.BotStartup(discordToken, dbContext).StartupAsync();
try
{
while (true)
{
var api = new GitHubStorage(username, token, appName);
var issueTracker = new IssueTracker(api,
new HelloWorldTrigger(api, dbContext, fileSetName),
new OrganizationLastMonthActivityTrigger(api),
new LastCommitActivityTrigger(api),
new ProtectMainBranchTrigger(api));
var pullRequenstTracker = new PullRequestTracker(api, new MergeDependabotBumpsTrigger(api));
var timestampTracker = new DateTimeTracker(api, new CreateAndSaveOrganizationRepositoriesMigrationTrigger(api, dbContext, Path.Combine(Directory.GetCurrentDirectory(), "/github-migrations")));
issueTracker.Start(cancellation.Token);
pullRequenstTracker.Start(cancellation.Token);
timestampTracker.Start(cancellation.Token);
Thread.Sleep(minimumInteractionInterval);
}
var api = new GitHubStorage(username, token, appName);
Task.Run(() => new InviteToOrgTracker(OrgName, 1000, dbContext, api).Start(cancellation.Token));

new InviteToOrgTracker("LinksPlatfrom", 1200, dbContext, api).Start(cancellation.Token);
new PullRequestTracker(new List<ITrigger<PullRequest>> { new MergeDependabotBumpsTrigger(api) }, api).Start(cancellation.Token);

}
catch (Exception ex)
{
Expand Down
42 changes: 42 additions & 0 deletions csharp/Platform.Bot/Trackers/InviteToOrgTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Storage.Local;
using Storage.Remote.GitHub;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Platform.Bot.Trackers
{
public class InviteToOrgTracker
{
private string OrgName {get;set;}

private int MinimumInteractionInterval = 1000;

private FileStorage Storage { get; set; }

GitHubStorage GitHubStorage { get; set; }

public InviteToOrgTracker(string orgName,int minimumInteractionInterval,FileStorage storage, GitHubStorage gitHubStorage)
{
OrgName = orgName;
MinimumInteractionInterval = minimumInteractionInterval;
Storage = storage;
GitHubStorage = gitHubStorage;
}

public void Start(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
foreach (var link in Storage.GetLinksToInvite())
{
GitHubStorage.InviteToOrg(OrgName, link.Replace("https://github.com/", ""));
}
Thread.Sleep(MinimumInteractionInterval);
}
}
}
}
19 changes: 14 additions & 5 deletions csharp/Platform.Bot/Trackers/IssueTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,26 @@ public IssueTracker(GitHubStorage gitHubApi, params ITrigger<Issue>[] triggers)
/// </param>
public void Start(CancellationToken cancellationToken)
{
foreach (var trigger in _triggers)
Console.WriteLine("issue Trecker has been started");
while (!cancellationToken.IsCancellationRequested)

{
foreach (var issue in _storage.GetIssues())
{
if (cancellationToken.IsCancellationRequested)
try
{
return;
foreach (var issue in GitHubApi.GetIssues())
{
if (trigger.Condition(issue))
{
trigger.Action(issue);
}
}

}
if (trigger.Condition(issue))
catch (Exception ex)
{
trigger.Action(issue);
Console.WriteLine(ex);
}
}
}
Expand Down
36 changes: 18 additions & 18 deletions csharp/Storage/LocalStorage/File.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@

namespace Storage.Local
{
/// <summary>
/// <para>
/// Represents the file.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Represents the file.
/// </para>
/// <para></para>
/// </summary>
public class File
{
/// <summary>
/// <para>
/// Gets or sets the path value.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Gets or sets the path value.
/// </para>
/// <para></para>
/// </summary>
public string Path { get; set; }

/// <summary>
/// <para>
/// Gets or sets the content value.
/// </para>
/// <para></para>
/// </summary>
/// <summary>
/// <para>
/// Gets or sets the content value.
/// </para>
/// <para></para>
/// </summary>
public string Content { get; set; }
}
}
Loading