Directories

This commit is contained in:
2018-10-04 21:18:18 +02:00
parent 3b88ea1d48
commit b5ca26c7cb
16 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
namespace LaDOSE.DiscordBot.Command
{
public partial class Twitch
{
internal class Result
{
Dependencies dep;
public Result(Dependencies d)
{
this.dep = d;
}
[RequireRolesAttribute("Staff")]
[Command("update")]
public async Task UpdateAsync(CommandContext ctx)
{
var tournament = await dep.ChallongeService.GetLastTournament();
await ctx.RespondAsync($"Mise à jour effectuée");
}
[Command("last")]
public async Task LastAsync(CommandContext ctx)
{
var lastTournamentMessage = dep.ChallongeService.GetLastTournamentMessage();
await ctx.RespondAsync(lastTournamentMessage);
}
}
}
}

View File

@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
namespace LaDOSE.DiscordBot.Command
{
[RequireRolesAttribute("SuperAdmin")]
internal class Shutdown
{
private readonly Dependencies dep;
public Shutdown(Dependencies d)
{
dep = d;
}
[Command("shutdown")]
public async Task ShutDownAsync(CommandContext ctx)
{
await ctx.RespondAsync("Hasta la vista, baby");
dep.Cts.Cancel();
}
}
}

View File

@@ -0,0 +1,26 @@
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
namespace LaDOSE.DiscordBot.Command
{
public class Result
{
internal class Twitch
{
Dependencies dep;
public Twitch(Dependencies d)
{
this.dep = d;
}
[Command("twitch")]
public async Task TwitchAsync(CommandContext ctx)
{
await ctx.RespondAsync("https://www.twitch.tv/LaDOSETV");
}
}
}
}

View File

@@ -0,0 +1,13 @@
using System.Threading;
using DSharpPlus.Interactivity;
using LaDOSE.DiscordBot.Service;
namespace LaDOSE.DiscordBot
{
internal class Dependencies
{
internal InteractivityModule Interactivity { get; set; }
internal CancellationTokenSource Cts { get; set; }
public ChallongeService ChallongeService { get; set; }
}
}

View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="3.2.3" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="3.2.3" />
<PackageReference Include="DSharpPlus.Interactivity" Version="3.2.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />
</ItemGroup>
<ItemGroup>
<Reference Include="ChallongeCSharpDriver">
<HintPath>..\..\Library\ChallongeCSharpDriver.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<None Update="settings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.Interactivity;
using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs;
using LaDOSE.DiscordBot.Command;
using LaDOSE.DiscordBot.Service;
using Microsoft.Extensions.Configuration;
namespace LaDOSE.DiscordBot
{
class Program
{
static DiscordClient discord;
static void Main(string[] args)
{
MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build();
var discordToken = builder["Discord:Token"].ToString();
var challongeToken = builder["Challonge:Token"].ToString();
Console.WriteLine($"LaDOSE.Net Discord Bot");
discord = new DiscordClient(new DiscordConfiguration
{
Token = discordToken,
TokenType = TokenType.Bot
});
var challongeService = new ChallongeService(challongeToken);
var cts = new CancellationTokenSource();
DependencyCollection dep = null;
using (var d = new DependencyCollectionBuilder())
{
d.AddInstance(new Dependencies()
{
Cts = cts,
ChallongeService = challongeService
});
dep = d.Build();
}
var _cnext = discord.UseCommandsNext(new CommandsNextConfiguration()
{
CaseSensitive = false,
EnableDefaultHelp = true,
EnableDms = false,
EnableMentionPrefix = true,
StringPrefix = "!",
IgnoreExtraArguments = true,
Dependencies = dep
});
_cnext.RegisterCommands<Result>();
_cnext.RegisterCommands<Twitch>();
_cnext.RegisterCommands<Shutdown>();
//discord.MessageCreated += async e =>
//{
// if (e.Message.Content.ToLower().Equals("!result"))
// await e.Message.RespondAsync("Les Résultats du dernier Ranking : XXXX");
// if (e.Message.Content.ToLower().Equals("!twitch"))
// await e.Message.RespondAsync("https://www.twitch.tv/LaDOSETV");
//};
discord.GuildMemberAdded += async e =>
{
await e.Guild.GetDefaultChannel().SendMessageAsync($"Bonjour {e.Member.DisplayName}!");
};
await discord.ConnectAsync();
while (!cts.IsCancellationRequested)
{
await Task.Delay(200);
}
}
}
}

View File

@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChallongeCSharpDriver;
using ChallongeCSharpDriver.Caller;
using ChallongeCSharpDriver.Core.Queries;
using ChallongeCSharpDriver.Core.Results;
using ChallongeCSharpDriver.Main;
using ChallongeCSharpDriver.Main.Objects;
namespace LaDOSE.DiscordBot.Service
{
public class ChallongeService
{
private ChallongeConfig Config;
public string ApiKey { get; set; }
public ChallongeHTTPClientAPICaller ApiCaller { get; set; }
public string DernierTournois { get; set; }
public ChallongeService(string apiKey)
{
this.ApiKey = apiKey;
this.Config = new ChallongeConfig(this.ApiKey);
this.ApiCaller = new ChallongeHTTPClientAPICaller(Config);
DernierTournois = "Aucun tournois.";
}
public async Task<Boolean> GetLastTournament()
{
try
{
List<TournamentResult> tournamentResultList = await new TournamentsQuery()
{
state = TournamentState.Ended
}
.call(this.ApiCaller);
var lastDate = tournamentResultList.Max(e => e.completed_at);
var lastRankingDate = new DateTime(lastDate.Year, lastDate.Month, lastDate.Day);
var lastTournament = tournamentResultList.Where(e => e.completed_at > lastRankingDate).ToList();
string returnValue = "Les derniers tournois : \n";
foreach (var tournamentResult in lastTournament)
{
returnValue += $"{tournamentResult.name} : <https://challonge.com/{tournamentResult.url}> \n";
}
DernierTournois = returnValue;
return true;
}
catch
{
return false;
}
}
public string GetLastTournamentMessage()
{
return DernierTournois;
}
}
}

View File

@@ -0,0 +1,8 @@
{
"Discord": {
"Token": "DISCORD TOKEN"
},
"Challonge": {
"Token": "CHALLONGE API TOKEN"
}
}