Essai de module avec persistence (en mémoire, seulement dans la session courante)
This commit is contained in:
parent
94323b7f89
commit
9dca2e3d0e
@ -1,5 +1,6 @@
|
|||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using System;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -10,13 +11,15 @@ namespace Sean
|
|||||||
#region Fields
|
#region Fields
|
||||||
private readonly DiscordSocketClient _client;
|
private readonly DiscordSocketClient _client;
|
||||||
private readonly CommandService _commands;
|
private readonly CommandService _commands;
|
||||||
|
private readonly IServiceProvider _services;
|
||||||
#endregion Fields
|
#endregion Fields
|
||||||
|
|
||||||
#region Constructors
|
#region Constructors
|
||||||
public CommandHandler(DiscordSocketClient client, CommandService commands)
|
public CommandHandler(IServiceProvider services, DiscordSocketClient client, CommandService commands)
|
||||||
{
|
{
|
||||||
_commands = commands;
|
_commands = commands;
|
||||||
_client = client;
|
_client = client;
|
||||||
|
_services = services;
|
||||||
}
|
}
|
||||||
#endregion Constructors
|
#endregion Constructors
|
||||||
|
|
||||||
@ -35,7 +38,7 @@ namespace Sean
|
|||||||
// If you do not use Dependency Injection, pass null.
|
// If you do not use Dependency Injection, pass null.
|
||||||
// See Dependency Injection guide for more information.
|
// See Dependency Injection guide for more information.
|
||||||
await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
|
await _commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(),
|
||||||
services: null);
|
services: _services);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task HandleCommandAsync(SocketMessage messageParam)
|
private async Task HandleCommandAsync(SocketMessage messageParam)
|
||||||
@ -64,7 +67,7 @@ namespace Sean
|
|||||||
var result = await _commands.ExecuteAsync(
|
var result = await _commands.ExecuteAsync(
|
||||||
context: context,
|
context: context,
|
||||||
argPos: argPos,
|
argPos: argPos,
|
||||||
services: null);
|
services: _services);
|
||||||
|
|
||||||
// Optionally, we may inform the user if the command fails
|
// Optionally, we may inform the user if the command fails
|
||||||
// to be executed; however, this may not always be desired,
|
// to be executed; however, this may not always be desired,
|
||||||
|
45
Sean/Modules/AwardsModule.cs
Normal file
45
Sean/Modules/AwardsModule.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Sean.Services;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -16,6 +18,14 @@ namespace Sean
|
|||||||
#endregion Fields
|
#endregion Fields
|
||||||
|
|
||||||
#region Methods
|
#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()
|
public async Task MainAsync()
|
||||||
{
|
{
|
||||||
commandServiceConfig = new CommandServiceConfig
|
commandServiceConfig = new CommandServiceConfig
|
||||||
@ -27,7 +37,10 @@ namespace Sean
|
|||||||
commandService = new CommandService(commandServiceConfig);
|
commandService = new CommandService(commandServiceConfig);
|
||||||
client = new DiscordSocketClient();
|
client = new DiscordSocketClient();
|
||||||
client.Log += Log;
|
client.Log += Log;
|
||||||
commandHandler = new CommandHandler(client, commandService);
|
|
||||||
|
IServiceProvider provider = BuildServiceProvider();
|
||||||
|
|
||||||
|
commandHandler = new CommandHandler(provider, client, commandService);
|
||||||
await commandHandler.InstallCommandsAsync();
|
await commandHandler.InstallCommandsAsync();
|
||||||
|
|
||||||
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean"));
|
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean"));
|
||||||
|
44
Sean/Services/AwardsPersistencyService.cs
Normal file
44
Sean/Services/AwardsPersistencyService.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user