Sean/Sean/Program.cs

83 lines
2.7 KiB
C#

using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.DependencyInjection;
using Sean.Services;
using Sentry;
using System;
using System.Threading.Tasks;
namespace Sean
{
internal class Program
{
#region Fields
private DiscordSocketClient client;
private CommandHandler commandHandler;
private CommandService commandService;
private CommandServiceConfig commandServiceConfig;
#endregion Fields
#region Methods
//.AddSingleton<DatabaseService>()
public IServiceProvider BuildProvider() => new ServiceCollection().BuildServiceProvider();
public IServiceProvider BuildServiceProvider() => new ServiceCollection()
.AddSingleton(client)
.AddSingleton(commandService)
.AddSingleton<CommandHandler>()
.AddSingleton<DbService>()
.BuildServiceProvider();
public async Task MainAsync()
{
commandServiceConfig = new CommandServiceConfig
{
CaseSensitiveCommands = false,
LogLevel = LogSeverity.Debug
};
commandService = new CommandService(commandServiceConfig);
client = new DiscordSocketClient(new DiscordSocketConfig { MessageCacheSize = 100, AlwaysDownloadUsers = true/*, LogLevel = LogSeverity.Debug */});
client.Log += Log;
IServiceProvider provider = BuildServiceProvider();
commandHandler = new CommandHandler(provider, client, commandService);
await commandHandler.InstallCommandsAsync();
await client.LoginAsync(TokenType.Bot, Environment.GetEnvironmentVariable("DiscordToken_Sean"));
await client.StartAsync();
//client.Disconnected += Client_Disconnected;
// Block this task until the program is closed.
await Task.Delay(-1);
}
//private Task Client_Disconnected(Exception arg)
//{
// Environment.Exit(0);
// return Task.CompletedTask;
//}
private static void Main(string[] args)
{
using (SentrySdk.Init(Environment.GetEnvironmentVariable("SentryURL_Sean")))
{
try
{
new Program().MainAsync().GetAwaiter().GetResult();
}
catch (Exception e)
{
SentrySdk.CaptureException(e);
}
}
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
#endregion Methods
}
}