Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted the !login command into the /login slash command. #23

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading