Skip to content

Commit

Permalink
v1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
NockyCZ committed Mar 24, 2024
1 parent 68ead3b commit f1cea0f
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 19 deletions.
55 changes: 51 additions & 4 deletions src/Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,40 @@ public class DUConfig : BasePluginConfig
[JsonPropertyName("Link Section")] public Link Link { get; set; } = new Link();
[JsonPropertyName("Ingame Chatlog Section")] public Chatlog Chatlog { get; set; } = new Chatlog();
[JsonPropertyName("Discord Relay Section")] public DiscordRelay DiscordRelay { get; set; } = new DiscordRelay();
[JsonPropertyName("Rcon Section")] public Rcon Rcon { get; set; } = new Rcon();
[JsonPropertyName("Server Status Section")] public ServerStatus ServerStatus { get; set; } = new ServerStatus();
[JsonPropertyName("Conneced Players Role")] public ConnectedPlayers ConnectedPlayers { get; set; } = new ConnectedPlayers();
[JsonPropertyName("Event Notifications")] public EventNotifications EventNotifications { get; set; } = new EventNotifications();
[JsonPropertyName("Manage Roles and Permissions")] public CustomFlagsAndRoles CustomFlagsAndRoles { get; set; } = new CustomFlagsAndRoles();
[JsonPropertyName("ConfigVersion")] public new int Version { get; set; } = 3;
}

public class Rcon
{
[JsonPropertyName("Enabled")] public bool Enabled { get; set; } = false;
[JsonPropertyName("Admin Role ID")] public string AdminRoleId { get; set; } = "";
[JsonPropertyName("Servers List")] public string ServerList { get; set; } = "Only Mirage,Public,AWP";
[JsonPropertyName("Server")] public string Server { get; set; } = "Public";
[JsonPropertyName("Discord Rcon Command")] public string Command { get; set; } = "rcon";
[JsonPropertyName("Discord Rcon Command Description")] public string Description { get; set; } = "Execute commands from the Discord server";
[JsonPropertyName("Discord Server Option Name")] public string ServerOptionName { get; set; } = "server";
[JsonPropertyName("Discord Server Option Description")] public string ServerOptionDescription { get; set; } = "On which server the command will be executed";
[JsonPropertyName("Discord Command Option Name")] public string CommandOptionName { get; set; } = "command";
[JsonPropertyName("Discord Command Option Description")] public string CommandOptionDescription { get; set; } = "What command will be executed";
[JsonPropertyName("Rcon Reply Embed")] public RconReplyEmbed RconReplyEmbed { get; set; } = new RconReplyEmbed();

}
public class RconReplyEmbed
{
[JsonPropertyName("Content")] public string Content { get; set; } = "";
[JsonPropertyName("Title")] public string Title { get; set; } = "";
[JsonPropertyName("Description")] public string Description { get; set; } = "> Command `{COMMAND}` was executed on the `{SERVER}` server";
[JsonPropertyName("Fields")] public string Fields { get; set; } = "";
[JsonPropertyName("Thumbnail")] public string Thumbnail { get; set; } = "";
[JsonPropertyName("Image")] public string Image { get; set; } = "";
[JsonPropertyName("HEX Color")] public string Color { get; set; } = "#0099cc";
[JsonPropertyName("Footer")] public string Footer { get; set; } = "";
[JsonPropertyName("Footer Timestamp")] public bool FooterTimestamp { get; set; } = false;
}
public class Database
{
[JsonPropertyName("Host")] public string Host { get; set; } = "";
Expand Down Expand Up @@ -53,12 +80,12 @@ public class ReportEmbed
[JsonPropertyName("HEX Color")] public string Color { get; set; } = "#ffff66";
[JsonPropertyName("Footer")] public string Footer { get; set; } = "";
[JsonPropertyName("Footer Timestamp")] public bool FooterTimestamp { get; set; } = false;
[JsonPropertyName("Report Embed")] public ReportButton ReportButton { get; set; } = new ReportButton();
[JsonPropertyName("Button Settings")] public ReportButton ReportButton { get; set; } = new ReportButton();
}

