2020-01-26 00:27:45 +00:00
|
|
|
using System;
|
|
|
|
using System.Net.Http;
|
|
|
|
|
|
|
|
using Autofac;
|
|
|
|
|
2021-11-02 09:34:17 +00:00
|
|
|
using App.Metrics;
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
using Myriad.Cache;
|
|
|
|
using Myriad.Gateway;
|
2021-08-23 20:53:58 +00:00
|
|
|
using Myriad.Rest;
|
2020-12-22 12:15:26 +00:00
|
|
|
|
2020-06-06 23:30:19 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
2020-02-12 14:16:19 +00:00
|
|
|
using PluralKit.Core;
|
2020-01-26 00:27:45 +00:00
|
|
|
|
|
|
|
using Sentry;
|
|
|
|
|
2020-12-22 12:15:26 +00:00
|
|
|
using Serilog;
|
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
namespace PluralKit.Bot
|
|
|
|
{
|
|
|
|
public class BotModule: Module
|
|
|
|
{
|
|
|
|
protected override void Load(ContainerBuilder builder)
|
|
|
|
{
|
2020-04-24 19:50:28 +00:00
|
|
|
// Clients
|
2021-03-18 08:47:58 +00:00
|
|
|
builder.Register(c =>
|
2020-12-22 12:15:26 +00:00
|
|
|
{
|
2021-03-18 08:47:58 +00:00
|
|
|
var botConfig = c.Resolve<BotConfig>();
|
|
|
|
return new GatewaySettings
|
|
|
|
{
|
|
|
|
Token = botConfig.Token,
|
|
|
|
MaxShardConcurrency = botConfig.MaxShardConcurrency,
|
2021-06-09 14:22:10 +00:00
|
|
|
GatewayQueueUrl = botConfig.GatewayQueueUrl,
|
2021-03-18 08:47:58 +00:00
|
|
|
Intents = GatewayIntent.Guilds |
|
|
|
|
GatewayIntent.DirectMessages |
|
|
|
|
GatewayIntent.DirectMessageReactions |
|
|
|
|
GatewayIntent.GuildEmojis |
|
|
|
|
GatewayIntent.GuildMessages |
|
|
|
|
GatewayIntent.GuildWebhooks |
|
|
|
|
GatewayIntent.GuildMessageReactions
|
|
|
|
};
|
2020-12-22 12:15:26 +00:00
|
|
|
}).AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<Cluster>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<MemoryDiscordCache>().AsSelf().As<IDiscordCache>().SingleInstance();
|
|
|
|
|
2021-11-02 09:34:17 +00:00
|
|
|
builder.Register(c =>
|
|
|
|
{
|
|
|
|
var client = new Myriad.Rest.DiscordApiClient(
|
|
|
|
c.Resolve<BotConfig>().Token,
|
2021-11-02 09:36:53 +00:00
|
|
|
c.Resolve<ILogger>(),
|
|
|
|
c.Resolve<BotConfig>().DiscordBaseUrl
|
2021-11-02 09:34:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
client.OnResponseEvent += ((_, ev) =>
|
|
|
|
{
|
|
|
|
var (endpoint, statusCode, ticks) = ev;
|
|
|
|
var timer = c.Resolve<IMetrics>().Provider.Timer.Instance(BotMetrics.DiscordApiRequests, new MetricTags(
|
|
|
|
new[] { "endpoint", "status_code" },
|
|
|
|
new[] { endpoint, statusCode.ToString() }
|
|
|
|
));
|
|
|
|
timer.Record(ticks / 10, TimeUnit.Microseconds);
|
|
|
|
});
|
|
|
|
|
|
|
|
return client;
|
|
|
|
}).AsSelf().SingleInstance();
|
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
// Commands
|
|
|
|
builder.RegisterType<CommandTree>().AsSelf();
|
2021-06-08 17:37:44 +00:00
|
|
|
builder.RegisterType<Admin>().AsSelf();
|
2020-02-01 12:03:02 +00:00
|
|
|
builder.RegisterType<Autoproxy>().AsSelf();
|
2021-08-25 18:36:13 +00:00
|
|
|
builder.RegisterType<Checks>().AsSelf();
|
2020-02-01 12:03:02 +00:00
|
|
|
builder.RegisterType<Fun>().AsSelf();
|
2020-06-29 21:51:12 +00:00
|
|
|
builder.RegisterType<Groups>().AsSelf();
|
2020-02-01 12:03:02 +00:00
|
|
|
builder.RegisterType<Help>().AsSelf();
|
|
|
|
builder.RegisterType<ImportExport>().AsSelf();
|
|
|
|
builder.RegisterType<Member>().AsSelf();
|
|
|
|
builder.RegisterType<MemberAvatar>().AsSelf();
|
|
|
|
builder.RegisterType<MemberEdit>().AsSelf();
|
2020-11-23 00:57:01 +00:00
|
|
|
builder.RegisterType<MemberGroup>().AsSelf();
|
2020-02-01 12:03:02 +00:00
|
|
|
builder.RegisterType<MemberProxy>().AsSelf();
|
|
|
|
builder.RegisterType<Misc>().AsSelf();
|
2021-08-25 18:36:13 +00:00
|
|
|
builder.RegisterType<ProxiedMessage>().AsSelf();
|
2020-11-22 16:58:23 +00:00
|
|
|
builder.RegisterType<Random>().AsSelf();
|
2020-02-01 12:03:02 +00:00
|
|
|
builder.RegisterType<ServerConfig>().AsSelf();
|
|
|
|
builder.RegisterType<Switch>().AsSelf();
|
2020-02-12 14:16:19 +00:00
|
|
|
builder.RegisterType<System>().AsSelf();
|
2020-02-01 12:03:02 +00:00
|
|
|
builder.RegisterType<SystemEdit>().AsSelf();
|
|
|
|
builder.RegisterType<SystemFront>().AsSelf();
|
|
|
|
builder.RegisterType<SystemLink>().AsSelf();
|
|
|
|
builder.RegisterType<SystemList>().AsSelf();
|
|
|
|
builder.RegisterType<Token>().AsSelf();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
// Bot core
|
|
|
|
builder.RegisterType<Bot>().AsSelf().SingleInstance();
|
2020-12-22 12:15:26 +00:00
|
|
|
builder.RegisterType<MessageCreated>().As<IEventHandler<MessageCreateEvent>>();
|
|
|
|
builder.RegisterType<MessageDeleted>().As<IEventHandler<MessageDeleteEvent>>().As<IEventHandler<MessageDeleteBulkEvent>>();
|
|
|
|
builder.RegisterType<MessageEdited>().As<IEventHandler<MessageUpdateEvent>>();
|
|
|
|
builder.RegisterType<ReactionAdded>().As<IEventHandler<MessageReactionAddEvent>>();
|
2021-05-26 20:27:52 +00:00
|
|
|
builder.RegisterType<InteractionCreated>().As<IEventHandler<InteractionCreateEvent>>();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-05-05 14:03:46 +00:00
|
|
|
// Event handler queue
|
2020-12-24 13:52:44 +00:00
|
|
|
builder.RegisterType<HandlerQueue<MessageCreateEvent>>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<HandlerQueue<MessageReactionAddEvent>>().AsSelf().SingleInstance();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
// Bot services
|
|
|
|
builder.RegisterType<EmbedService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<ProxyService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<LogChannelService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<DataFileService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<WebhookExecutorService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<WebhookCacheService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<ShardInfoService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<CpuStatService>().AsSelf().SingleInstance();
|
|
|
|
builder.RegisterType<PeriodicStatCollector>().AsSelf().SingleInstance();
|
2020-02-12 13:21:48 +00:00
|
|
|
builder.RegisterType<LastMessageCacheService>().AsSelf().SingleInstance();
|
2020-02-14 23:12:03 +00:00
|
|
|
builder.RegisterType<LoggerCleanService>().AsSelf().SingleInstance();
|
2020-09-20 20:35:05 +00:00
|
|
|
builder.RegisterType<ErrorMessageService>().AsSelf().SingleInstance();
|
2020-10-23 10:18:28 +00:00
|
|
|
builder.RegisterType<CommandMessageService>().AsSelf().SingleInstance();
|
2021-05-26 20:27:52 +00:00
|
|
|
builder.RegisterType<InteractionDispatchService>().AsSelf().SingleInstance();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
// Sentry stuff
|
|
|
|
builder.Register(_ => new Scope(null)).AsSelf().InstancePerLifetimeScope();
|
2021-01-31 14:03:11 +00:00
|
|
|
builder.RegisterType<SentryEnricher>()
|
|
|
|
.As<ISentryEnricher<MessageCreateEvent>>()
|
|
|
|
.As<ISentryEnricher<MessageDeleteEvent>>()
|
|
|
|
.As<ISentryEnricher<MessageUpdateEvent>>()
|
|
|
|
.As<ISentryEnricher<MessageDeleteBulkEvent>>()
|
|
|
|
.As<ISentryEnricher<MessageReactionAddEvent>>()
|
|
|
|
.SingleInstance();
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2020-06-11 21:20:46 +00:00
|
|
|
// Proxy stuff
|
2020-06-12 18:29:50 +00:00
|
|
|
builder.RegisterType<ProxyMatcher>().AsSelf().SingleInstance();
|
2020-06-11 21:20:46 +00:00
|
|
|
builder.RegisterType<ProxyTagParser>().AsSelf().SingleInstance();
|
2020-04-28 22:05:26 +00:00
|
|
|
|
2020-01-26 00:27:45 +00:00
|
|
|
// Utils
|
|
|
|
builder.Register(c => new HttpClient
|
|
|
|
{
|
2021-08-23 20:53:58 +00:00
|
|
|
Timeout = TimeSpan.FromSeconds(5),
|
2021-08-27 15:03:47 +00:00
|
|
|
DefaultRequestHeaders = { { "User-Agent", DiscordApiClient.UserAgent } }
|
2020-01-26 00:27:45 +00:00
|
|
|
}).AsSelf().SingleInstance();
|
2021-11-02 09:34:17 +00:00
|
|
|
builder.RegisterInstance(SystemClock.Instance).As<NodaTime.IClock>();
|
2021-06-10 12:21:05 +00:00
|
|
|
builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance();
|
2020-01-26 00:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|