From d828a704c12841dbc1872bef0a552949c8a171e8 Mon Sep 17 00:00:00 2001 From: TheUbMunster <66451362+TheUbMunster@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:27:12 -0600 Subject: [PATCH] Updated with Sanae's no private channel logic. Behavior: No DM'ing commands under any circumstance if Config.LogChannel == null, commands can be in any non-private channel if Config.LogChannel != null, commands can only be in the log channel --- Server/DiscordBot.cs | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/Server/DiscordBot.cs b/Server/DiscordBot.cs index f78fd1f..facd35f 100644 --- a/Server/DiscordBot.cs +++ b/Server/DiscordBot.cs @@ -1,4 +1,7 @@ -using DSharpPlus; +#define SEND_RESP_TO_BAD_REQ +#define LOG_BAD_REQ + +using DSharpPlus; using DSharpPlus.Entities; using Microsoft.Extensions.Logging; using Shared; @@ -14,6 +17,8 @@ public class DiscordBot { private DiscordChannel? LogChannel; private bool Reconnecting; + private bool warnedAboutNullLogChannel = false; //print warning message + public DiscordBot() { Token = Config.Token; Logger.AddLogHandler(Log); @@ -28,6 +33,7 @@ public class DiscordBot { } private async Task Reconnect() { + warnedAboutNullLogChannel = false; if (DiscordClient != null) // usually null prop works, not here though...` await DiscordClient.DisconnectAsync(); await Run(); @@ -90,16 +96,34 @@ public class DiscordBot { Reconnecting = false; string mentionPrefix = $"{DiscordClient.CurrentUser.Mention} "; DiscordClient.MessageCreated += async (_, args) => { - if (args.Author.IsCurrent) return; - //prevent commands via dm + if (args.Author.IsCurrent) return; //dont respond to commands from ourselves (prevent "sql-injection" esq attacks) + //prevent commands via dm and non-public channels if (Config.LogChannel == null) { - Logger.Warn("The discord bot cannot process commands because the LogChannel in settings isn't set"); - return; + if (!warnedAboutNullLogChannel) { + Logger.Warn("You probably should set your LogChannel in settings.json"); + warnedAboutNullLogChannel = true; + } + if (args.Channel.IsPrivate) { +#if LOG_BAD_REQ + Logger.Warn("A command was sent to the bot in a non-public channel. This will not be processed. (Send commands in the specified LogChannel in settings.json or only in public channels)"); +#endif +#if SEND_RESP_TO_BAD_REQ + await args.Message.RespondAsync("This channel is not valid for running commands. (Your command was not processed)."); +#endif + return; + } } - ulong chId = ulong.Parse(Config.LogChannel); - if (args.Channel.Id != chId) { - Logger.Warn("A command was sent to the bot in a channel other than the specified log channel (probably attempt to run command via dm)"); - return; + else { + ulong chId = ulong.Parse(Config.LogChannel); + if (args.Channel.Id != chId) { +#if LOG_BAD_REQ + Logger.Warn("A command was sent to the bot in a non-public channel. This will not be processed. (Send commands in the specified LogChannel in settings.json or only in public channels)"); +#endif +#if SEND_RESP_TO_BAD_REQ + await args.Message.RespondAsync("This channel is not valid for running commands. (Your command was not processed)."); +#endif + return; + } } //run command try {