Skip to content

Commit f1a9327

Browse files
authored
Add watchlist feature (#133)
1 parent e208977 commit f1a9327

File tree

19 files changed

+315
-45
lines changed

19 files changed

+315
-45
lines changed

DiscordIntegration.Bot/Bot.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using DiscordIntegration.Dependency.Database;
2+
13
namespace DiscordIntegration.Bot;
24

35
using System.Collections.Specialized;
@@ -56,6 +58,8 @@ private async Task Init()
5658
return;
5759
}
5860

61+
DatabaseHandler.Init();
62+
5963
Log.Debug(ServerNumber, nameof(Init), "Setting up commands...");
6064
InteractionService = new(Client, new InteractionServiceConfig()
6165
{

DiscordIntegration.Bot/Commands/SendCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public async Task Send([Summary("command", "The command to send.")] string comma
2222
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser) Context.User, bot.ServerNumber, command);
2323
if (canRunCommand != ErrorCodes.None)
2424
{
25-
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand));
25+
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true);
2626
return;
2727
}
2828

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using DiscordIntegration.Bot.Services;
2+
using DiscordIntegration.Dependency.Database;
3+
4+
namespace DiscordIntegration.Bot.Commands;
5+
6+
using Discord;
7+
using Discord.Interactions;
8+
9+
[Group("watchlist", "Commands for managing the watchlist.")]
10+
public class WatchlistCommands: InteractionModuleBase<SocketInteractionContext>
11+
{
12+
private readonly Bot bot;
13+
14+
public WatchlistCommands(Bot bot) => this.bot = bot;
15+
16+
[SlashCommand("add", "Adds a UserID to the watchlist.")]
17+
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)
18+
{
19+
ErrorCodes canRunCommand = SlashCommandHandler.CanRunCommand((IGuildUser) Context.User, bot.ServerNumber, "watchlist");
20+
if (canRunCommand != ErrorCodes.None)
21+
{
22+
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true);
23+
return;
24+
}
25+
26+
if (DatabaseHandler.CheckWatchlist(userId, out string res))
27+
{
28+
await RespondAsync(
29+
embed: await EmbedBuilderService.CreateBasicEmbed("User already on Watchlist",
30+
$"The userID {userId} is already on the watchlist for {reason}. If you wish to change the reason, please remove the user first then re-add them.",
31+
Color.Orange), ephemeral: true);
32+
return;
33+
}
34+
35+
DatabaseHandler.AddEntry(userId, reason);
36+
await RespondAsync(
37+
embed: await EmbedBuilderService.CreateBasicEmbed("User added to Watchlist",
38+
$"The userID {userId} has been added to the watchlist for {reason}", Color.Green), ephemeral: true);
39+
}
40+
41+
[SlashCommand("remove", "Removes a UserID from the watchlist.")]
42+
public async Task RemoveFromWatchlist([Summary("UserID", "The user ID of the player to remove from the watchlist.")] string userId)
43+
{
44+
ErrorCodes canRunCommand =
45+
SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, "watchlist");
46+
if (canRunCommand != ErrorCodes.None)
47+
{
48+
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true);
49+
return;
50+
}
51+
52+
if (!DatabaseHandler.CheckWatchlist(userId, out string _))
53+
{
54+
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.NoRecordForUserFound), ephemeral: true);
55+
return;
56+
}
57+
58+
DatabaseHandler.RemoveEntry(userId);
59+
await RespondAsync(
60+
embed: await EmbedBuilderService.CreateBasicEmbed("User removed from watchlist.",
61+
$"User {userId} has been removed from the watchlist.", Color.Green), ephemeral: true);
62+
}
63+
}

DiscordIntegration.Bot/Config.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public class Config
8787
LogType = LogType.Embed
8888
},
8989
},
90+
Watchlist = new List<LogChannel>
91+
{
92+
new()
93+
{
94+
Id = 0,
95+
LogType = LogType.Embed
96+
},
97+
},
9098
}
9199
}
92100
}

DiscordIntegration.Bot/ConfigObjects/LogChannels.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class LogChannels
1010
public List<LogChannel>? Reports { get; set; } = new();
1111
public List<LogChannel>? StaffCopy { get; set; } = new();
1212
public List<LogChannel>? Errors { get; set; } = new();
13+
public List<LogChannel>? Watchlist { get; set; } = new();
1314

1415
public IEnumerable<LogChannel> this[ChannelType result]
1516
{
@@ -22,6 +23,7 @@ public IEnumerable<LogChannel> this[ChannelType result]
2223
ChannelType.Reports => Reports,
2324
ChannelType.StaffCopy => StaffCopy,
2425
ChannelType.Errors => Errors,
26+
ChannelType.Watchlist => Watchlist,
2527
_ => throw new ArgumentOutOfRangeException(nameof(result), result, null)
2628
})!;
2729
}

DiscordIntegration.Bot/DiscordIntegration.Bot.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<TargetFramework>net6.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8-
<AssemblyVersion>1.5.1</AssemblyVersion>
9-
<FileVersion>1.5.1</FileVersion>
8+
<AssemblyVersion>1.6.0</AssemblyVersion>
9+
<FileVersion>1.6.0</FileVersion>
1010
</PropertyGroup>
1111

1212
<ItemGroup>

