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 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
26 changes: 11 additions & 15 deletions DiscordIntegration.Bot/Bot.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using DiscordIntegration.Dependency.Database;

namespace DiscordIntegration.Bot;

using System.Collections.Specialized;
using System.Net;
using System.Net.Sockets;
using API.EventArgs.Network;
using Commands;
using Dependency;
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;

using DiscordIntegration.Bot.ConfigObjects;

using Newtonsoft.Json;
using Services;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using ActionType = Dependency.ActionType;
using ChannelType = Dependency.ChannelType;

Expand Down Expand Up @@ -59,7 +55,7 @@ private async Task Init()
}

DatabaseHandler.Init();

Log.Debug(ServerNumber, nameof(Init), "Setting up commands...");
InteractionService = new(Client, new InteractionServiceConfig()
{
Expand Down Expand Up @@ -133,7 +129,7 @@ private async void OnReceived(object? sender, ReceivedFullEventArgs ev)
case ActionType.UpdateActivity:
string commandMessage = string.Empty;
foreach (object obj in command.Parameters)
commandMessage += (string) obj + " ";
commandMessage += (string)obj + " ";
Log.Debug(ServerNumber, nameof(OnReceived), $"Updating activity status.. {commandMessage}");
try
{
Expand All @@ -149,7 +145,7 @@ private async void OnReceived(object? sender, ReceivedFullEventArgs ev)
Log.Error(ServerNumber, nameof(ActionType.UpdateActivity), $"Error parsing player total {split[1]}");
return;
}

switch (count)
{
case > 0 when Client.Status != UserStatus.Online:
Expand All @@ -159,7 +155,7 @@ private async void OnReceived(object? sender, ReceivedFullEventArgs ev)
await Client.SetStatusAsync(UserStatus.AFK);
break;
}

Log.Debug(ServerNumber, nameof(OnReceived), $"Status message count: {count}");
Log.Debug(ServerNumber, nameof(OnReceived), $"Status message total: {total}");
if (count != lastCount || total != lastTotal)
Expand All @@ -181,8 +177,8 @@ private async void OnReceived(object? sender, ReceivedFullEventArgs ev)
foreach (ulong channelId in Program.Config.Channels[ServerNumber].TopicInfo)
{
SocketTextChannel channel = Guild.GetTextChannel(channelId);
if (channel is not null)
await channel.ModifyAsync(x => x.Topic = (string) command.Parameters[0]);
if (channel is not null)
await channel.ModifyAsync(x => x.Topic = (string)command.Parameters[0]);
}

break;
Expand All @@ -191,14 +187,14 @@ private async void OnReceived(object? sender, ReceivedFullEventArgs ev)
catch (Exception e)
{
Log.Error(ServerNumber, nameof(OnReceived), e.Message);
if (e.StackTrace is not null)
if (e.StackTrace is not null)
Log.Error(ServerNumber, nameof(OnReceived), e.StackTrace);
}
}

private async Task DequeueMessages()
{
for (;;)
for (; ; )
{
Log.Debug(ServerNumber, nameof(DequeueMessages), "Dequeue loop");
List<KeyValuePair<LogChannel, string>> toSend = new();
Expand Down
7 changes: 4 additions & 3 deletions DiscordIntegration.Bot/Commands/ClearQueueCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ namespace DiscordIntegration.Bot.Commands;

using Discord;
using Discord.Interactions;

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

public class ClearQueueCommand : InteractionModuleBase<SocketInteractionContext>
{
Expand All @@ -14,13 +15,13 @@ public class ClearQueueCommand : InteractionModuleBase<SocketInteractionContext>
[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);
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();
Expand Down
13 changes: 8 additions & 5 deletions DiscordIntegration.Bot/Commands/GetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ 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;

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

[SlashCommand("get", "Shows which commands you can execute.")]
public async Task ShowAvailableCommandsAsync()
{
Expand All @@ -20,16 +23,16 @@ public async Task ShowAvailableCommandsAsync()

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);
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);
}
}
10 changes: 4 additions & 6 deletions DiscordIntegration.Bot/Commands/SendCommand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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>
Expand All @@ -19,13 +17,13 @@ public class SendCommand : InteractionModuleBase<SocketInteractionContext>
[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);
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}");
Expand Down
10 changes: 7 additions & 3 deletions DiscordIntegration.Bot/Commands/SlashCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
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
{
Expand Down Expand Up @@ -77,7 +81,7 @@ public static ErrorCodes CanRunCommand(IGuildUser user, ushort serverNum, string

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)
Expand Down
11 changes: 6 additions & 5 deletions DiscordIntegration.Bot/Commands/WatchlistCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ namespace DiscordIntegration.Bot.Commands;

using Discord;
using Discord.Interactions;
using System.Threading.Tasks;

[Group("watchlist", "Commands for managing the watchlist.")]
public class WatchlistCommands: InteractionModuleBase<SocketInteractionContext>
public class WatchlistCommands : InteractionModuleBase<SocketInteractionContext>
{
private readonly Bot bot;

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

[SlashCommand("add", "Adds a UserID to the watchlist.")]
public async Task AddToWatchlist([Summary("UserId", "The user ID of the player to watch.")] string userId, [Summary("Reason", "The reason they should be watched.")] string reason)
{
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser) Context.User, bot.ServerNumber, "watchlist");
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, "watchlist");
if (canRunCommand != ErrorCodes.None)
{
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true);
Expand All @@ -31,7 +32,7 @@ await RespondAsync(
Color.Orange), ephemeral: true);
return;
}

DatabaseHandler.AddEntry(userId, reason);
await RespondAsync(
embed: await EmbedBuilderService.CreateBasicEmbed("User added to Watchlist",
Expand All @@ -54,7 +55,7 @@ public async Task RemoveFromWatchlist([Summary("UserID", "The user ID of the pla
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.NoRecordForUserFound), ephemeral: true);
return;
}

DatabaseHandler.RemoveEntry(userId);
await RespondAsync(
embed: await EmbedBuilderService.CreateBasicEmbed("User removed from watchlist.",
Expand Down
11 changes: 6 additions & 5 deletions DiscordIntegration.Bot/Config.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace DiscordIntegration.Bot;

using ConfigObjects;
using System.Collections.Generic;

public class Config
{
Expand All @@ -22,7 +23,7 @@ public class Config
"bot-token-here"
}
},

Channels = new Dictionary<ushort, ChannelConfig>
{
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public class Config
}
}
},

ValidCommands = new Dictionary<ushort, Dictionary<ulong, List<string>>>
{
{
Expand All @@ -114,14 +115,14 @@ public class Config
}
}
},

DiscordServerIds = new Dictionary<ushort, ulong>
{
{
1, 0
}
},

TcpServers = new Dictionary<ushort, TcpServerConfig>
{
{
Expand All @@ -132,7 +133,7 @@ public class Config
}
}
},

KeepAliveInterval = 2000,
MessageDelay = 1000,
Debug = false,
Expand Down
2 changes: 2 additions & 0 deletions DiscordIntegration.Bot/ConfigObjects/ChannelConfig.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace DiscordIntegration.Bot.ConfigObjects;

public class ChannelConfig
Expand Down
2 changes: 2 additions & 0 deletions DiscordIntegration.Bot/ConfigObjects/LogChannels.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace DiscordIntegration.Bot.ConfigObjects;

using Dependency;
using System;
using System.Collections.Generic;

public class LogChannels
{
Expand Down
8 changes: 4 additions & 4 deletions DiscordIntegration.Bot/DiscordIntegration.Bot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyVersion>1.6.0</AssemblyVersion>
<FileVersion>1.6.0</FileVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net" Version="3.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2-beta1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />
<PackageReference Include="Discord.Net" Version="3.17.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpZipLib" Version="1.4.2" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 10 additions & 10 deletions DiscordIntegration.Bot/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
namespace DiscordIntegration.Bot;

using System.Text.RegularExpressions;
using Discord.WebSocket;
using System.Text.RegularExpressions;

public static class Extensions
{
public static string SplitCamelCase(this string str)
{
return Regex.Replace(
Regex.Replace(
str,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
return Regex.Replace(
Regex.Replace(
str,
@"(\P{Ll})(\P{Ll}\p{Ll})",
"$1 $2"
),
@"(\p{Ll})(\P{Ll})",
"$1 $2"
);
}

public static string GetUsername(this SocketGuild guild, ulong userId) => !string.IsNullOrEmpty(guild.GetUser(userId)?.Username) ? guild.GetUser(userId).Username : $"Name unavailable. ({userId})";
}
Loading