PluralKit/PluralKit.Bot/Modules.cs

147 lines
6.2 KiB
C#
Raw Normal View History

using App.Metrics;
2020-01-26 00:27:45 +00:00
using Autofac;
2020-12-22 12:15:26 +00:00
using Myriad.Cache;
using Myriad.Gateway;
using Myriad.Rest;
2020-12-22 12:15:26 +00:00
using NodaTime;
using PluralKit.Core;
2020-01-26 00:27:45 +00:00
using Sentry;
2020-12-22 12:15:26 +00:00
using Serilog;
using IClock = NodaTime.IClock;
namespace PluralKit.Bot;
public class BotModule: Module
2020-01-26 00:27:45 +00:00
{
protected override void Load(ContainerBuilder builder)
2020-01-26 00:27:45 +00:00
{
// Clients
builder.Register(c =>
2020-01-26 00:27:45 +00:00
{
var botConfig = c.Resolve<BotConfig>();
return new GatewaySettings
2020-12-22 12:15:26 +00:00
{
Token = botConfig.Token,
MaxShardConcurrency = botConfig.MaxShardConcurrency,
GatewayQueueUrl = botConfig.GatewayQueueUrl,
Intents = GatewayIntent.Guilds |
GatewayIntent.DirectMessages |
GatewayIntent.DirectMessageReactions |
GatewayIntent.GuildEmojis |
GatewayIntent.GuildMessages |
GatewayIntent.GuildWebhooks |
GatewayIntent.GuildMessageReactions
};
}).AsSelf().SingleInstance();
builder.RegisterType<Cluster>().AsSelf().SingleInstance();
builder.Register(c => { return new MemoryDiscordCache(); }).AsSelf().As<IDiscordCache>().SingleInstance();
builder.Register(c =>
{
var client = new DiscordApiClient(
c.Resolve<BotConfig>().Token,
c.Resolve<ILogger>(),
c.Resolve<BotConfig>().DiscordBaseUrl
);
2020-12-22 12:15:26 +00:00
var metrics = c.Resolve<IMetrics>();
client.OnResponseEvent += (_, ev) =>
2020-01-26 00:27:45 +00:00
{
var (endpoint, statusCode, ticks) = ev;
var timer = metrics.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();
// Commands
builder.RegisterType<CommandTree>().AsSelf();
builder.RegisterType<Admin>().AsSelf();
builder.RegisterType<Api>().AsSelf();
builder.RegisterType<Autoproxy>().AsSelf();
builder.RegisterType<Checks>().AsSelf();
builder.RegisterType<Fun>().AsSelf();
builder.RegisterType<Groups>().AsSelf();
builder.RegisterType<Help>().AsSelf();
builder.RegisterType<ImportExport>().AsSelf();
builder.RegisterType<Member>().AsSelf();
builder.RegisterType<MemberAvatar>().AsSelf();
builder.RegisterType<MemberEdit>().AsSelf();
builder.RegisterType<MemberGroup>().AsSelf();
builder.RegisterType<MemberProxy>().AsSelf();
builder.RegisterType<Misc>().AsSelf();
builder.RegisterType<ProxiedMessage>().AsSelf();
builder.RegisterType<Random>().AsSelf();
builder.RegisterType<ServerConfig>().AsSelf();
builder.RegisterType<Switch>().AsSelf();
builder.RegisterType<System>().AsSelf();
builder.RegisterType<SystemEdit>().AsSelf();
builder.RegisterType<SystemFront>().AsSelf();
builder.RegisterType<SystemLink>().AsSelf();
builder.RegisterType<SystemList>().AsSelf();
// Bot core
builder.RegisterType<Bot>().AsSelf().SingleInstance();
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>>();
builder.RegisterType<InteractionCreated>().As<IEventHandler<InteractionCreateEvent>>();
// Event handler queue
builder.RegisterType<HandlerQueue<MessageCreateEvent>>().AsSelf().SingleInstance();
builder.RegisterType<HandlerQueue<MessageReactionAddEvent>>().AsSelf().SingleInstance();
// 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();
builder.RegisterType<LastMessageCacheService>().AsSelf().SingleInstance();
builder.RegisterType<LoggerCleanService>().AsSelf().SingleInstance();
builder.RegisterType<ErrorMessageService>().AsSelf().SingleInstance();
builder.RegisterType<CommandMessageService>().AsSelf().SingleInstance();
builder.RegisterType<InteractionDispatchService>().AsSelf().SingleInstance();
// Sentry stuff
builder.Register(_ => new Scope(null)).AsSelf().InstancePerLifetimeScope();
builder.RegisterType<SentryEnricher>()
.As<ISentryEnricher<MessageCreateEvent>>()
.As<ISentryEnricher<MessageDeleteEvent>>()
.As<ISentryEnricher<MessageUpdateEvent>>()
.As<ISentryEnricher<MessageDeleteBulkEvent>>()
.As<ISentryEnricher<MessageReactionAddEvent>>()
.SingleInstance();
// Proxy stuff
builder.RegisterType<ProxyMatcher>().AsSelf().SingleInstance();
builder.RegisterType<ProxyTagParser>().AsSelf().SingleInstance();
// Utils
builder.Register(c => new HttpClient
{
Timeout = TimeSpan.FromSeconds(5),
DefaultRequestHeaders = { { "User-Agent", DiscordApiClient.UserAgent } }
}).AsSelf().SingleInstance();
builder.RegisterInstance(SystemClock.Instance).As<IClock>();
builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance();
2020-01-26 00:27:45 +00:00
}
}