2
0
mirror of synced 2025-01-13 18:14:14 +00:00

119 lines
3.6 KiB
C#
Raw Permalink Normal View History

2023-10-18 11:01:24 +02:00
using ArgsUniform;
2023-10-22 10:38:46 +02:00
using BiblioTech.Commands;
2024-02-19 14:56:49 +01:00
using BiblioTech.Rewards;
2023-10-18 11:01:24 +02:00
using Discord;
2023-10-18 09:10:04 +02:00
using Discord.WebSocket;
2024-04-07 14:04:31 +02:00
using DiscordRewards;
2023-12-18 11:27:28 +01:00
using Logging;
2023-10-18 09:10:04 +02:00
2023-10-18 11:01:24 +02:00
namespace BiblioTech
2023-10-18 08:57:59 +02:00
{
2023-10-18 11:01:24 +02:00
public class Program
{
private DiscordSocketClient client = null!;
2024-07-02 10:37:32 +02:00
private CustomReplacement replacement = null!;
2023-10-18 09:10:04 +02:00
2023-10-18 11:01:24 +02:00
public static Configuration Config { get; private set; } = null!;
2023-10-22 09:32:03 +02:00
public static UserRepo UserRepo { get; } = new UserRepo();
2023-12-18 11:27:28 +01:00
public static AdminChecker AdminChecker { get; private set; } = null!;
2024-02-19 14:56:49 +01:00
public static IDiscordRoleDriver RoleDriver { get; set; } = null!;
2023-12-18 11:27:28 +01:00
public static ILog Log { get; private set; } = null!;
2023-10-18 09:10:04 +02:00
2023-10-18 11:01:24 +02:00
public static Task Main(string[] args)
{
Log = new ConsoleLog();
2023-10-18 11:01:24 +02:00
var uniformArgs = new ArgsUniform<Configuration>(PrintHelp, args);
2023-10-18 11:21:06 +02:00
Config = uniformArgs.Parse();
2023-10-18 09:10:04 +02:00
2023-12-18 11:27:28 +01:00
Log = new LogSplitter(
2024-01-22 10:27:07 +01:00
new FileLog(Path.Combine(Config.LogPath, "discordbot")),
2023-12-18 11:27:28 +01:00
new ConsoleLog()
);
2023-10-25 11:53:33 +02:00
EnsurePath(Config.DataPath);
EnsurePath(Config.UserDataPath);
EnsurePath(Config.EndpointsPath);
2023-10-22 11:10:45 +02:00
2024-02-19 14:56:49 +01:00
return new Program().MainAsync(args);
2023-10-18 11:01:24 +02:00
}
2023-10-18 08:57:59 +02:00
2024-02-19 14:56:49 +01:00
public async Task MainAsync(string[] args)
2023-10-18 11:01:24 +02:00
{
2023-12-18 11:27:28 +01:00
Log.Log("Starting Codex Discord Bot...");
2024-07-02 10:37:32 +02:00
try
{
replacement = new CustomReplacement(Config);
replacement.Load();
}
catch (Exception ex)
{
Log.Error("Failed to load logReplacements: " + ex);
throw;
}
if (Config.DebugNoDiscord)
{
Log.Log("Debug option is set. Discord connection disabled!");
RoleDriver = new LoggingRoleDriver(Log);
}
else
{
await StartDiscordBot();
}
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel((context, options) =>
{
options.ListenAnyIP(Config.RewardApiPort);
});
builder.Services.AddControllers();
var app = builder.Build();
app.MapControllers();
Log.Log("Running...");
await app.RunAsync();
await Task.Delay(-1);
}
private async Task StartDiscordBot()
{
2023-10-18 11:01:24 +02:00
client = new DiscordSocketClient();
2023-12-18 11:27:28 +01:00
client.Log += ClientLog;
2023-10-18 11:01:24 +02:00
2024-01-22 10:27:07 +01:00
var notifyCommand = new NotifyCommand();
var associateCommand = new UserAssociateCommand(notifyCommand);
var sprCommand = new SprCommand();
var handler = new CommandHandler(Log, client, replacement,
new GetBalanceCommand(associateCommand),
new MintCommand(associateCommand),
sprCommand,
2023-10-22 10:38:46 +02:00
associateCommand,
2024-01-22 10:27:07 +01:00
notifyCommand,
new AdminCommand(sprCommand, replacement)
);
2023-10-18 11:01:24 +02:00
2023-10-18 13:48:15 +02:00
await client.LoginAsync(TokenType.Bot, Config.ApplicationToken);
2023-10-18 11:01:24 +02:00
await client.StartAsync();
2023-12-18 11:27:28 +01:00
AdminChecker = new AdminChecker();
2023-10-18 11:01:24 +02:00
}
private static void PrintHelp()
{
2023-12-18 11:27:28 +01:00
Log.Log("BiblioTech - Codex Discord Bot");
2023-10-18 11:01:24 +02:00
}
2023-12-18 11:27:28 +01:00
private Task ClientLog(LogMessage msg)
2023-10-18 11:01:24 +02:00
{
2023-12-18 11:27:28 +01:00
Log.Log("DiscordClient: " + msg.ToString());
2023-10-18 11:01:24 +02:00
return Task.CompletedTask;
}
2023-10-25 11:53:33 +02:00
private static void EnsurePath(string path)
{
if (Directory.Exists(path)) return;
Directory.CreateDirectory(path);
}
2023-10-18 08:57:59 +02:00
}
2023-10-18 11:01:24 +02:00
}