Essai de module avec persistence (en mémoire, seulement dans la session courante)

This commit is contained in:
Christophe Chailloleau-Leclerc 2019-05-22 23:39:40 +02:00
parent 94323b7f89
commit 9dca2e3d0e
4 changed files with 109 additions and 4 deletions

View File

@ -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,

View File

@ -0,0 +1,45 @@
using Discord.Commands;
using Discord.WebSocket;
using Sean.Services;
using System.Threading.Tasks;
namespace Sean.Modules
{
public class AwardsModule : ModuleBase<SocketCommandContext>
{
#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
}
}

View File

@ -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<DatabaseService>()
public IServiceProvider BuildProvider() => new ServiceCollection().BuildServiceProvider();
public IServiceProvider BuildServiceProvider() => new ServiceCollection()
.AddSingleton(client)
.AddSingleton(commandService)
.AddSingleton<CommandHandler>()
.AddSingleton<AwardsPersistencyService>()
.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"));

View File

@ -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<string, int> scores = new Dictionary<string, int>();
#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<string, int> 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
}
}