mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-18 09:35:48 +00:00
Discord Bot Scaffolding
This commit is contained in:
parent
ceb3ab73c4
commit
c4bf4906ea
24 changed files with 1119 additions and 5 deletions
57
src/ServerManager.Discord/Services/LoggingService.cs
Normal file
57
src/ServerManager.Discord/Services/LoggingService.cs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServerManagerTool.Discord.Services
|
||||
{
|
||||
public class LoggingService
|
||||
{
|
||||
private readonly DiscordSocketClient _discord;
|
||||
private readonly CommandService _commands;
|
||||
private readonly IConfigurationRoot _config;
|
||||
|
||||
private string LogDirectory { get; }
|
||||
private string LogFile => Path.Combine(LogDirectory, $"ServerManager_DiscordBot.{DateTime.Now:yyyyMMdd}.log");
|
||||
|
||||
public LoggingService(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config)
|
||||
{
|
||||
_discord = discord;
|
||||
_commands = commands;
|
||||
_config = config;
|
||||
|
||||
// Get the data directory from the config file
|
||||
var rootDirectory = _config["ServerManager:DataDirectory"] ?? AppContext.BaseDirectory;
|
||||
LogDirectory = Path.Combine(rootDirectory, "logs");
|
||||
|
||||
_discord.Log += OnLogAsync;
|
||||
_commands.Log += OnLogAsync;
|
||||
}
|
||||
|
||||
private async Task OnLogAsync(LogMessage message)
|
||||
{
|
||||
// Create the log directory if it doesn't exist
|
||||
if (!Directory.Exists(LogDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(LogDirectory);
|
||||
}
|
||||
|
||||
// Create today's log file if it doesn't exist
|
||||
if (!File.Exists(LogFile))
|
||||
{
|
||||
File.Create(LogFile).Dispose();
|
||||
}
|
||||
|
||||
var logText = $"{DateTime.Now:HH:mm:ss:ffff} [{message.Severity}] {message.Source}: {message.Exception?.ToString() ?? message.Message}";
|
||||
|
||||
// Write the log text to a file
|
||||
File.AppendAllText(LogFile, logText + "\n");
|
||||
|
||||
// Write the log text to the console
|
||||
await Console.Out.WriteLineAsync(logText);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue