From b4340d573edd1e2ff73ba955a86c5276dfba2b29 Mon Sep 17 00:00:00 2001 From: Osvaldon Date: Sat, 20 Jan 2024 17:34:00 +0200 Subject: [PATCH] Converted the !login command into the /login slash command. --- .../Commands/AuthenticationCommands.cs | 43 ++++++++++ DiscordBot.App/Program.cs | 1 - DiscordBot.App/Services/DiscordService.cs | 4 +- DiscordBot.App/Services/LoginService.cs | 79 ------------------- DiscordBot.App/Services/RunnerService.cs | 4 +- 5 files changed, 46 insertions(+), 85 deletions(-) create mode 100644 DiscordBot.App/Commands/AuthenticationCommands.cs delete mode 100644 DiscordBot.App/Services/LoginService.cs diff --git a/DiscordBot.App/Commands/AuthenticationCommands.cs b/DiscordBot.App/Commands/AuthenticationCommands.cs new file mode 100644 index 0000000..dccfd44 --- /dev/null +++ b/DiscordBot.App/Commands/AuthenticationCommands.cs @@ -0,0 +1,43 @@ +using DiscordBot.Apis; +using DiscordBot.Services; +using DSharpPlus; +using DSharpPlus.EventArgs; +using DSharpPlus.SlashCommands; +using System.Xml.Linq; + +namespace DiscordBot.Commands; + +public class AuthenticationCommands : ApplicationCommandModule +{ + private readonly DiscordService _discordService; + private readonly IServerDiscordApi _serverDiscordApi; + + public AuthenticationCommands(DiscordService discordService, IServerDiscordApi serverDiscordApi) + { + _discordService = discordService; + _serverDiscordApi = serverDiscordApi; + } + + [SlashCommand("login", "Use to link your Discord user to an in-game account. Use via direct message only!", defaultPermission: false)] + public async Task LoginCommand(InteractionContext ctx, [Option("account", "your in-game account name")] string username, [Option("password", "the password for your in-game account")] string password) + { + if (ctx.Channel.Type != ChannelType.Private) + { + // Do not provide an interaction context response so the command parameters remain hidden to others. Send a direct message instead. + await ctx.Member.SendMessageAsync("Please do not use the **/login** command in public channels as it could compromise your in-game account! It is also recommended to click **\"Dismiss message\"** on all of the notifications that contain the bot command to permanently hide your credentials."); + return; + } + + var discordMember = await _discordService.Guild.GetMemberAsync(ctx.User.Id); + + if (discordMember != null) + { + var response = await _serverDiscordApi.PostLogin(username, discordMember.Id, discordMember.Username, discordMember.Discriminator, discordMember.AvatarUrl, password); + + if (response.Status) + await discordMember.GrantRoleAsync(_discordService.MemberRole); + + await ctx.CreateResponseAsync(response.Message); + } + } +} diff --git a/DiscordBot.App/Program.cs b/DiscordBot.App/Program.cs index edcd618..483f3e1 100644 --- a/DiscordBot.App/Program.cs +++ b/DiscordBot.App/Program.cs @@ -25,7 +25,6 @@ builder.Services.AddControllers(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSingleton(); -builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHostedService(provider => provider.GetService()); diff --git a/DiscordBot.App/Services/DiscordService.cs b/DiscordBot.App/Services/DiscordService.cs index cb5e11f..3832f29 100644 --- a/DiscordBot.App/Services/DiscordService.cs +++ b/DiscordBot.App/Services/DiscordService.cs @@ -66,10 +66,10 @@ public async Task Start() }; var slashCommands = Client.UseSlashCommands(slashConfig); - + slashCommands.RegisterCommands(); slashCommands.RegisterCommands(); slashCommands.RegisterCommands(); - + await Client.InitializeAsync(); await Client.ConnectAsync(); diff --git a/DiscordBot.App/Services/LoginService.cs b/DiscordBot.App/Services/LoginService.cs deleted file mode 100644 index c6b35c8..0000000 --- a/DiscordBot.App/Services/LoginService.cs +++ /dev/null @@ -1,79 +0,0 @@ -using DiscordBot.Apis; -using DSharpPlus; -using DSharpPlus.EventArgs; - -namespace DiscordBot.Services; - -public class LoginService -{ - private readonly DiscordService _discordService; - private readonly IServerDiscordApi _serverDiscordApi; - - public LoginService(DiscordService discordService, IServerDiscordApi serverDiscordApi) - { - _discordService = discordService; - _serverDiscordApi = serverDiscordApi; - - discordService.Client.MessageCreated += OnMessageCreated; - } - - private async Task OnMessageCreated(DiscordClient client, MessageCreateEventArgs e) - { - if (e.Author.IsBot) - return; - - if (e.Channel.Type == ChannelType.Private) - { - await OnPrivateMessage(e); - } - } - - private async Task HandleLoginCommand(MessageCreateEventArgs e, string name, string password) - { - var discordMember = await _discordService.Guild.GetMemberAsync(e.Author.Id); - - if (discordMember == null) - { - await e.Channel.SendMessageAsync("You are not a member of our Discord server!"); - return; - } - - var response = await _serverDiscordApi.PostLogin(name, discordMember.Id, discordMember.Username, discordMember.Discriminator, discordMember.AvatarUrl, password); - - if(response.Status) - { - await discordMember.GrantRoleAsync(_discordService.MemberRole); - } - - await e.Message.RespondAsync(response.Message); - } - - private async Task OnPrivateMessage(MessageCreateEventArgs e) - { - var message = e.Message; - var content = message.Content; - - var trimmed = content.TrimStart(); - - if (trimmed.StartsWith("!login")) - { - var input = trimmed.Remove(0, 7); - var index = input.IndexOf(' '); - - if (index == -1) - { - await e.Message.RespondAsync("Incorrect format! Write **!login USERNAME PASSWORD**"); - return; - } - - var accountName = input[..index]; - var password = input.Substring(index + 1, input.Length - index - 1); - - await HandleLoginCommand(e, accountName, password); - } - else - { - await e.Message.RespondAsync("Incorrect format! Write **!login USERNAME PASSWORD**"); - } - } -} diff --git a/DiscordBot.App/Services/RunnerService.cs b/DiscordBot.App/Services/RunnerService.cs index ab5c845..818270f 100644 --- a/DiscordBot.App/Services/RunnerService.cs +++ b/DiscordBot.App/Services/RunnerService.cs @@ -7,15 +7,13 @@ namespace DiscordBot.Services; public class RunnerService : BackgroundService { private readonly DiscordService _discordService; - private readonly LoginService _loginService; private readonly GuildJoinService _guildJoinService; private readonly IServerDiscordApi _serverApi; - public RunnerService(DiscordService discordService, LoginService loginService, GuildJoinService guildJoinService, IServerDiscordApi serverApi) + public RunnerService(DiscordService discordService, GuildJoinService guildJoinService, IServerDiscordApi serverApi) { _discordService = discordService; - _loginService = loginService; _guildJoinService = guildJoinService; _serverApi = serverApi; }