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
65
src/ServerManager.Discord/Services/CommandHandlerService.cs
Normal file
65
src/ServerManager.Discord/Services/CommandHandlerService.cs
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServerManagerTool.Discord.Services
|
||||
{
|
||||
public class CommandHandlerService
|
||||
{
|
||||
private readonly DiscordSocketClient _discord;
|
||||
private readonly CommandService _commands;
|
||||
private readonly IConfigurationRoot _config;
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public CommandHandlerService(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider)
|
||||
{
|
||||
_discord = discord;
|
||||
_commands = commands;
|
||||
_config = config;
|
||||
_provider = provider;
|
||||
|
||||
_discord.MessageReceived += OnMessageReceivedAsync;
|
||||
}
|
||||
|
||||
private async Task OnMessageReceivedAsync(SocketMessage s)
|
||||
{
|
||||
// Ensure the message is from a user/bot
|
||||
var msg = s as SocketUserMessage;
|
||||
if (msg is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Ignore self when checking commands
|
||||
if (msg.Author == _discord.CurrentUser)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//Tell bot to ignore itself.
|
||||
if (msg.Author.IsBot)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Create the command context
|
||||
var context = new SocketCommandContext(_discord, msg);
|
||||
|
||||
// Check if the message has a valid command prefix
|
||||
var argPos = 0;
|
||||
if (msg.HasStringPrefix(_config["DiscordSettings:Prefix"], ref argPos) || msg.HasMentionPrefix(_discord.CurrentUser, ref argPos))
|
||||
{
|
||||
// Execute the command
|
||||
var result = await _commands.ExecuteAsync(context, argPos, _provider);
|
||||
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
// If not successful, reply with the error.
|
||||
await context.Channel.SendMessageAsync(result.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
21
src/ServerManager.Discord/Services/ShutdownService.cs
Normal file
21
src/ServerManager.Discord/Services/ShutdownService.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
using Discord.WebSocket;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServerManagerTool.Discord.Services
|
||||
{
|
||||
public class ShutdownService
|
||||
{
|
||||
private readonly DiscordSocketClient _discord;
|
||||
|
||||
public ShutdownService(DiscordSocketClient discord)
|
||||
{
|
||||
_discord = discord;
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
await _discord.StopAsync();
|
||||
await _discord.LogoutAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
45
src/ServerManager.Discord/Services/StartupService.cs
Normal file
45
src/ServerManager.Discord/Services/StartupService.cs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ServerManagerTool.Discord.Services
|
||||
{
|
||||
public class StartupService
|
||||
{
|
||||
private readonly DiscordSocketClient _discord;
|
||||
private readonly CommandService _commands;
|
||||
private readonly IConfigurationRoot _config;
|
||||
private readonly IServiceProvider _provider;
|
||||
|
||||
public StartupService(DiscordSocketClient discord, CommandService commands, IConfigurationRoot config, IServiceProvider provider)
|
||||
{
|
||||
_discord = discord;
|
||||
_commands = commands;
|
||||
_config = config;
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
// Get the discord token from the config file
|
||||
var discordToken = _config["DiscordSettings:Token"];
|
||||
|
||||
if (string.IsNullOrWhiteSpace(discordToken))
|
||||
{
|
||||
throw new Exception("#DiscordBot_MissingTokenError");
|
||||
}
|
||||
|
||||
// Login to discord
|
||||
await _discord.LoginAsync(TokenType.Bot, discordToken);
|
||||
// Connect to the websocket
|
||||
await _discord.StartAsync();
|
||||
|
||||
// Load commands and modules into the command service
|
||||
await _commands.AddModulesAsync(Assembly.GetExecutingAssembly(), _provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue