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

The code compiles, but I haven't checked if it causes any errors. #1

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
446 changes: 221 additions & 225 deletions DiscordIntegration.Bot/Bot.cs

Large diffs are not rendered by default.

48 changes: 25 additions & 23 deletions DiscordIntegration.Bot/Commands/ClearQueueCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,34 @@ namespace DiscordIntegration.Bot.Commands;
using Discord.Interactions;

using DiscordIntegration.Bot.Services;
using System;
using System.Threading.Tasks;

public class ClearQueueCommand : InteractionModuleBase<SocketInteractionContext>
{
private readonly Bot bot;
private readonly Bot bot;

public ClearQueueCommand(Bot bot) => this.bot = bot;
public ClearQueueCommand(Bot bot) => this.bot = bot;

[SlashCommand($"clear", "Sends a command to the SCP server.")]
public async Task Send([Summary("command", "The command to send.")] string command)
{
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser) Context.User, bot.ServerNumber, command);
if (canRunCommand != ErrorCodes.None)
{
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand));
return;
}
try
{
bot.Messages.Clear();
await RespondAsync("Current message queue for ", ephemeral: true);
}
catch (Exception e)
{
Log.Error(bot.ServerNumber, nameof(Send), e);
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, e.Message));
}
}
[SlashCommand($"clear", "Sends a command to the SCP server.")]
public async Task Send([Summary("command", "The command to send.")] string command)
{
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, command);
if (canRunCommand != ErrorCodes.None)
{
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand));
return;
}

try
{
bot.Messages.Clear();
await RespondAsync("Current message queue for ", ephemeral: true);
}
catch (Exception e)
{
Log.Error(bot.ServerNumber, nameof(Send), e);
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, e.Message));
}
}
}
51 changes: 27 additions & 24 deletions DiscordIntegration.Bot/Commands/GetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,37 @@ namespace DiscordIntegration.Bot.Commands;

using Discord;
using Discord.Interactions;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

public class GetCommand : InteractionModuleBase<SocketInteractionContext>
{
private readonly Bot bot;
private readonly Bot bot;

public GetCommand(Bot bot) => this.bot = bot;

[SlashCommand("get", "Shows which commands you can execute.")]
public async Task ShowAvailableCommandsAsync()
{
if (!Program.Config.ValidCommands.ContainsKey(bot.ServerNumber))
{
await RespondAsync("This server was not found.", ephemeral: true);
return;
}
public GetCommand(Bot bot) => this.bot = bot;

List<string> availableCommands = new();
foreach (KeyValuePair<ulong, List<string>> commandList in Program.Config.ValidCommands[bot.ServerNumber])
foreach (string cmd in commandList.Value)
if (SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, cmd) ==0)
availableCommands.Add(cmd);
[SlashCommand("get", "Shows which commands you can execute.")]
public async Task ShowAvailableCommandsAsync()
{
if (!Program.Config.ValidCommands.ContainsKey(bot.ServerNumber))
{
await RespondAsync("This server was not found.", ephemeral: true);
return;
}

if (!availableCommands.Any())
{
await RespondAsync("There are no available commands for you.", ephemeral: true);
return;
}

await RespondAsync("You can use these commands:\n" + string.Join("\n", availableCommands), ephemeral: true);
}
List<string> availableCommands = new();
foreach (KeyValuePair<ulong, List<string>> commandList in Program.Config.ValidCommands[bot.ServerNumber])
foreach (string cmd in commandList.Value)
if (SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, cmd) == 0)
availableCommands.Add(cmd);

if (!availableCommands.Any())
{
await RespondAsync("There are no available commands for you.", ephemeral: true);
return;
}

await RespondAsync("You can use these commands:\n" + string.Join("\n", availableCommands), ephemeral: true);
}
}
54 changes: 26 additions & 28 deletions DiscordIntegration.Bot/Commands/SendCommand.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
namespace DiscordIntegration.Bot.Commands;