public class ReportButton
{
[JsonPropertyName("Enabled")] public bool Enabled { get; set; } = true;
[JsonPropertyName("Enabled")] public bool Enabled { get; set; } = false;
[JsonPropertyName("Admin Role ID")] public string AdminRoleId { get; set; } = "";
[JsonPropertyName("Button Color")] public int Color { get; set; } = 1;
[JsonPropertyName("Button Text")] public string Text { get; set; } = "Mark as solved";
Expand All @@ -75,7 +102,7 @@ public class UpdatedReportEmbed
[JsonPropertyName("Image")] public string Image { get; set; } = "";
[JsonPropertyName("HEX Color")] public string Color { get; set; } = "#00ff99";
[JsonPropertyName("Footer")] public string Footer { get; set; } = "Solved at";
[JsonPropertyName("Footer Timestamp")] public bool FooterTimestamp { get; set; } = false;
[JsonPropertyName("Footer Timestamp")] public bool FooterTimestamp { get; set; } = true;
}

public class ReplyReportEmbed
Expand Down Expand Up @@ -278,6 +305,13 @@ public class EventNotifications
{
[JsonPropertyName("Player Connect")] public Connect Connect { get; set; } = new Connect();
[JsonPropertyName("Player Disconnect")] public Disconnect Disconnect { get; set; } = new Disconnect();
[JsonPropertyName("Map Changed")] public MapChanged MapChanged { get; set; } = new MapChanged();
}
public class MapChanged
{
[JsonPropertyName("Enabled")] public bool Enabled { get; set; } = false;
[JsonPropertyName("Channel ID")] public string ChannelID { get; set; } = "";
[JsonPropertyName("Map Changed Embed")] public MapChangedEmbed MapChangedEmbed { get; set; } = new MapChangedEmbed();
}
public class Connect
{
Expand Down Expand Up @@ -318,6 +352,19 @@ public class DisconnectdEmbed
[JsonPropertyName("Footer Timestamp")] public bool FooterTimestamp { get; set; } = false;
}

public class MapChangedEmbed
{
[JsonPropertyName("Content")] public string Content { get; set; } = "";
[JsonPropertyName("Title")] public string Title { get; set; } = "";
[JsonPropertyName("Description")] public string Description { get; set; } = "> The `{Server.MapName}` map has just started!";
[JsonPropertyName("Fields")] public string Fields { get; set; } = "";
[JsonPropertyName("Thumbnail")] public string Thumbnail { get; set; } = "";
[JsonPropertyName("Image")] public string Image { get; set; } = "";
[JsonPropertyName("HEX Color")] public string Color { get; set; } = "#993366";
[JsonPropertyName("Footer")] public string Footer { get; set; } = "";
[JsonPropertyName("Footer Timestamp")] public bool FooterTimestamp { get; set; } = false;
}

public class CustomFlagsAndRoles
{
[JsonPropertyName("Enabled")] public bool Enabled { get; set; } = false;
Expand Down
102 changes: 92 additions & 10 deletions src/DiscordUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public partial class DiscordUtilities : BasePlugin, IPluginConfig<DUConfig>
{
public override string ModuleName => "Discord Utilities";
public override string ModuleAuthor => "Nocky (SourceFactory.eu)";
public override string ModuleVersion => "1.0.5";
public override string ModuleVersion => "1.0.6";
private DiscordSocketClient? BotClient;
private CommandService? BotCommands;
private IServiceProvider? BotServices;
Expand Down Expand Up @@ -66,6 +66,7 @@ public override void Load(bool hotReload)
if (Config.ConnectedPlayers.Enabled && IsBotConnected)
_ = ClearConnectedPlayersRole();

PerformMapStart();
AddTimer(5.0f, () =>
{
UpdateServerData();
Expand Down Expand Up @@ -113,7 +114,6 @@ public override void Load(bool hotReload)
});
});
}

private async Task LoadDiscordBOT()
{
try
Expand Down Expand Up @@ -157,16 +157,36 @@ private async Task ReadyAsync()
.WithDescription(Config.Link.DiscordDescription)
.AddOption(Config.Link.DiscordOptionName.ToLower(), ApplicationCommandOptionType.String, Config.Link.DiscordOptionDescription, isRequired: true);

var rconCommand = new SlashCommandBuilder()
.WithName(Config.Rcon.Command.ToLower())
.WithDescription(Config.Rcon.Description);

var serverOption = new SlashCommandOptionBuilder()
.WithName(Config.Rcon.ServerOptionName.ToLower())
.WithDescription(Config.Rcon.ServerOptionDescription)
.WithType(ApplicationCommandOptionType.String)
.WithRequired(true);

serverOption.AddChoice("All", "All");
string[] Servers = Config.Rcon.ServerList.Split(',');
foreach (var server in Servers)
serverOption.AddChoice(server, server);
rconCommand.AddOption(serverOption);
rconCommand.AddOption(Config.Rcon.CommandOptionName.ToLower(), ApplicationCommandOptionType.String, Config.Rcon.CommandOptionDescription, isRequired: true);

try
{
await BotClient.CreateGlobalApplicationCommandAsync(linkCommand.Build());
if (Config.Link.Enabled && IsDbConnected)
await BotClient.CreateGlobalApplicationCommandAsync(linkCommand.Build());
if (Config.Rcon.Enabled)
await BotClient.CreateGlobalApplicationCommandAsync(rconCommand.Build());
}
catch (HttpException ex)
{
SendConsoleMessage($"[Discord Utilities] An error occurred while updating Slash Commands: {ex.Message}", ConsoleColor.Red);
}

if (Config.Link.Enabled && IsDbConnected)
if (Config.Link.Enabled || Config.Rcon.Enabled)
BotClient.SlashCommandExecuted += SlashCommandHandler;
if (Config.DiscordRelay.Enabled && !string.IsNullOrEmpty(Config.DiscordRelay.ChannelID))
BotClient.MessageReceived += MessageReceived;
Expand Down Expand Up @@ -197,14 +217,19 @@ private async Task OnInteractionCreatedAsync(SocketInteraction interaction)
string reportedId = componentData.CustomId.Replace("report-", "");
var user = guild.GetUser(interaction.User.Id);

var permissions = user.GuildPermissions;
if (string.IsNullOrEmpty(Config.Report.ReportEmbed.ReportButton.AdminRoleId))
{
await component.RespondAsync(text: "The Admin role ID is not set!", ephemeral: true);
return;
}
var role = guild.GetRole(ulong.Parse(Config.Report.ReportEmbed.ReportButton.AdminRoleId));
if (role == null)
{
SendConsoleMessage($"[Discord Utilities] Role with id '{Config.Report.ReportEmbed.ReportButton.AdminRoleId}' was not found (Report Admin Role)!", ConsoleColor.Red);
SendConsoleMessage($"[Discord Utilities] Admin Role with ID '{Config.Report.ReportEmbed.ReportButton.AdminRoleId}' was not found (Report Admin Role)!", ConsoleColor.Red);
return;
}

var permissions = user.GuildPermissions;
if (permissions.Administrator || user.Roles.Any(id => id == role))
{
await UpdateReportMessage(user, ulong.Parse(reportedId));
Expand All @@ -214,7 +239,7 @@ private async Task OnInteractionCreatedAsync(SocketInteraction interaction)
}
else
{
await component.RespondAsync(text: "You're not the administrator!", ephemeral: true);
await component.RespondAsync(text: "You're not the Administrator!", ephemeral: true);
}
}
}
Expand All @@ -232,19 +257,76 @@ private Task MessageReceived(SocketMessage message)
return Task.CompletedTask;

