Test Connection

Add Todo
Bot use Webservice now
TBD : Rework Event
This commit is contained in:
2019-03-27 00:37:11 +01:00
parent 74327eb381
commit 63db02d798
25 changed files with 379 additions and 178 deletions

View File

@@ -14,18 +14,18 @@ namespace LaDOSE.DiscordBot.Command
}
[RequireRolesAttribute("Staff")]
[Command("update")]
public async Task UpdateAsync(CommandContext ctx)
{
var tournament = await dep.ChallongeService.GetLastTournament();
await ctx.RespondAsync($"Mise à jour effectuée");
}
//[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();
var lastTournamentMessage = dep.WebService.GetLastChallonge();
await ctx.RespondAsync(lastTournamentMessage);
}

View File

@@ -1,6 +1,9 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Interactivity;
using LaDOSE.DTO;
namespace LaDOSE.DiscordBot.Command
{
@@ -17,22 +20,98 @@ namespace LaDOSE.DiscordBot.Command
public async Task TodoAsync(CommandContext ctx, string command,params string[] todo)
{
await ctx.TriggerTypingAsync();
string args = string.Join(" ",todo);
switch (command.ToUpperInvariant())
{
case "ADD":
dep.TodoService.Add(todo[0]);
var todoDto = new TodoDTO
{
Created = DateTime.Now,
Done = false,
Deleted = null,
Task = args,
User = ctx.User.Username,
};
dep.WebService.RestService.UpdateTodo(todoDto);
//dep.WebService.RestService.UpdateGame();
break;
case "LIST":
await ctx.RespondAsync($"{dep.TodoService.List()}");
var todoDtos = dep.WebService.RestService.GetTodos();
foreach (var task in todoDtos)
{
string taskStatus = task.Done ? ":white_check_mark:" : ":negative_squared_cross_mark:";
await ctx.RespondAsync($"{task?.Id} - {task?.Task} Par : {task?.User} Etat : {taskStatus}");
}
break;
case "DEL":
int id;
if (int.TryParse(todo[0], out id))
try
{
await ctx.RespondAsync($"{dep.TodoService.Delete(id)}");
break;
};
await ctx.RespondAsync($"invalid id");
int id = int.Parse(todo[0]);
await ctx.RespondAsync(dep.WebService.RestService.DeleteTodo(id) ? $"Deleted" : $"Error");
}
catch (Exception e)
{
await ctx.RespondAsync($"Error {e.Message}");
return;
}
break;
case "V":
try
{
int id = int.Parse(todo[0]);
var todoById = dep.WebService.RestService.GetTodoById(id);
todoById.Done = true;
dep.WebService.RestService.UpdateTodo(todoById);
await ctx.RespondAsync($"Done : {todoById.Id} - {todoById.Task}");
}
catch (Exception e)
{
await ctx.RespondAsync($"Error {e.Message}");
return;
}
break;
case "X":
try
{
int id = int.Parse(todo[0]);
var todoById = dep.WebService.RestService.GetTodoById(id);
todoById.Done = false;
dep.WebService.RestService.UpdateTodo(todoById);
await ctx.RespondAsync($"Undone : {todoById.Id} - {todoById.Task}");
}
catch (Exception e)
{
await ctx.RespondAsync($"Erreur {e.Message}");
return;
}
break;
case "TRUNC":
try
{
var todos = dep.WebService.RestService.GetTodos();
await ctx.RespondAsync($"Sure ? (Y/N)");
var interactivity = ctx.Client.GetInteractivityModule();
var waitForMessageAsync = await interactivity.WaitForMessageAsync(xm => xm.Content.Contains("Y")||xm.Content.Contains("N"), TimeSpan.FromSeconds(10));
if (waitForMessageAsync!= null)
{
if (waitForMessageAsync.Message.Content == "Y")
{
foreach (var task in todos)
{
dep.WebService.RestService.DeleteTodo(task.Id);
await ctx.RespondAsync($"Deleted - {task.Id}");
}
}
}
}
catch (Exception e)
{
await ctx.RespondAsync($"Erreur {e.Message}");
return;
}
break;
}

View File

@@ -8,8 +8,6 @@ namespace LaDOSE.DiscordBot
{
internal InteractivityModule Interactivity { get; set; }
internal CancellationTokenSource Cts { get; set; }
public ChallongeService ChallongeService { get; set; }
public TodoService TodoService { get; set; }
public WebService WebService { get; set; }
}
}

