feat: cache Discord DM channels in database
This commit is contained in:
		@@ -17,12 +17,14 @@ public class Api
 | 
			
		||||
    private readonly BotConfig _botConfig;
 | 
			
		||||
    private readonly DispatchService _dispatch;
 | 
			
		||||
    private readonly ModelRepository _repo;
 | 
			
		||||
    private readonly PrivateChannelService _dmCache;
 | 
			
		||||
 | 
			
		||||
    public Api(BotConfig botConfig, ModelRepository repo, DispatchService dispatch)
 | 
			
		||||
    public Api(BotConfig botConfig, ModelRepository repo, DispatchService dispatch, PrivateChannelService dmCache)
 | 
			
		||||
    {
 | 
			
		||||
        _botConfig = botConfig;
 | 
			
		||||
        _repo = repo;
 | 
			
		||||
        _dispatch = dispatch;
 | 
			
		||||
        _dmCache = dmCache;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task GetToken(Context ctx)
 | 
			
		||||
@@ -35,17 +37,17 @@ public class Api
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            // DM the user a security disclaimer, and then the token in a separate message (for easy copying on mobile)
 | 
			
		||||
            var dm = await ctx.Cache.GetOrCreateDmChannel(ctx.Rest, ctx.Author.Id);
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm.Id,
 | 
			
		||||
            var dm = await _dmCache.GetOrCreateDmChannel(ctx.Author.Id);
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm,
 | 
			
		||||
                new MessageRequest
 | 
			
		||||
                {
 | 
			
		||||
                    Content = $"{Emojis.Warn} Please note that this grants access to modify (and delete!) all your system data, so keep it safe and secure."
 | 
			
		||||
                            + $" If it leaks or you need a new one, you can invalidate this one with `pk;token refresh`.\n\nYour token is below:"
 | 
			
		||||
                });
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm.Id, new MessageRequest { Content = token });
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = token });
 | 
			
		||||
 | 
			
		||||
            if (_botConfig.IsBetaBot)
 | 
			
		||||
                await ctx.Rest.CreateMessage(dm.Id, new MessageRequest
 | 
			
		||||
                await ctx.Rest.CreateMessage(dm, new MessageRequest
 | 
			
		||||
                {
 | 
			
		||||
                    Content = $"{Emojis.Note} The beta bot's API base URL is currently <{_botConfig.BetaBotAPIUrl}>."
 | 
			
		||||
                                                                                    + " You need to use this URL instead of the base URL listed on the documentation website."
 | 
			
		||||
@@ -84,8 +86,8 @@ public class Api
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            // DM the user an invalidation disclaimer, and then the token in a separate message (for easy copying on mobile)
 | 
			
		||||
            var dm = await ctx.Cache.GetOrCreateDmChannel(ctx.Rest, ctx.Author.Id);
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm.Id,
 | 
			
		||||
            var dm = await _dmCache.GetOrCreateDmChannel(ctx.Author.Id);
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm,
 | 
			
		||||
                new MessageRequest
 | 
			
		||||
                {
 | 
			
		||||
                    Content = $"{Emojis.Warn} Your previous API token has been invalidated. You will need to change it anywhere it's currently used.\n\nYour token is below:"
 | 
			
		||||
@@ -94,10 +96,10 @@ public class Api
 | 
			
		||||
            // Make the new token after sending the first DM; this ensures if we can't DM, we also don't end up
 | 
			
		||||
            // breaking their existing token as a side effect :)
 | 
			
		||||
            var token = await MakeAndSetNewToken(ctx.System);
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm.Id, new MessageRequest { Content = token });
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = token });
 | 
			
		||||
 | 
			
		||||
            if (_botConfig.IsBetaBot)
 | 
			
		||||
                await ctx.Rest.CreateMessage(dm.Id, new MessageRequest
 | 
			
		||||
                await ctx.Rest.CreateMessage(dm, new MessageRequest
 | 
			
		||||
                {
 | 
			
		||||
                    Content = $"{Emojis.Note} The beta bot's API base URL is currently <{_botConfig.BetaBotAPIUrl}>."
 | 
			
		||||
                                                                                   + " You need to use this URL instead of the base URL listed on the documentation website."
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ public class ImportExport
 | 
			
		||||
{
 | 
			
		||||
    private readonly HttpClient _client;
 | 
			
		||||
    private readonly DataFileService _dataFiles;
 | 
			
		||||
    private readonly PrivateChannelService _dmCache;
 | 
			
		||||
 | 
			
		||||
    private readonly JsonSerializerSettings _settings = new()
 | 
			
		||||
    {
 | 
			
		||||
@@ -24,10 +25,11 @@ public class ImportExport
 | 
			
		||||
        DateParseHandling = DateParseHandling.None
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    public ImportExport(DataFileService dataFiles, HttpClient client)
 | 
			
		||||
    public ImportExport(DataFileService dataFiles, HttpClient client, PrivateChannelService dmCache)
 | 
			
		||||
    {
 | 
			
		||||
        _dataFiles = dataFiles;
 | 
			
		||||
        _client = client;
 | 
			
		||||
        _dmCache = dmCache;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public async Task Import(Context ctx)
 | 
			
		||||
@@ -110,12 +112,12 @@ public class ImportExport
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            var dm = await ctx.Cache.GetOrCreateDmChannel(ctx.Rest, ctx.Author.Id);
 | 
			
		||||
            var dm = await _dmCache.GetOrCreateDmChannel(ctx.Author.Id);
 | 
			
		||||
 | 
			
		||||
            var msg = await ctx.Rest.CreateMessage(dm.Id,
 | 
			
		||||
            var msg = await ctx.Rest.CreateMessage(dm,
 | 
			
		||||
                new MessageRequest { Content = $"{Emojis.Success} Here you go!" },
 | 
			
		||||
                new[] { new MultipartFile("system.json", stream, null) });
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm.Id, new MessageRequest { Content = $"<{msg.Attachments[0].Url}>" });
 | 
			
		||||
            await ctx.Rest.CreateMessage(dm, new MessageRequest { Content = $"<{msg.Attachments[0].Url}>" });
 | 
			
		||||
 | 
			
		||||
            // If the original message wasn't posted in DMs, send a public reminder
 | 
			
		||||
            if (ctx.Channel.Type != Channel.ChannelType.Dm)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user