diff --git a/.gitignore b/.gitignore index 4df4dab..f474457 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ src/bin src/obj src/Properties -src/sodoffmmo.csproj.user \ No newline at end of file +src/sodoffmmo.csproj.user +/src/discord_bot.json \ No newline at end of file diff --git a/src/Management/DiscordManager.cs b/src/Management/DiscordManager.cs new file mode 100644 index 0000000..ba7d943 --- /dev/null +++ b/src/Management/DiscordManager.cs @@ -0,0 +1,90 @@ +using Discord.WebSocket; +using Discord; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace sodoffmmo.Management; + +class DiscordManager { + private static readonly string token_file; + + private static DiscordSocketClient client; + private static BotConfig config; + + static DiscordManager() { + DirectoryInfo dir = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "src")); + while (dir.Parent != null) { + if (dir.Name == "src" && dir.Exists) break; + dir = dir.Parent; + } + if (dir.Parent == null) dir = new DirectoryInfo(Directory.GetCurrentDirectory()); + token_file = Path.Combine(dir.FullName, "discord_bot.json"); + } + + public static void Initialize() { + try { + // This approach is taken because I don't want to accidentally + // push my bot token because it was in appsettings. The + // json handled here has been added to gitignore. + if (File.Exists(token_file)) { + Task.Run(() => RunBot(JsonSerializer.Deserialize(File.ReadAllText(token_file)))); + } else { + Log("Discord Integration not started because the file containing the token was not found.\nPut the token in \""+token_file+"\" and restart the server to use Discord Integration.", ConsoleColor.Yellow); + File.WriteAllText(token_file, JsonSerializer.Serialize(new BotConfig { + Token = "" + }, new JsonSerializerOptions { + WriteIndented = true + })); + } + } catch (Exception e) { + Log(e.ToString(), ConsoleColor.Red); + } + } + + private static async Task RunBot(BotConfig? config) { + if (config == null) { + Log("Bot config didn't deserialize correctly. Discord Integration is not active.", ConsoleColor.Yellow); + return; + } + client = new DiscordSocketClient(new DiscordSocketConfig { + GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.MessageContent + }); + client.MessageReceived += OnMessage; + await client.LoginAsync(TokenType.Bot, config.Token); + DiscordManager.config = config; + Log("Loaded bot config from "+token_file+" and bot is running."); + await client.StartAsync(); + } + private static async Task OnMessage(SocketMessage arg) { + // Check that message is from a user in the correct place. + if (arg is not SocketUserMessage socketMessage || socketMessage.Author.IsBot || socketMessage.Channel.Id != config.Channel) return; + + string[] message = socketMessage.Content.Split(' '); + if (message.Length >= 1) { + if (message[0] == "test") { + await socketMessage.Channel.SendMessageAsync("fungus amongus"); + } + } + } + + class BotConfig { + [JsonPropertyName("// Token")] + private string __TokenComment { get{ + return "This is your bot's token. DO NOT SHARE THIS WITH ANYBODY!!!"; + } set {}} + public string Token { get; set; } + + [JsonPropertyName("// Channel")] + private string __ChannelComment { get{ + return "This is the Channel ID where bot logs things and can run admin commands from."; + } set {}} + public ulong Channel { get; set; } + } + + private static void Log(string msg, ConsoleColor? color=null) { + ConsoleColor currentColor = Console.ForegroundColor; + Console.ForegroundColor = color ?? currentColor; + Console.WriteLine(msg); + Console.ForegroundColor = currentColor; + } +} diff --git a/src/Server.cs b/src/Server.cs index 08b9d40..0eebd1e 100644 --- a/src/Server.cs +++ b/src/Server.cs @@ -31,6 +31,8 @@ public class Server { SpecialRoom.CreateRooms(); + DiscordManager.Initialize(); + await Listen(listener); } diff --git a/src/sodoffmmo.csproj b/src/sodoffmmo.csproj index 04b1f9b..aff0588 100644 --- a/src/sodoffmmo.csproj +++ b/src/sodoffmmo.csproj @@ -10,6 +10,7 @@ +