From 9dca2e3d0e1e6ab2f87deab8ac48ea91f86885fe Mon Sep 17 00:00:00 2001 From: Christophe Chailloleau-Leclerc Date: Wed, 22 May 2019 23:39:40 +0200 Subject: [PATCH] =?UTF-8?q?Essai=20de=20module=20avec=20persistence=20(en?= =?UTF-8?q?=20m=C3=A9moire,=20seulement=20dans=20la=20session=20courante)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sean/CommandHandler.cs | 9 +++-- Sean/Modules/AwardsModule.cs | 45 +++++++++++++++++++++++ Sean/Program.cs | 15 +++++++- Sean/Services/AwardsPersistencyService.cs | 44 ++++++++++++++++++++++ 4 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 Sean/Modules/AwardsModule.cs create mode 100644 Sean/Services/AwardsPersistencyService.cs diff --git a/Sean/CommandHandler.cs b/Sean/CommandHandler.cs index 679992a..e730a6c 100644 --- a/Sean/CommandHandler.cs +++ b/Sean/CommandHandler.cs @@ -1,5 +1,6 @@ using Discord.Commands; using Discord.WebSocket; +using System; using System.Reflection; using System.Threading.Tasks; @@ -10,13 +11,15 @@ namespace Sean #region Fields private readonly DiscordSocketClient _client; private readonly CommandService _commands; + private readonly IServiceProvider _services; #endregion Fields #region Constructors - public CommandHandler(DiscordSocketClient client, CommandService commands) + public CommandHandler(IServiceProvider services, DiscordSocketClient client, CommandService commands) { _commands = commands; _client = client; + _services = services; } #endregion Constructors @@ -35,7 +38,7 @@ namespace Sean // If you do not use Dependency Injection, pass null. // See Dependency Injection guide for more information. await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), - services: null); + services: _services); } private async Task HandleCommandAsync(SocketMessage messageParam) @@ -64,7 +67,7 @@ namespace Sean var result = await _commands.ExecuteAsync( context: context, argPos: argPos, - services: null); + services: _services); // Optionally, we may inform the user if the command fails // to be executed; however, this may not always be desired, diff --git a/Sean/Modules/AwardsModule.cs b/Sean/Modules/AwardsModule.cs new file mode 100644 index 0000000..6661c5f --- /dev/null +++ b/Sean/Modules/AwardsModule.cs @@ -0,0 +1,45 @@ +using Discord.Commands; +using Discord.WebSocket; +using Sean.Services; +using System.Threading.Tasks; + +namespace Sean.Modules +{ + public class AwardsModule : ModuleBase + { + #region Properties + public AwardsPersistencyService persistencyService { get; set; } + #endregion Properties + + #region Methods + [Command("blame")] + [Summary("Blames the user and removes him one point")] + [Alias("bash")] + public async Task BlameUserAsync([Summary("The user to blame")] SocketUser user) + { + var userInfo = user; + int score = persistencyService.RemovePoint(user); + await ReplyAsync($"Nice job {userInfo.Username}#{userInfo.Discriminator}, your score is now {score}"); + } + [Command("board")] + [Summary("Give the leader board")] + [Alias("top")] + public async Task BoardAsync() + { + await ReplyAsync(persistencyService.LeaderBoard()); + } + [Command("great")] + [Summary("Greats the user and give him one point")] + [Alias("reward")] + public async Task GreatUserAsync([Summary("The user to great")] SocketUser user) + { + var userInfo = user; + int score = persistencyService.AddPoint(user); + await ReplyAsync($"{userInfo.Username}#{userInfo.Discriminator} have now a score of {score}"); + } + [Command("test")] + [Summary("Ping pong !")] + public Task PongAsync() => ReplyAsync("Toi-même !"); + #endregion Methods + } +} \ No newline at end of file diff --git a/Sean/Program.cs b/Sean/Program.cs index 1eae022..47f51c2 100644 --- a/Sean/Program.cs +++ b/Sean/Program.cs @@ -1,6 +1,8 @@ using Discord; using Discord.Commands; using Discord.WebSocket; +using Microsoft.Extensions.DependencyInjection; +using Sean.Services; using System; using System.Threading.Tasks; @@ -16,6 +18,14 @@ namespace Sean #endregion Fields #region Methods + //.AddSingleton() + public IServiceProvider BuildProvider() => new ServiceCollection().BuildServiceProvider(); + public IServiceProvider BuildServiceProvider() => new ServiceCollection() + .AddSingleton(client) + .AddSingleton(commandService) + .AddSingleton() + .AddSingleton() + .BuildServiceProvider(); public async Task MainAsync() { commandServiceConfig = new CommandServiceConfig @@ -27,7 +37,10 @@ namespace Sean commandService = new CommandService(commandServiceConfig); client = new DiscordSocketClient(); client.Log += Log; - commandHandler = new CommandHandler(client, commandService); + + IServiceProvider provider = BuildServiceProvider(); + + commandHandler = new CommandHandler(provider, client, commandService); await commandHandler.InstallCommandsAsync(); await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean")); diff --git a/Sean/Services/AwardsPersistencyService.cs b/Sean/Services/AwardsPersistencyService.cs new file mode 100644 index 0000000..7fab12a --- /dev/null +++ b/Sean/Services/AwardsPersistencyService.cs @@ -0,0 +1,44 @@ +using Discord; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Sean.Services +{ + public class AwardsPersistencyService + { + #region Fields + private Dictionary scores = new Dictionary(); + #endregion Fields + + #region Methods + public int AddPoint(IUser user) + { + if (!scores.ContainsKey($"{user.Username}#{user.Discriminator}")) + scores.Add($"{user.Username}#{user.Discriminator}", 1); + else + scores[$"{user.Username}#{user.Discriminator}"]++; + return scores[$"{user.Username}#{user.Discriminator}"]; + } + + public string LeaderBoard() + { + string board = string.Empty; + foreach (KeyValuePair score in scores.OrderByDescending(s => s.Value)) + { + board += $@"{score.Key} => {score.Value}" + Environment.NewLine; + } + return board; + } + + public int RemovePoint(IUser user) + { + if (!scores.ContainsKey($"{user.Username}#{user.Discriminator}")) + scores.Add($"{user.Username}#{user.Discriminator}", -1); + else + scores[$"{user.Username}#{user.Discriminator}"]--; + return scores[$"{user.Username}#{user.Discriminator}"]; + } + #endregion Methods + } +} \ No newline at end of file