using System.Text;
using Dependency;

using Discord;
using Discord.Interactions;
using Newtonsoft.Json;
using Services;

using System;
using System.Threading.Tasks;
using ActionType = DiscordIntegration.Dependency.ActionType;

public class SendCommand : InteractionModuleBase<SocketInteractionContext>
{
private readonly Bot bot;
private readonly Bot bot;

public SendCommand(Bot bot) => this.bot = bot;

public SendCommand(Bot bot) => this.bot = bot;
[SlashCommand($"send", "Sends a command to the SCP server.")]
public async Task Send([Summary("command", "The command to send.")] string command)
{
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, command);
if (canRunCommand != ErrorCodes.None)
{
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true);
return;
}

[SlashCommand($"send", "Sends a command to the SCP server.")]
public async Task Send([Summary("command", "The command to send.")] string command)
{
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser) Context.User, bot.ServerNumber, command);
if (canRunCommand != ErrorCodes.None)
{
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true);
return;
}

try
{
Log.Debug(bot.ServerNumber, nameof(Send), $"Sending {command}");
await bot.Server.SendAsync(new RemoteCommand(ActionType.ExecuteCommand, Context.Channel.Id, command, Context.User.Id, Context.User.Username));
await RespondAsync("Command sent.", ephemeral: true);
}
catch (Exception e)
{
Log.Error(bot.ServerNumber, nameof(Send), e);
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, e.Message));
}
}
try
{
Log.Debug(bot.ServerNumber, nameof(Send), $"Sending {command}");
await bot.Server.SendAsync(new RemoteCommand(ActionType.ExecuteCommand, Context.Channel.Id, command, Context.User.Id, Context.User.Username));
await RespondAsync("Command sent.", ephemeral: true);
}
catch (Exception e)
{
Log.Error(bot.ServerNumber, nameof(Send), e);
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, e.Message));
}
}
}
140 changes: 72 additions & 68 deletions DiscordIntegration.Bot/Commands/SlashCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,93 @@
namespace DiscordIntegration.Bot.Commands;

using System.ComponentModel.Design;
using System.Reflection;
using Discord;
using Discord.Interactions;
using Discord.WebSocket;
using Services;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

public class SlashCommandHandler
{
private readonly InteractionService service;
private readonly DiscordSocketClient client;
private readonly ServiceContainer serviceContainer;
private readonly Bot bot;
private readonly InteractionService service;
private readonly DiscordSocketClient client;
private readonly ServiceContainer serviceContainer;
private readonly Bot bot;

public SlashCommandHandler(InteractionService service, DiscordSocketClient client, Bot bot)
{
this.service = service;
this.client = client;
serviceContainer = new();
serviceContainer.AddService(typeof(Bot), bot);
this.bot = bot;
}

public SlashCommandHandler(InteractionService service, DiscordSocketClient client, Bot bot)
{
this.service = service;
this.client = client;
serviceContainer = new();
serviceContainer.AddService(typeof(Bot), bot);
this.bot = bot;
}
public async Task InstallCommandsAsync()
{
await service.AddModulesAsync(Assembly.GetEntryAssembly(), serviceContainer);
client.InteractionCreated += HandleInteraction;
service.SlashCommandExecuted += HandleSlashCommand;
service.ContextCommandExecuted += HandleContextCommand;
service.ComponentCommandExecuted += HandleComponentCommand;
}

public async Task InstallCommandsAsync()
{
await service.AddModulesAsync(Assembly.GetEntryAssembly(), serviceContainer);
client.InteractionCreated += HandleInteraction;
service.SlashCommandExecuted += HandleSlashCommand;
service.ContextCommandExecuted += HandleContextCommand;
service.ComponentCommandExecuted += HandleComponentCommand;
}
private async Task HandleInteraction(SocketInteraction interaction)
{
try
{
SocketInteractionContext context = new(client, interaction);
await service.ExecuteCommandAsync(context, serviceContainer);
Log.Info(bot.ServerNumber, nameof(HandleInteraction), $"{interaction.User.Username} used an interaction.");
}
catch (Exception e)
{
Log.Error(bot.ServerNumber, nameof(HandleInteraction), e);
if (interaction.Type == InteractionType.ApplicationCommand)
await interaction.RespondAsync(
embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, e.Message));
}
}

