Fix the bots

This commit is contained in:
2022-09-14 23:29:22 +02:00
parent 8fd6c532c5
commit 3d4ff74d71
8 changed files with 172 additions and 158 deletions

View File

@@ -3,31 +3,31 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes; using DSharpPlus.CommandsNext.Attributes;
using LaDOSE.DiscordBot.Service;
using LaDOSE.DTO; using LaDOSE.DTO;
namespace LaDOSE.DiscordBot.Command namespace LaDOSE.DiscordBot.Command
{ {
public class BotEvent public class BotEvent : BaseCommandModule
{ {
private readonly Dependencies dep; private WebService dep;
public BotEvent(WebService d)
public BotEvent(Dependencies d)
{ {
dep = d; dep = d;
} }
[RequireRolesAttribute("Staff")] [RequireRolesAttribute(RoleCheckMode.Any, "Staff")]
[Command("newevent")] [Command("newevent")]
public async Task NewEventAsync(CommandContext ctx, string command) public async Task NewEventAsync(CommandContext ctx, string command)
{ {
await ctx.RespondAsync(dep.WebService.RestService.CreateBotEvent(command).ToString()); await ctx.RespondAsync(dep.RestService.CreateBotEvent(command).ToString());
} }
[RequireRolesAttribute("Staff")] [RequireRolesAttribute(RoleCheckMode.Any,"Staff")]
[Command("staffs")] [Command("staffs")]
public async Task StaffAsync(CommandContext ctx) public async Task StaffAsync(CommandContext ctx)
{ {
BotEventDTO currentEvent = dep.WebService.RestService.GetLastBotEvent(); BotEventDTO currentEvent = dep.RestService.GetLastBotEvent();
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
var present = currentEvent.Results.Where(x => x.Result).ToList(); var present = currentEvent.Results.Where(x => x.Result).ToList();
@@ -40,19 +40,19 @@ namespace LaDOSE.DiscordBot.Command
await ctx.RespondAsync(stringBuilder.ToString()); await ctx.RespondAsync(stringBuilder.ToString());
} }
[RequireRolesAttribute("Staff")] [RequireRolesAttribute(RoleCheckMode.Any, "Staff")]
[Command("present")] [Command("present")]
public async Task PresentAsync(CommandContext ctx) public async Task PresentAsync(CommandContext ctx)
{ {
await ctx.RespondAsync(dep.WebService.RestService.ResultBotEvent(new DTO.BotEventSendDTO() { DiscordId = ctx.Member.Id.ToString(), DiscordName = ctx.Member.DisplayName, Present = true }).ToString()); await ctx.RespondAsync(dep.RestService.ResultBotEvent(new DTO.BotEventSendDTO() { DiscordId = ctx.Member.Id.ToString(), DiscordName = ctx.Member.DisplayName, Present = true }).ToString());
} }
[RequireRolesAttribute("Staff")] [RequireRolesAttribute(RoleCheckMode.Any, "Staff")]
[Command("absent")] [Command("absent")]
public async Task AbsentAsync(CommandContext ctx) public async Task AbsentAsync(CommandContext ctx)
{ {
await ctx.RespondAsync(dep.WebService.RestService.ResultBotEvent(new DTO.BotEventSendDTO() { DiscordId = ctx.Member.Id.ToString(), DiscordName = ctx.Member.DisplayName, Present = false }).ToString()); await ctx.RespondAsync(dep.RestService.ResultBotEvent(new DTO.BotEventSendDTO() { DiscordId = ctx.Member.Id.ToString(), DiscordName = ctx.Member.DisplayName, Present = false }).ToString());
} }
} }
} }

View File

