Skip to content

Commit

Permalink
Merge pull request #23 from eclipserporg/feature/authentication
Browse files Browse the repository at this point in the history
Converted the !login command into the /login slash command.
  • Loading branch information
Osvaldon authored Jan 20, 2024
2 parents a3a9961 + b4340d5 commit 8a58021
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 85 deletions.
43 changes: 43 additions & 0 deletions DiscordBot.App/Commands/AuthenticationCommands.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 0 additions & 1 deletion DiscordBot.App/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSingleton<DiscordService>();
builder.Services.AddSingleton<LoginService>();
builder.Services.AddSingleton<RunnerService>();
builder.Services.AddSingleton<GuildJoinService>();
builder.Services.AddHostedService(provider => provider.GetService<RunnerService>());
Expand Down
4 changes: 2 additions & 2 deletions DiscordBot.App/Services/DiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public async Task Start()
};

var slashCommands = Client.UseSlashCommands(slashConfig);

slashCommands.RegisterCommands<AuthenticationCommands>();
slashCommands.RegisterCommands<GeneralCommands>();
slashCommands.RegisterCommands<RestartCommands>();

await Client.InitializeAsync();
await Client.ConnectAsync();

Expand Down
79 changes: 0 additions & 79 deletions DiscordBot.App/Services/LoginService.cs

This file was deleted.

4 changes: 1 addition & 3 deletions DiscordBot.App/Services/RunnerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 8a58021

Please sign in to comment.