-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
315 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using DiscordIntegration.Bot.Services; | ||
using DiscordIntegration.Dependency.Database; | ||
|
||
namespace DiscordIntegration.Bot.Commands; | ||
|
||
using Discord; | ||
using Discord.Interactions; | ||
|
||
[Group("watchlist", "Commands for managing the watchlist.")] | ||
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"); | ||
if (canRunCommand != ErrorCodes.None) | ||
{ | ||
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true); | ||
return; | ||
} | ||
|
||
if (DatabaseHandler.CheckWatchlist(userId, out string res)) | ||
{ | ||
await RespondAsync( | ||
embed: await EmbedBuilderService.CreateBasicEmbed("User already on Watchlist", | ||
$"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.", | ||
Color.Orange), ephemeral: true); | ||
return; | ||
} | ||
|
||
DatabaseHandler.AddEntry(userId, reason); | ||
await RespondAsync( | ||
embed: await EmbedBuilderService.CreateBasicEmbed("User added to Watchlist", | ||
$"The userID {userId} has been added to the watchlist for {reason}", Color.Green), ephemeral: true); | ||
} | ||
|
||
[SlashCommand("remove", "Removes a UserID from the watchlist.")] | ||
public async Task RemoveFromWatchlist([Summary("UserID", "The user ID of the player to remove from the watchlist.")] string userId) | ||
{ | ||
ErrorCodes canRunCommand = | ||
SlashCommandHandler.CanRunCommand((IGuildUser)Context.User, bot.ServerNumber, "watchlist"); | ||
if (canRunCommand != ErrorCodes.None) | ||
{ | ||
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(canRunCommand), ephemeral: true); | ||
return; | ||
} | ||
|
||
if (!DatabaseHandler.CheckWatchlist(userId, out string _)) | ||
{ | ||
await RespondAsync(embed: await ErrorHandlingService.GetErrorEmbed(ErrorCodes.NoRecordForUserFound), ephemeral: true); | ||
return; | ||
} | ||
|
||
DatabaseHandler.RemoveEntry(userId); | ||
await RespondAsync( | ||
embed: await EmbedBuilderService.CreateBasicEmbed("User removed from watchlist.", | ||
$"User {userId} has been removed from the watchlist.", Color.Green), ephemeral: true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ public enum ChannelType | |
Bans, | ||
Reports, | ||
StaffCopy, | ||
Errors | ||
Errors, | ||
Watchlist | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using System.IO; | ||
using Microsoft.Data.Sqlite; | ||
|
||
namespace DiscordIntegration.Dependency.Database; | ||
|
||
public class DatabaseHandler | ||
{ | ||
private static string _connectionString = | ||
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "DiscordIntegration.db"); | ||
|
||
public static void Init() | ||
{ | ||
using SqliteConnection conn = new(_connectionString); | ||
conn.Open(); | ||
|
||
using (SqliteCommand cmd = conn.CreateCommand()) | ||
{ | ||
cmd.CommandText = | ||
"CREATE TABLE IF NOT EXISTS Watchlist(Id INTEGER PRIMARY KEY AUTOINCREMENT, UserId TEXT, Reason TEXT)"; | ||
cmd.ExecuteNonQuery(); | ||
} | ||
|
||
conn.Close(); | ||
} | ||
|
||
public static void AddEntry(string userId, string reason) | ||
{ | ||
using SqliteConnection conn = new(_connectionString); | ||
conn.Open(); | ||
|
||
using (SqliteCommand cmd = conn.CreateCommand()) | ||
{ | ||
cmd.CommandText = "INSERT INTO Watchlist(UserId, Reason) VALUES(@id, @reason)"; | ||
cmd.Parameters.AddWithValue("@id", userId); | ||
cmd.Parameters.AddWithValue("@reason", reason); | ||
cmd.ExecuteNonQuery(); | ||
} | ||
conn.Close(); | ||
} | ||
|
||
|
||
public static void RemoveEntry(string userId) | ||
{ | ||
using SqliteConnection conn = new(_connectionString); | ||
conn.Open(); | ||
|
||
using (SqliteCommand cmd = conn.CreateCommand()) | ||
{ | ||
cmd.CommandText = "DELETE FROM Watchlist WHERE UserId=@id"; | ||
cmd.Parameters.AddWithValue("@id", userId); | ||
|
||
cmd.ExecuteNonQuery(); | ||
} | ||
|
||
conn.Close(); | ||
} | ||
|
||
public static bool CheckWatchlist(string userId, out string reason) | ||
{ | ||
reason = "Not in watchlist"; | ||
using SqliteConnection conn = new(_connectionString); | ||
conn.Open(); | ||
|
||
using (SqliteCommand cmd = conn.CreateCommand()) | ||
{ | ||
cmd.CommandText = "SELECT * FROM Watchlist WHERE UserId=@id"; | ||
cmd.Parameters.AddWithValue("@id", userId); | ||
|
||
using (SqliteDataReader reader = cmd.ExecuteReader()) | ||
{ | ||
while (reader.Read()) | ||
{ | ||
reason = reader.GetString(2); | ||
return true; | ||
} | ||
} | ||
} | ||
conn.Close(); | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" /> | ||
<package id="Microsoft.Data.Sqlite" version="7.0.0" targetFramework="net472" /> | ||
</packages> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// ----------------------------------------------------------------------- | ||
// <copyright file="PlayerList.cs" company="Exiled Team"> | ||
// Copyright (c) Exiled Team. All rights reserved. | ||
// Licensed under the CC BY-SA 3.0 license. | ||
// </copyright> | ||
// ----------------------------------------------------------------------- | ||
|
||
using DiscordIntegration.Dependency.Database; | ||
|
||
namespace DiscordIntegration.Commands | ||
{ | ||
using System; | ||
using System.Text; | ||
using CommandSystem; | ||
using Exiled.API.Features; | ||
using Exiled.Permissions.Extensions; | ||
using NorthwoodLib.Pools; | ||
using static DiscordIntegration; | ||
|
||
/// <summary> | ||
/// Adds a user to the watchlist. | ||
/// </summary> | ||
internal sealed class WatchlistAdd : ICommand | ||
{ | ||
#pragma warning disable SA1600 // Elements should be documented | ||
private WatchlistAdd() | ||
{ | ||
} | ||
|
||
public static WatchlistAdd Instance { get; } = new WatchlistAdd(); | ||
|
||
public string Command { get; } = "watchadd"; | ||
|
||
public string[] Aliases { get; } = new[] { "wla" }; | ||
|
||
public string Description { get; } = Language.WatchlistAddDescription; | ||
|
||
public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response) | ||
{ | ||
if (!sender.CheckPermission("di.watchlistadd")) | ||
{ | ||
response = string.Format(Language.NotEnoughPermissions, "di.watchlistadd"); | ||
return false; | ||
} | ||
|
||
Player player = Player.Get(arguments.ElementAt(2)); | ||
string reason = string.Empty; | ||
foreach (string s in arguments.Skip(2)) | ||
reason += $"{s} "; | ||
reason = reason.TrimEnd(' '); | ||
DatabaseHandler.AddEntry(player.UserId, reason); | ||
response = $"{player.Nickname} added to watchlist for {reason}"; | ||
return true; | ||
} | ||
} | ||
} |
Oops, something went wrong.