Ajout de CommandNext

This commit is contained in:
2018-10-04 00:29:25 +02:00
parent d28d0333a7
commit 160b1ebb7b
6 changed files with 137 additions and 20 deletions

View File

@@ -0,0 +1,25 @@
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;
}
[Command("result")]
public async Task ResultAsync(CommandContext ctx)
{
await ctx.RespondAsync("Resultat");
}
}
}
}

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 partial 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,11 @@
using System.Threading;
using DSharpPlus.Interactivity;
namespace LaDOSE.DiscordBot
{
internal class Dependencies
{
internal InteractivityModule Interactivity { get; set; }
internal CancellationTokenSource Cts { get; set; }
}
}

View File

@@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DSharpPlus" Version="3.2.3" /> <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="DSharpPlus.Interactivity" Version="3.2.3" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.1.1" />

View File

@@ -1,28 +1,29 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using DSharpPlus; using DSharpPlus;
using DSharpPlus.Interactivity; using DSharpPlus.Interactivity;
using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs;
using LaDOSE.DiscordBot.Command;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
namespace LaDOSE.DiscordBot namespace LaDOSE.DiscordBot
{ {
class Program class Program
{ {
static DiscordClient discord; static DiscordClient discord;
static InteractivityModule _interactivity; static InteractivityModule _interactivity;
static void Main(string[] args) static void Main(string[] args)
{ {
MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult(); MainAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
} }
static async Task MainAsync(string[] args) static async Task MainAsync(string[] args)
{ {
var builder = new ConfigurationBuilder() var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) .SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build(); .AddJsonFile("settings.json", optional: true, reloadOnChange: true).Build();
@@ -46,26 +47,53 @@ namespace LaDOSE.DiscordBot
Timeout = TimeSpan.FromSeconds(30) Timeout = TimeSpan.FromSeconds(30)
}); });
discord.MessageCreated += async e => var _cts = new CancellationTokenSource();
DependencyCollection dep = null;
using (var d = new DependencyCollectionBuilder())
{ {
if (e.Message.Content.ToLower().Equals("!result")) d.AddInstance(new Dependencies()
await e.Message.RespondAsync("Les Résultats du dernier Ranking : XXXX"); {
if (e.Message.Content.ToLower().Equals("!twitch")) Interactivity = _interactivity,
await e.Message.RespondAsync("https://www.twitch.tv/LaDOSETV"); Cts = _cts
}; });
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 => discord.GuildMemberAdded += async e =>
{ {
await e.Guild.GetDefaultChannel().SendMessageAsync($"Bonjour {e.Member.Nickname}!"); await e.Guild.GetDefaultChannel().SendMessageAsync($"Bonjour {e.Member.DisplayName}!");
}; };
await discord.ConnectAsync(); await discord.ConnectAsync();
await Task.Delay(-1); while (!_cts.IsCancellationRequested)
{
await Task.Delay(200);
} }
} }
} }
}