DiscordIntegration.Dependency/ChannelType.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ public enum ChannelType
77
Bans,
88
Reports,
99
StaffCopy,
10-
Errors
10+
Errors,
11+
Watchlist
1112
}
1213
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System;
2+
using System.IO;
3+
using Microsoft.Data.Sqlite;
4+
5+
namespace DiscordIntegration.Dependency.Database;
6+
7+
public class DatabaseHandler
8+
{
9+
private static string _connectionString =
10+
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DiscordIntegration.db");
11+
12+
public static void Init()
13+
{
14+
using SqliteConnection conn = new(_connectionString);
15+
conn.Open();
16+
17+
using (SqliteCommand cmd = conn.CreateCommand())
18+
{
19+
cmd.CommandText =
20+
"CREATE TABLE IF NOT EXISTS Watchlist(Id INTEGER PRIMARY KEY AUTOINCREMENT, UserId TEXT, Reason TEXT)";
21+
cmd.ExecuteNonQuery();
22+
}
23+
24+
conn.Close();
25+
}
26+
27+
public static void AddEntry(string userId, string reason)
28+
{
29+
using SqliteConnection conn = new(_connectionString);
30+
conn.Open();
31+
32+
using (SqliteCommand cmd = conn.CreateCommand())
33+
{
34+
cmd.CommandText = "INSERT INTO Watchlist(UserId, Reason) VALUES(@id, @reason)";
35+
cmd.Parameters.AddWithValue("@id", userId);
36+
cmd.Parameters.AddWithValue("@reason", reason);
37+
cmd.ExecuteNonQuery();
38+
}
39+
conn.Close();
40+
}
41+
42+
43+
public static void RemoveEntry(string userId)
44+
{
45+
using SqliteConnection conn = new(_connectionString);
46+
conn.Open();
47+
48+
using (SqliteCommand cmd = conn.CreateCommand())
49+
{
50+
cmd.CommandText = "DELETE FROM Watchlist WHERE UserId=@id";
51+
cmd.Parameters.AddWithValue("@id", userId);
52+
53+
cmd.ExecuteNonQuery();
54+
}
55+
56+
conn.Close();
57+
}
58+
59+
public static bool CheckWatchlist(string userId, out string reason)
60+
{
61+
reason = "Not in watchlist";
62+
using SqliteConnection conn = new(_connectionString);
63+
conn.Open();
64+
65+
using (SqliteCommand cmd = conn.CreateCommand())
66+
{
67+
cmd.CommandText = "SELECT * FROM Watchlist WHERE UserId=@id";
68+
cmd.Parameters.AddWithValue("@id", userId);
69+
70+
using (SqliteDataReader reader = cmd.ExecuteReader())
71+
{
72+
while (reader.Read())
73+
{
74+
reason = reader.GetString(2);
75+
return true;
76+
}
77+
}
78+
}
79+
conn.Close();
80+
return false;
81+
}
82+
}

DiscordIntegration.Dependency/DiscordIntegration.Dependency.csproj

100755100644
Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,10 @@
1111
<FileAlignment>512</FileAlignment>
1212
<LangVersion>10</LangVersion>
1313
<Nullable>enable</Nullable>
14-
<TargetFramework>net472</TargetFramework>
15-
<AssemblyVersion>1.4.2</AssemblyVersion>
16-
<FileVersion>1.4.2</FileVersion>
14+
<AssemblyVersion>1.5.0</AssemblyVersion>
15+
<FileVersion>1.5.0</FileVersion>
1716
<OutputPath>../bin/Plugin/dependencies</OutputPath>
18-
</PropertyGroup>
19-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
20-
<PlatformTarget>AnyCPU</PlatformTarget>
21-
<DebugSymbols>true</DebugSymbols>
22-
<DebugType>full</DebugType>
23-
<Optimize>false</Optimize>
24-
<OutputPath>../bin/Plugin/dependencies/</OutputPath>
25-
<DefineConstants>DEBUG;TRACE</DefineConstants>
26-
<ErrorReport>prompt</ErrorReport>
27-
<WarningLevel>4</WarningLevel>
28-
</PropertyGroup>
29-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30-
<PlatformTarget>AnyCPU</PlatformTarget>
31-
<DebugType>pdbonly</DebugType>
32-
<Optimize>true</Optimize>
33-
<OutputPath>../bin/Plugin/dependencies/</OutputPath>
34-
<DefineConstants>TRACE</DefineConstants>
35-
<ErrorReport>prompt</ErrorReport>
36-
<WarningLevel>4</WarningLevel>
17+
<TargetFramework>net472</TargetFramework>
3718
</PropertyGroup>
3819
<ItemGroup>
3920
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
@@ -48,15 +29,8 @@
4829
<None Include="packages.config" />
4930
</ItemGroup>
5031
<ItemGroup>
32+
<PackageReference Include="Microsoft.Data.Sqlite" Version="7.0.0" />
5133
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
5234
</ItemGroup>
53-
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
55-
Other similar extension points exist, see Microsoft.Common.targets.
56-
<Target Name="BeforeBuild">
57-
</Target>
58-
<Target Name="AfterBuild">
59-
</Target>
60-
-->
6135

6236
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
4+
<package id="Microsoft.Data.Sqlite" version="7.0.0" targetFramework="net472" />
45
</packages>

0 commit comments

Comments
 (0)