private async Task HandleInteraction(SocketInteraction interaction)
{
try
{
SocketInteractionContext context = new(client, interaction);
await service.ExecuteCommandAsync(context, serviceContainer);
Log.Info(bot.ServerNumber, nameof(HandleInteraction), $"{interaction.User.Username} used an interaction.");
}
catch (Exception e)
{
Log.Error(bot.ServerNumber, nameof(HandleInteraction), e);
if (interaction.Type == InteractionType.ApplicationCommand)
await interaction.RespondAsync(
embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, e.Message));
}
}
private async Task HandleServiceError(IInteractionContext context, IResult result)
{
if (!result.IsSuccess)
{
if (result.Error == InteractionCommandError.UnknownCommand)
return;

private async Task HandleServiceError(IInteractionContext context, IResult result)
{
if (!result.IsSuccess)
{
if (result.Error == InteractionCommandError.UnknownCommand)
return;
await context.Interaction.RespondAsync(
embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, result.ErrorReason));
}
}

await context.Interaction.RespondAsync(
embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.Unspecified, result.ErrorReason));
}
}
private async Task HandleSlashCommand(SlashCommandInfo info, IInteractionContext context, IResult result) =>
await HandleServiceError(context, result);

private async Task HandleSlashCommand(SlashCommandInfo info, IInteractionContext context, IResult result) =>
await HandleServiceError(context, result);
private async Task HandleContextCommand(ContextCommandInfo info, IInteractionContext context, IResult result) =>
await HandleServiceError(context, result);

private async Task HandleContextCommand(ContextCommandInfo info, IInteractionContext context, IResult result) =>
await HandleServiceError(context, result);
private async Task HandleComponentCommand(ComponentCommandInfo info, IInteractionContext context, IResult result) =>
await HandleServiceError(context, result);

private async Task HandleComponentCommand(ComponentCommandInfo info, IInteractionContext context, IResult result) =>
await HandleServiceError(context, result);
public static ErrorCodes CanRunCommand(IGuildUser user, ushort serverNum, string command)
{
if (!Program.Config.ValidCommands.ContainsKey(serverNum) || Program.Config.ValidCommands[serverNum].Count == 0)
return ErrorCodes.InvalidCommand;

public static ErrorCodes CanRunCommand(IGuildUser user, ushort serverNum, string command)
{
if (!Program.Config.ValidCommands.ContainsKey(serverNum) || Program.Config.ValidCommands[serverNum].Count == 0)
return ErrorCodes.InvalidCommand;
foreach (KeyValuePair<ulong, List<string>> commandList in Program.Config.ValidCommands[serverNum])
{

foreach (KeyValuePair<ulong, List<string>> commandList in Program.Config.ValidCommands[serverNum])
{

if (!commandList.Value.Contains(command) && !commandList.Value.Any(command.StartsWith) && !commandList.Value.Contains(".*"))
return ErrorCodes.InvalidCommand;
if (user.Hierarchy >= user.Guild.GetRole(commandList.Key)?.Position)
return ErrorCodes.None;
}
if (!commandList.Value.Contains(command) && !commandList.Value.Any(command.StartsWith) && !commandList.Value.Contains(".*"))
return ErrorCodes.InvalidCommand;
if (user.Hierarchy >= user.Guild.GetRole(commandList.Key)?.Position)
return ErrorCodes.None;
}

return ErrorCodes.PermissionDenied;
}
return ErrorCodes.PermissionDenied;
}
}
Loading