var user = guild.GetUser(message.Author.Id);
Server.NextWorldUpdate(() =>
Server.NextFrame(() =>
{
PerformChatRelay(user, message);
});
}

return Task.CompletedTask;
}

private async Task SlashCommandHandler(SocketSlashCommand command)
{
if (command.CommandName == Config.Link.DiscordCommand)
if (command.CommandName == Config.Link.DiscordCommand.ToLower())
await DiscordLink_CMD(command);
if (command.CommandName == Config.Rcon.Command.ToLower())
await DiscordRcon_CMD(command);
}
private async Task DiscordRcon_CMD(SocketSlashCommand command)
{
ulong guildId = command.GuildId!.Value;
var guild = BotClient!.GetGuild(guildId);

if (guild == null)
return;
var user = guild.GetUser(command.User.Id);
if (user == null)
return;
if (string.IsNullOrEmpty(Config.Rcon.AdminRoleId))
{
await command.RespondAsync(text: "The Admin role ID is not set!", ephemeral: true);
return;
}
var role = guild.GetRole(ulong.Parse(Config.Rcon.AdminRoleId));
if (role == null)
{
SendConsoleMessage($"[Discord Utilities] Admin Role with ID '{Config.Rcon.AdminRoleId}' was not found (Rcon Section)!", ConsoleColor.Red);
return;
}
string[] data = new string[2];
foreach (var option in command.Data.Options)
{
if (option.Name == Config.Rcon.ServerOptionName)
{
data[1] = option.Value.ToString()!;
}
else if (option.Name == Config.Rcon.CommandOptionName)
{
data[0] = option.Value.ToString()!;
}
}
if (data[1] != Config.Rcon.Server)
{
if (data[1] != "All")
return;
}

var permissions = user.GuildPermissions;
if (permissions.Administrator || user.Roles.Any(id => id == role))
{
Server.NextWorldUpdate(() =>
{
Server.ExecuteCommand(data[0]);
});

var content = GetContent(ContentTypes.Rcon, data);
var embed = GetEmbed(EmbedTypes.Rcon, data);
await command.RespondAsync(text: string.IsNullOrEmpty(content) ? null : content, embed: IsEmbedValid(embed) ? embed.Build() : null, ephemeral: true);
}
else
{
await command.RespondAsync(text: "You're not the Administrator!", ephemeral: true);
}
}
private async Task DiscordLink_CMD(SocketSlashCommand command)
{
Expand Down
7 changes: 3 additions & 4 deletions src/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,11 @@ public HookResult OnPlayerDeath(EventPlayerDeath @event, GameEventInfo info)
var attacker = @event.Attacker;
var assister = @event.Assister;

bool hasPlayerData = playerData.ContainsKey(player);
if (player != null && player.IsValid && hasPlayerData)
if (player != null && player.IsValid && playerData.ContainsKey(player))
UpdatePlayerData(player, 1);
if (attacker != null && attacker.IsValid && hasPlayerData)
if (attacker != null && attacker.IsValid && playerData.ContainsKey(attacker))
UpdatePlayerData(attacker, 1);
if (assister != null && assister.IsValid && hasPlayerData)
if (assister != null && assister.IsValid && playerData.ContainsKey(assister))
UpdatePlayerData(assister, 1);

return HookResult.Continue;
Expand Down
26 changes: 26 additions & 0 deletions src/Functions/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public enum EmbedTypes
ServerStatus_Player,
Connect,
Disconnect,
MapChanged,
Rcon,
}
public enum ContentTypes
{
Expand All @@ -78,6 +80,8 @@ public enum ContentTypes
ServerStatus_Player,
Connect,
Disconnect,
MapChanged,
Rcon,
}

