0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-25 12:45:18 +00:00

Made changes for pr, cleaned up formatting

This commit is contained in:
TheUbMunster 2022-08-22 18:22:45 -06:00 committed by Sanae
parent 67a740dec9
commit 4d743b3e1b

View file

@ -1,6 +1,4 @@
//#define SEND_RESP_TO_BAD_REQ_DM // should the bot send a message to people who attempt to run commands/speak in dms using DSharpPlus;
using DSharpPlus;
using DSharpPlus.Entities; using DSharpPlus.Entities;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Shared; using Shared;
@ -13,12 +11,10 @@ public class DiscordBot {
private Settings.DiscordTable Config => Settings.Instance.Discord; private Settings.DiscordTable Config => Settings.Instance.Discord;
private string Prefix => Config.Prefix; private string Prefix => Config.Prefix;
private readonly Logger Logger = new Logger("Discord"); private readonly Logger Logger = new Logger("Discord");
//private DiscordChannel? CommandChannel; private DiscordChannel? CommandChannel;
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,12 +24,13 @@ public class DiscordBot {
Task.Run(Reconnect); Task.Run(Reconnect);
return "Restarting Discord bot"; return "Restarting Discord bot";
}); });
if (Config.CommandChannel == null)
Logger.Warn("You probably should set your CommandChannel in settings.json");
if (Config.Token == null) return; if (Config.Token == null) return;
Settings.LoadHandler += SettingsLoadHandler; Settings.LoadHandler += SettingsLoadHandler;
} }
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();
@ -44,9 +41,9 @@ public class DiscordBot {
if (DiscordClient == null || Token != Config.Token) if (DiscordClient == null || Token != Config.Token)
await Run(); await Run();
//CommandChannel not currently used //CommandChannel not currently used
//if (Config.CommandChannel != null) if (Config.CommandChannel != null)
// CommandChannel = await (DiscordClient?.GetChannelAsync(ulong.Parse(Config.CommandChannel)) ?? CommandChannel = await (DiscordClient?.GetChannelAsync(ulong.Parse(Config.CommandChannel)) ??
// throw new NullReferenceException("Discord client not setup yet!")); throw new NullReferenceException("Discord client not setup yet!"));
if (Config.LogChannel != null) if (Config.LogChannel != null)
LogChannel = await (DiscordClient?.GetChannelAsync(ulong.Parse(Config.LogChannel)) ?? LogChannel = await (DiscordClient?.GetChannelAsync(ulong.Parse(Config.LogChannel)) ??
throw new NullReferenceException("Discord client not setup yet!")); throw new NullReferenceException("Discord client not setup yet!"));
@ -102,23 +99,12 @@ public class DiscordBot {
DiscordClient.MessageCreated += async (_, args) => { DiscordClient.MessageCreated += async (_, args) => {
if (args.Author.IsCurrent) return; //dont respond to commands from ourselves (prevent "sql-injection" esq attacks) if (args.Author.IsCurrent) return; //dont respond to commands from ourselves (prevent "sql-injection" esq attacks)
//prevent commands via dm and non-public channels //prevent commands via dm and non-public channels
if (Config.CommandChannel == null) { if (CommandChannel == null) {
if (!warnedAboutNullLogChannel) { if (args.Channel is DiscordDmChannel)
Logger.Warn("You probably should set your CommandChannel in settings.json"); return; //no dm'ing the bot allowed!
warnedAboutNullLogChannel = true;
} }
if (args.Channel is DiscordDmChannel) { else if (args.Channel.Id != CommandChannel.Id)
#if SEND_RESP_TO_BAD_REQ_DM
await args.Message.RespondAsync("DM's are not valid for running commands or communication to the bot. (Your command was not processed).");
#endif
return; return;
}
}
else {
ulong chId = ulong.Parse(Config.CommandChannel);
if (args.Channel.Id != chId)
return;
}
//run command //run command
try { try {
DiscordMessage msg = args.Message; DiscordMessage msg = args.Message;