0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-22 03:05:16 +00:00

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
This commit is contained in:
TheUbMunster 2022-08-22 16:27:12 -06:00 committed by Sanae
parent 08d1020770
commit d828a704c1

View file

@ -1,4 +1,7 @@
using DSharpPlus; #define SEND_RESP_TO_BAD_REQ
#define LOG_BAD_REQ
using DSharpPlus;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Shared; using Shared;
@ -14,6 +17,8 @@ public class DiscordBot {
private DiscordChannel? LogChannel; private DiscordChannel? LogChannel;
private bool Reconnecting; private bool Reconnecting;
private bool warnedAboutNullLogChannel = false; //print warning message
public DiscordBot() { public DiscordBot() {
Token = Config.Token; Token = Config.Token;
Logger.AddLogHandler(Log); Logger.AddLogHandler(Log);
@ -28,6 +33,7 @@ public class DiscordBot {
} }
private async Task Reconnect() { private async Task Reconnect() {
warnedAboutNullLogChannel = false;
if (DiscordClient != null) // usually null prop works, not here though...` if (DiscordClient != null) // usually null prop works, not here though...`
await DiscordClient.DisconnectAsync(); await DiscordClient.DisconnectAsync();
await Run(); await Run();
@ -90,16 +96,34 @@ public class DiscordBot {
Reconnecting = false; Reconnecting = false;
string mentionPrefix = $"{DiscordClient.CurrentUser.Mention} "; string mentionPrefix = $"{DiscordClient.CurrentUser.Mention} ";
DiscordClient.MessageCreated += async (_, args) => { DiscordClient.MessageCreated += async (_, args) => {
if (args.Author.IsCurrent) return; if (args.Author.IsCurrent) return; //dont respond to commands from ourselves (prevent "sql-injection" esq attacks)
//prevent commands via dm //prevent commands via dm and non-public channels
if (Config.LogChannel == null) { if (Config.LogChannel == null) {
Logger.Warn("The discord bot cannot process commands because the LogChannel in settings isn't set"); if (!warnedAboutNullLogChannel) {
return; 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); else {
if (args.Channel.Id != chId) { ulong chId = ulong.Parse(Config.LogChannel);
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)"); if (args.Channel.Id != chId) {
return; #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 //run command
try { try {