public string GetContent(ContentTypes type, string[] data)
Expand All @@ -95,6 +99,16 @@ public string GetContent(ContentTypes type, string[] data)
content = ReplaceServerDataVariables(content);
break;

case ContentTypes.MapChanged:
content = ReplaceServerDataVariables(Config.EventNotifications.MapChanged.MapChangedEmbed.Content);
break;

case ContentTypes.Rcon:
content = Config.Rcon.RconReplyEmbed.Content;
content = content.Replace("{COMMAND}", data[0]);
content = content.Replace("{SERVER}", data[1]);
break;

case ContentTypes.Report:
content = ReplacePlayerDataVariables(Config.Report.ReportEmbed.Content, ulong.Parse(data[0]));
content = ReplacePlayerDataVariables(content, ulong.Parse(data[1]), true);
Expand Down Expand Up @@ -177,6 +191,16 @@ public EmbedBuilder GetEmbed(EmbedTypes type, string[] data)
replacedValue = ReplaceServerDataVariables(replacedValue);
break;

case EmbedTypes.MapChanged:
replacedValue = ReplaceServerDataVariables((string)value);
break;

case EmbedTypes.Rcon:
replacedValue = (string)value;
replacedValue = replacedValue.Replace("{COMMAND}", data[0]);
replacedValue = replacedValue.Replace("{SERVER}", data[1]);
break;

case EmbedTypes.ServerStatus_Player:
replacedValue = ReplacePlayerDataVariables((string)value, ulong.Parse(data[0]));
replacedValue = ReplaceServerDataVariables(replacedValue);
Expand Down Expand Up @@ -299,6 +323,8 @@ private object GetEmbedConfig(EmbedTypes type)
EmbedTypes.Admin_Chatlog => Config.Chatlog.AdminChat.AdminChatEmbed,
EmbedTypes.Connect => Config.EventNotifications.Connect.ConnectedEmbed,
EmbedTypes.Disconnect => Config.EventNotifications.Disconnect.DisconnectdEmbed,
EmbedTypes.MapChanged => Config.EventNotifications.MapChanged.MapChangedEmbed,
EmbedTypes.Rcon => Config.Rcon.RconReplyEmbed,
_ => null!,
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Functions/Menus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void OpenReportMenu_Players(CCSPlayerController player)
}
var Menu = new CenterHtmlMenu($"{Localizer["Menu.ReportSelectPlayer"]}");
//foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && p.SteamID.ToString().Length == 17))
foreach (var p in Utilities.GetPlayers().Where(p => p.IsValid && p != player && p.SteamID.ToString().Length == 17 && !AdminManager.PlayerHasPermissions(p, Config.Report.UnreportableFlag)))
foreach (var p in Utilities.GetPlayers().Where(p => p != null && p.IsValid && p != player && playerData.ContainsKey(p) && p.Connected == PlayerConnectedState.PlayerConnected && p.SteamID.ToString().Length == 17 && !AdminManager.PlayerHasPermissions(p, Config.Report.UnreportableFlag)))
Menu.AddMenuOption(p.PlayerName, (player, target) => OnSelectPlayer_ReportMenu(player, p));

MenuManager.OpenCenterHtmlMenu(Instance, player!, Menu);
Expand Down
7 changes: 7 additions & 0 deletions src/Sections/EventNotifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ namespace DiscordUtilities
{
public partial class DiscordUtilities
{
public void PerformMapStart()
{
var embedBuiler = GetEmbed(EmbedTypes.MapChanged, new string[1]);
var content = GetContent(ContentTypes.MapChanged, new string[1]);

_ = SendDiscordMessage(embedBuiler, content, ulong.Parse(Config.EventNotifications.MapChanged.ChannelID), "Map Changed Event");
}
public void PerformConnectEvent(ulong steamid)
{
string[] data = new string[1];
Expand Down

0 comments on commit f1cea0f

Please sign in to comment.