@@ -9,14 +9,14 @@ using DSharpPlus.Entities;
namespace LaDOSE.DiscordBot.Command namespace LaDOSE.DiscordBot.Command
{ {
public class Hokuto public class Hokuto : BaseCommandModule
{ {
private readonly Dependencies dep;
private static List<string> Games = new List<string> { "2X", "3.3", "Karnov" }; private static List<string> Games = new List<string> { "2X", "3.3", "Karnov" };
private static Random r = new Random(); private static Random r = new Random();
public Hokuto(Dependencies d) public Hokuto()
{ {
dep = d;
} }

View File

@@ -9,16 +9,16 @@ using DSharpPlus.CommandsNext.Attributes;
namespace LaDOSE.DiscordBot.Command namespace LaDOSE.DiscordBot.Command
{ {
public class Public public class Public : BaseCommandModule
{ {
private readonly Dependencies dep;
private static List<string> Quotes { get; set; } private static List<string> Quotes { get; set; }
private static List<string> Questions { get; set; } private static List<string> Questions { get; set; }
private static List<string> Answers { get; set; } private static List<string> Answers { get; set; }
private Random rnd { get; set; } private Random rnd { get; set; }
public Public(Dependencies d) public Public()
{ {
dep = d;
rnd = new Random(DateTime.Now.Millisecond); rnd = new Random(DateTime.Now.Millisecond);
} }
@@ -65,9 +65,17 @@ namespace LaDOSE.DiscordBot.Command
} }
else else
{ {
for (int i = 0; i < s.Length - 1; i++) for (int i = 0; i < s.Length - 1; i++)
{ {
response += s[i] + Answers[rnd.Next(Answers.Count - 1)]; if (s[i] == "" && i==0)
{
response += Answers[rnd.Next(Answers.Count - 1)];
}
else
{
response += s[i] + "***" + Answers[rnd.Next(Answers.Count - 1)].ToLower() + "***";
}
} }
response += s[s.Length - 1]; response += s[s.Length - 1];
} }

View File

@@ -1,45 +1,45 @@
using System.Threading.Tasks; //using System.Threading.Tasks;
using DSharpPlus.CommandsNext; //using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes; //using DSharpPlus.CommandsNext.Attributes;
namespace LaDOSE.DiscordBot.Command //namespace LaDOSE.DiscordBot.Command
{ //{
internal class Result // internal class Result : BaseCommandModule
{ // {
Dependencies dep; // Dependencies dep;
public Result(Dependencies d) // public Result(Dependencies d)
{ // {
this.dep = d; // this.dep = d;
} // }
//[Command("last")] // //[Command("last")]
//public async Task LastAsync(CommandContext ctx) // //public async Task LastAsync(CommandContext ctx)
//{ // //{
// var lastTournamentMessage = dep.WebService.GetLastChallonge(); // // var lastTournamentMessage = dep.WebService.GetLastChallonge();
// await ctx.RespondAsync(lastTournamentMessage); // // await ctx.RespondAsync(lastTournamentMessage);
//} // //}
//[RequireRolesAttribute("Staff")] // //[RequireRolesAttribute("Staff")]
//[Command("inscriptions")] // //[Command("inscriptions")]
//public async Task InscriptionsAsync(CommandContext ctx) // //public async Task InscriptionsAsync(CommandContext ctx)
//{ // //{
// await ctx.TriggerTypingAsync(); // // await ctx.TriggerTypingAsync();
// var inscrits = dep.WebService.GetInscrits(); // // var inscrits = dep.WebService.GetInscrits();
// await ctx.RespondAsync(inscrits); // // await ctx.RespondAsync(inscrits);
//} // //}
[RequireRolesAttribute("Staff")] // [RequireRolesAttribute(RoleCheckMode.Any,"Staff")]
[Command("UpdateDb")] // [Command("UpdateDb")]
public async Task UpdateDbAsync(CommandContext ctx) // public async Task UpdateDbAsync(CommandContext ctx)
{ // {
await ctx.RespondAsync("Mise à jour des inscriptions en cours..."); // await ctx.RespondAsync("Mise à jour des inscriptions en cours...");
await ctx.TriggerTypingAsync(); // await ctx.TriggerTypingAsync();
var status = dep.WebService.RefreshDb() ? "Ok" : "erreur"; // var status = dep.WebService.RefreshDb() ? "Ok" : "erreur";
await ctx.RespondAsync($"Status: {status}"); // await ctx.RespondAsync($"Status: {status}");
} // }
} // }
} //}

View File

@@ -1,25 +1,25 @@
using System.Threading.Tasks; //using System.Threading.Tasks;
using DSharpPlus.CommandsNext; //using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes; //using DSharpPlus.CommandsNext.Attributes;
namespace LaDOSE.DiscordBot.Command //namespace LaDOSE.DiscordBot.Command
{ //{
[RequireRolesAttribute("SuperAdmin")] // [RequireRolesAttribute(RoleCheckMode.Any,"SuperAdmin")]
public class Shutdown // public class Shutdown : BaseCommandModule
{ // {
private readonly Dependencies dep; // private readonly Dependencies dep;
public Shutdown(Dependencies d) // public Shutdown(Dependencies d)
{ // {
dep = d; // dep = d;
} // }
[Command("shutdown")] // [Command("shutdown")]
public async Task ShutDownAsync(CommandContext ctx) // public async Task ShutDownAsync(CommandContext ctx)
{ // {
await ctx.RespondAsync("Hasta la vista, baby"); // await ctx.RespondAsync("Hasta la vista, baby");
dep.Cts.Cancel(); // dep.Cts.Cancel();
} // }
} // }
} //}

View File

@@ -4,10 +4,10 @@ using LaDOSE.DiscordBot.Service;
namespace LaDOSE.DiscordBot namespace LaDOSE.DiscordBot
{ {
public class Dependencies //public class Dependencies
{ //{
internal InteractivityModule Interactivity { get; set; } // //internal InteractivityModule Interactivity { get; set; }
internal CancellationTokenSource Cts { get; set; } // internal CancellationTokenSource Cts { get; set; }
public WebService WebService { get; set; } // public WebService WebService { get; set; }
} //}
} }

View File

@@ -7,9 +7,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DSharpPlus" Version="3.2.3" /> <PackageReference Include="DSharpPlus" Version="4.2.0" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="3.2.3" /> <PackageReference Include="DSharpPlus.CommandsNext" Version="4.2.0" />
<PackageReference Include="DSharpPlus.Interactivity" Version="3.2.3" /> <PackageReference Include="DSharpPlus.Interactivity" Version="4.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" /> <PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.8" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" /> <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.8" />
</ItemGroup> </ItemGroup>

View File

@@ -7,21 +7,29 @@ using DSharpPlus;
using DSharpPlus.Interactivity; using DSharpPlus.Interactivity;
using DSharpPlus.CommandsNext; using DSharpPlus.CommandsNext;
using DSharpPlus.EventArgs; using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity.Extensions;
//using DSharpPlus.SlashCommands;
//using DSharpPlus.SlashCommands.Attributes;
using LaDOSE.DiscordBot.Command; using LaDOSE.DiscordBot.Command;
using LaDOSE.DiscordBot.Service; using LaDOSE.DiscordBot.Service;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace LaDOSE.DiscordBot namespace LaDOSE.DiscordBot
{ {
class Program class Program
{ {
static DiscordClient discord; static DiscordClient discord;
static InteractivityModule Interactivity { get; set; }
//static InteractivityModule Interactivity { get; set; }
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)
{ {
Console.WriteLine(Directory.GetCurrentDirectory()); Console.WriteLine(Directory.GetCurrentDirectory());
@@ -35,108 +43,106 @@ namespace LaDOSE.DiscordBot
var restUser = builder["REST:User"].ToString(); var restUser = builder["REST:User"].ToString();
var restPassword = builder["REST:Password"].ToString(); var restPassword = builder["REST:Password"].ToString();
var service = new ServiceCollection()
.AddSingleton(typeof(WebService), new WebService(new Uri(restUrl), restUser, restPassword))
.BuildServiceProvider();
Console.WriteLine($"LaDOSE.Net Discord Bot"); Console.WriteLine($"LaDOSE.Net Discord Bot");
discord = new DiscordClient(new DiscordConfiguration discord = new DiscordClient(new DiscordConfiguration
{ {
Token = discordToken, Token = discordToken,
TokenType = TokenType.Bot TokenType = TokenType.Bot,
//AutoReconnect = true,
//MinimumLogLevel = LogLevel.Debug,
//MessageCacheSize = 0,
}); });
discord.UseInteractivity(new InteractivityConfiguration discord.UseInteractivity(new InteractivityConfiguration
{ {
// default pagination behaviour to just ignore the reactions // default pagination behaviour to just ignore the reactions
PaginationBehaviour = TimeoutBehaviour.Ignore, //PaginationBehaviour = TimeoutBehaviour.Ignore,
// default pagination timeout to 5 minutes // default pagination timeout to 5 minutes
PaginationTimeout = TimeSpan.FromMinutes(5), //PaginationTimeout = TimeSpan.FromMinutes(5),
// default timeout for other actions to 2 minutes // default timeout for other actions to 2 minutes
Timeout = TimeSpan.FromMinutes(2) Timeout = TimeSpan.FromMinutes(2)
}); });
var webService = new WebService(new Uri(restUrl),restUser,restPassword);
//var challongeService = new ChallongeService(challongeToken);
var cts = new CancellationTokenSource(); var cts = new CancellationTokenSource();
DependencyCollection dep = null;
using (var d = new DependencyCollectionBuilder())
{
d.AddInstance(new Dependencies()
{
Cts = cts,
//ChallongeService = challongeService,
WebService = webService
});
dep = d.Build();
}
var _cnext = discord.UseCommandsNext(new CommandsNextConfiguration() var _cnext = discord.UseCommandsNext(new CommandsNextConfiguration()
{ {
CaseSensitive = false, //CaseSensitive = false,
EnableDefaultHelp = true, //EnableDefaultHelp = true,
EnableDms = false, //EnableDms = false,
EnableMentionPrefix = true, //EnableMentionPrefix = true,
StringPrefix = "!", StringPrefixes = new List<string>() { "/", "!" },
IgnoreExtraArguments = true, //IgnoreExtraArguments = true,
Dependencies = dep Services = service
}); });
_cnext.RegisterCommands<Result>();
//var slashCommands = discord.UseSlashCommands(new SlashCommandsConfiguration() {Services = service});
//slashCommands.RegisterCommands<SlashCommand>(guildId:null);
//_cnext.RegisterCommands<Result>();
_cnext.RegisterCommands<Public>(); _cnext.RegisterCommands<Public>();
_cnext.RegisterCommands<Shutdown>(); //_cnext.RegisterCommands<Shutdown>();
//_cnext.RegisterCommands<Todo>(); //_cnext.RegisterCommands<Todo>();
_cnext.RegisterCommands<Hokuto>(); _cnext.RegisterCommands<Hokuto>();
_cnext.RegisterCommands<BotEvent>(); _cnext.RegisterCommands<BotEvent>();
//discord.UserUpdated += async e => foreach (var registeredCommandsKey in discord.GetCommandsNext().RegisteredCommands.Keys)
//{
// await e.Client?.InitializeAsync();
//};
//discord.UserSettingsUpdated += async e =>
//{
// await e.Client?.InitializeAsync();
//};
//discord.GuildMemberUpdated += async e =>
//{
// await e.Client?.InitializeAsync();
//};
//discord.GuildRoleUpdated += async e =>
//{
// e.Client?.InitializeAsync();
//};
//discord.UserSettingsUpdated += async e =>
//{
// e.Client?.InitializeAsync();
//};
//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 =>
{ {
Console.WriteLine($"{e.Member.DisplayName} Joined"); Console.WriteLine(registeredCommandsKey);
await Task.Delay(0);
//await e.Guild.GetDefaultChannel().SendMessageAsync($"Bonjour!");
};
await discord.ConnectAsync();
while (!cts.IsCancellationRequested)
{
await Task.Delay(200);
//if(discord.GetConnectionsAsync().Result.Count)
} }
discord.Ready += (sender, eventArgs) =>
{
Console.WriteLine($"Bot READY.");
return Task.CompletedTask;
};
discord.GuildAvailable += (sender, eventArgs) =>
{
Console.WriteLine($"Joined Guild " + eventArgs.Guild.Name);
return Task.CompletedTask;
};
await discord.ConnectAsync();
await Task.Delay(Timeout.Infinite);
//while (!cts.IsCancellationRequested)
//{
// await Task.Delay(200);
// //if(discord.GetConnectionsAsync().Result.Count)
//}
} }
} }
//internal class SlashCommand : ApplicationCommandModule
//{
// [SlashCommand("test", "A slash command made to test the DSharpPlusSlashCommands library!")]
// public async Task TestCommand(InteractionContext ctx)
// {
// await ctx.CreateResponseAsync("Lol");
// }
//}
} }