View File

@@ -16,7 +16,7 @@ namespace LaDOSE.DiscordBot
class Program
{
static DiscordClient discord;
static InteractivityModule Interactivity { get; set; }
static void Main(string[] args)
{
MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
@@ -44,9 +44,20 @@ namespace LaDOSE.DiscordBot
TokenType = TokenType.Bot
});
discord.UseInteractivity(new InteractivityConfiguration
{
// default pagination behaviour to just ignore the reactions
PaginationBehaviour = TimeoutBehaviour.Ignore,
// default pagination timeout to 5 minutes
PaginationTimeout = TimeSpan.FromMinutes(5),
// default timeout for other actions to 2 minutes
Timeout = TimeSpan.FromMinutes(2)
});
var webService = new WebService(new Uri(restUrl),restUser,restPassword);
var challongeService = new ChallongeService(challongeToken);
var todoService = new TodoService();
//var challongeService = new ChallongeService(challongeToken);
var cts = new CancellationTokenSource();
DependencyCollection dep = null;
@@ -54,10 +65,8 @@ namespace LaDOSE.DiscordBot
{
d.AddInstance(new Dependencies()
{
Cts = cts,
ChallongeService = challongeService,
TodoService = todoService,
//ChallongeService = challongeService,
WebService = webService
});
dep = d.Build();

View File

@@ -1,72 +0,0 @@
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);
if (lastDate.HasValue)
{
var lastRankingDate = new DateTime(lastDate.Value.Year, lastDate.Value.Month, lastDate.Value.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

@@ -1,61 +0,0 @@
using System;
using System.IO;
namespace LaDOSE.DiscordBot.Service
{
public class TodoService
{
private const string db = "todo.txt";
public TodoService()
{
}
public bool Add(string text)
{
if (!string.IsNullOrEmpty(text)) {
using (var textWriter =File.AppendText(db))
{
textWriter.WriteLine(text);
}
return true;
}
return false;
}
public bool Delete(int id)
{
string returnText = "";
var text = File.ReadAllText(db);
var i = 0;
foreach (var line in text.Split('\n'))
{
++i;
if (i != id)
{
returnText += $"{line}\n";
}
}
File.WriteAllText(db,returnText);
return true;
}
public string List()
{
string returnText = "";
var text = File.ReadAllText(db);
var i = 0;
foreach (var line in text.Split())
{
if(!string.IsNullOrEmpty(line))
returnText += $"{++i}. {line}";
}
return returnText;
}
}
}

View File

@@ -10,6 +10,7 @@ using ChallongeCSharpDriver.Main;
using ChallongeCSharpDriver.Main.Objects;
using LaDOSE.DTO;
using LaDOSE.REST;
using RestSharp.Authenticators;
namespace LaDOSE.DiscordBot.Service
{
@@ -17,12 +18,20 @@ namespace LaDOSE.DiscordBot.Service
{
private RestService restService;
public RestService RestService => restService;
public WebService(Uri uri,string user,string password)
{
restService = new RestService();
restService.Connect(uri,user,password);
}
private void CheckToken()
{
}
public String GetInscrits()
@@ -37,5 +46,10 @@ namespace LaDOSE.DiscordBot.Service
{
return restService.RefreshDb();
}
public string GetLastChallonge()
{
return restService.GetLastChallonge();
}
}
}