-
Notifications
You must be signed in to change notification settings - Fork 0
/
SasnoBot.cs
90 lines (75 loc) · 2.94 KB
/
SasnoBot.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SasnoBot.Database;
using SasnoBot.Services;
using SasnoBot.Services.Interfaces;
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace SasnoBot
{
internal class SasnoBot : IDisposable
{
private DiscordSocketClient _client;
private IConfigurationRoot _configuration;
private CommandService _commands;
private IServiceProvider _services;
private string[] _serviceArgs;
public SasnoBot(string[] args = null)
{
_serviceArgs = args;
}
public async Task Configure()
{
_client = new DiscordSocketClient();
var configBuilder = new ConfigurationBuilder()
.AddCommandLine(_serviceArgs);
_configuration = configBuilder.Build();
_commands = new CommandService();
RoomLifetimeService roomLifetimeService = new RoomLifetimeService(_client);
roomLifetimeService.Initialize();
_services = new ServiceCollection()
.AddDbContext<SasnoBotDbContext>(ServiceLifetime.Scoped)
.AddSingleton(_client)
.AddSingleton(_commands)
.AddSingleton(roomLifetimeService)
.AddScoped<IUserManager, UserManager>()
.BuildServiceProvider();
await InstallCommandsAsync();
await _client.LoginAsync(TokenType.Bot, _configuration["token"]);
await _client.StartAsync();
await Task.Delay(-1);
}
private async Task InstallCommandsAsync()
{
_client.MessageReceived += HandleCommandAsync;
await _commands.AddModulesAsync(Assembly.GetEntryAssembly(), _services);
}
private async Task HandleCommandAsync(SocketMessage messageParam)
{
var message = messageParam as SocketUserMessage;
if (message == null) return;
int argPos = 0;
if (!(message.HasCharPrefix('!', ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))) return;
var context = new SocketCommandContext(_client, message);
try
{
var result = await _commands.ExecuteAsync(context, argPos, _services, MultiMatchHandling.Exception);
if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
await context.Channel.SendMessageAsync(result.ErrorReason);
}
catch (Exception e)
{
await context.Channel.SendMessageAsync("Exception!");
await context.Channel.SendMessageAsync($"`{e.GetType().FullName} - {e.Message}`");
}
}
public void Dispose()
{
_client.LogoutAsync().GetAwaiter().GetResult();
}
}
}