Use a proper user agent when fetching images
This commit is contained in:
parent
55c56c4e58
commit
41427db178
@ -12,7 +12,7 @@ namespace Myriad.Rest
|
|||||||
{
|
{
|
||||||
public class DiscordApiClient
|
public class DiscordApiClient
|
||||||
{
|
{
|
||||||
private const string UserAgent = "DiscordBot (https://github.com/xSke/PluralKit/tree/main/Myriad/, vMyriad)";
|
public const string UserAgent = "PluralKit (https://github.com/xSke/PluralKit/tree/main/Myriad/, v1)";
|
||||||
private readonly BaseRestClient _client;
|
private readonly BaseRestClient _client;
|
||||||
|
|
||||||
public DiscordApiClient(string token, ILogger logger)
|
public DiscordApiClient(string token, ILogger logger)
|
||||||
|
@ -180,11 +180,21 @@ namespace PluralKit.Bot
|
|||||||
await using var serviceScope = _services.BeginLifetimeScope();
|
await using var serviceScope = _services.BeginLifetimeScope();
|
||||||
|
|
||||||
// Find an event handler that can handle the type of event (<T>) we're given
|
// Find an event handler that can handle the type of event (<T>) we're given
|
||||||
var handler = serviceScope.Resolve<IEventHandler<T>>();
|
IEventHandler<T> handler;
|
||||||
var queue = serviceScope.ResolveOptional<HandlerQueue<T>>();
|
try
|
||||||
|
{
|
||||||
|
handler = serviceScope.Resolve<IEventHandler<T>>();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.Error(e, "Error instantiating handler class");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var queue = serviceScope.ResolveOptional<HandlerQueue<T>>();
|
||||||
|
|
||||||
using var _ = LogContext.PushProperty("EventId", Guid.NewGuid());
|
using var _ = LogContext.PushProperty("EventId", Guid.NewGuid());
|
||||||
using var __ = LogContext.Push(serviceScope.Resolve<SerilogGatewayEnricherFactory>().GetEnricher(shard, evt));
|
using var __ = LogContext.Push(serviceScope.Resolve<SerilogGatewayEnricherFactory>().GetEnricher(shard, evt));
|
||||||
_logger.Verbose("Received gateway event: {@Event}", evt);
|
_logger.Verbose("Received gateway event: {@Event}", evt);
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -22,12 +23,14 @@ namespace PluralKit.Bot
|
|||||||
private readonly IDatabase _db;
|
private readonly IDatabase _db;
|
||||||
private readonly ModelRepository _repo;
|
private readonly ModelRepository _repo;
|
||||||
private readonly EmbedService _embeds;
|
private readonly EmbedService _embeds;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
public Groups(IDatabase db, ModelRepository repo, EmbedService embeds)
|
public Groups(IDatabase db, ModelRepository repo, EmbedService embeds, HttpClient client)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
_embeds = embeds;
|
_embeds = embeds;
|
||||||
|
_client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateGroup(Context ctx)
|
public async Task CreateGroup(Context ctx)
|
||||||
@ -200,7 +203,7 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
ctx.CheckOwnGroup(target);
|
ctx.CheckOwnGroup(target);
|
||||||
|
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
|
||||||
|
|
||||||
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url}));
|
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {Icon = img.Url}));
|
||||||
|
|
||||||
@ -262,7 +265,7 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
ctx.CheckOwnGroup(target);
|
ctx.CheckOwnGroup(target);
|
||||||
|
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
|
||||||
|
|
||||||
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {BannerImage = img.Url}));
|
await _db.Execute(c => _repo.UpdateGroup(c, target.Id, new GroupPatch {BannerImage = img.Url}));
|
||||||
|
|
||||||
|
@ -22,15 +22,17 @@ namespace PluralKit.Bot
|
|||||||
public class ImportExport
|
public class ImportExport
|
||||||
{
|
{
|
||||||
private readonly DataFileService _dataFiles;
|
private readonly DataFileService _dataFiles;
|
||||||
|
private readonly HttpClient _client;
|
||||||
private readonly JsonSerializerSettings _settings = new()
|
private readonly JsonSerializerSettings _settings = new()
|
||||||
{
|
{
|
||||||
// Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.>
|
// Otherwise it'll mess up/reformat the ISO strings for ???some??? reason >.>
|
||||||
DateParseHandling = DateParseHandling.None
|
DateParseHandling = DateParseHandling.None
|
||||||
};
|
};
|
||||||
|
|
||||||
public ImportExport(DataFileService dataFiles)
|
public ImportExport(DataFileService dataFiles, HttpClient client)
|
||||||
{
|
{
|
||||||
_dataFiles = dataFiles;
|
_dataFiles = dataFiles;
|
||||||
|
_client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Import(Context ctx)
|
public async Task Import(Context ctx)
|
||||||
@ -39,13 +41,11 @@ namespace PluralKit.Bot
|
|||||||
if (url == null) throw Errors.NoImportFilePassed;
|
if (url == null) throw Errors.NoImportFilePassed;
|
||||||
|
|
||||||
await ctx.BusyIndicator(async () =>
|
await ctx.BusyIndicator(async () =>
|
||||||
{
|
|
||||||
using (var client = new HttpClient())
|
|
||||||
{
|
{
|
||||||
HttpResponseMessage response;
|
HttpResponseMessage response;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
response = await client.GetAsync(url);
|
response = await _client.GetAsync(url);
|
||||||
}
|
}
|
||||||
catch (InvalidOperationException)
|
catch (InvalidOperationException)
|
||||||
{
|
{
|
||||||
@ -91,7 +91,6 @@ namespace PluralKit.Bot
|
|||||||
// We already had a system, so show them what changed
|
// We already had a system, so show them what changed
|
||||||
await ctx.Reply($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
|
await ctx.Reply($"{Emojis.Success} Updated {result.ModifiedNames.Count} members, created {result.AddedNames.Count} members. Type `pk;system list` to check!");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ namespace PluralKit.Bot
|
|||||||
if (avatarArg != null)
|
if (avatarArg != null)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Url);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Url);
|
||||||
await _db.Execute(conn => _repo.UpdateMember(conn, member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }));
|
await _db.Execute(conn => _repo.UpdateMember(conn, member.Id, new MemberPatch { AvatarUrl = avatarArg.Url }));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
imageMatchError = e;
|
imageMatchError = e;
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Myriad.Builders;
|
using Myriad.Builders;
|
||||||
@ -12,11 +13,13 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
private readonly IDatabase _db;
|
private readonly IDatabase _db;
|
||||||
private readonly ModelRepository _repo;
|
private readonly ModelRepository _repo;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
public MemberAvatar(IDatabase db, ModelRepository repo)
|
public MemberAvatar(IDatabase db, ModelRepository repo, HttpClient client)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
|
_client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task AvatarClear(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? mgs)
|
private async Task AvatarClear(AvatarLocation location, Context ctx, PKMember target, MemberGuildSettings? mgs)
|
||||||
@ -102,7 +105,7 @@ namespace PluralKit.Bot
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.CheckSystem().CheckOwnMember(target);
|
ctx.CheckSystem().CheckOwnMember(target);
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(avatarArg.Value.Url);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, avatarArg.Value.Url);
|
||||||
await UpdateAvatar(location, ctx, target, avatarArg.Value.Url);
|
await UpdateAvatar(location, ctx, target, avatarArg.Value.Url);
|
||||||
await PrintResponse(location, ctx, target, avatarArg.Value, guildData);
|
await PrintResponse(location, ctx, target, avatarArg.Value, guildData);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
using Myriad.Builders;
|
using Myriad.Builders;
|
||||||
|
|
||||||
@ -14,11 +15,13 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
private readonly IDatabase _db;
|
private readonly IDatabase _db;
|
||||||
private readonly ModelRepository _repo;
|
private readonly ModelRepository _repo;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
public MemberEdit(IDatabase db, ModelRepository repo)
|
public MemberEdit(IDatabase db, ModelRepository repo, HttpClient client)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
|
_client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Name(Context ctx, PKMember target)
|
public async Task Name(Context ctx, PKMember target)
|
||||||
@ -148,7 +151,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
async Task SetBannerImage(ParsedImage img)
|
async Task SetBannerImage(ParsedImage img)
|
||||||
{
|
{
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
|
||||||
|
|
||||||
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch {BannerImage = img.Url}));
|
await _db.Execute(c => _repo.UpdateMember(c, target.Id, new MemberPatch {BannerImage = img.Url}));
|
||||||
|
|
||||||
|
@ -1,10 +1,10 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Net.Http;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using Myriad.Builders;
|
using Myriad.Builders;
|
||||||
using Myriad.Types;
|
|
||||||
|
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
using NodaTime.Text;
|
using NodaTime.Text;
|
||||||
@ -18,11 +18,13 @@ namespace PluralKit.Bot
|
|||||||
{
|
{
|
||||||
private readonly IDatabase _db;
|
private readonly IDatabase _db;
|
||||||
private readonly ModelRepository _repo;
|
private readonly ModelRepository _repo;
|
||||||
|
private readonly HttpClient _client;
|
||||||
|
|
||||||
public SystemEdit(IDatabase db, ModelRepository repo)
|
public SystemEdit(IDatabase db, ModelRepository repo, HttpClient client)
|
||||||
{
|
{
|
||||||
_db = db;
|
_db = db;
|
||||||
_repo = repo;
|
_repo = repo;
|
||||||
|
_client = client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Name(Context ctx)
|
public async Task Name(Context ctx)
|
||||||
@ -279,7 +281,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
async Task SetIcon(ParsedImage img)
|
async Task SetIcon(ParsedImage img)
|
||||||
{
|
{
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url);
|
||||||
|
|
||||||
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url}));
|
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {AvatarUrl = img.Url}));
|
||||||
|
|
||||||
@ -332,7 +334,7 @@ namespace PluralKit.Bot
|
|||||||
|
|
||||||
async Task SetImage(ParsedImage img)
|
async Task SetImage(ParsedImage img)
|
||||||
{
|
{
|
||||||
await AvatarUtils.VerifyAvatarOrThrow(img.Url, isFullSizeImage: true);
|
await AvatarUtils.VerifyAvatarOrThrow(_client, img.Url, isFullSizeImage: true);
|
||||||
|
|
||||||
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {BannerImage = img.Url}));
|
await _db.Execute(c => _repo.UpdateSystem(c, ctx.System.Id, new SystemPatch {BannerImage = img.Url}));
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ using Autofac;
|
|||||||
|
|
||||||
using Myriad.Cache;
|
using Myriad.Cache;
|
||||||
using Myriad.Gateway;
|
using Myriad.Gateway;
|
||||||
|
using Myriad.Rest;
|
||||||
|
|
||||||
using NodaTime;
|
using NodaTime;
|
||||||
|
|
||||||
@ -113,7 +114,8 @@ namespace PluralKit.Bot
|
|||||||
// Utils
|
// Utils
|
||||||
builder.Register(c => new HttpClient
|
builder.Register(c => new HttpClient
|
||||||
{
|
{
|
||||||
Timeout = TimeSpan.FromSeconds(5)
|
Timeout = TimeSpan.FromSeconds(5),
|
||||||
|
DefaultRequestHeaders = {{"User-Agent", DiscordApiClient.UserAgent}}
|
||||||
}).AsSelf().SingleInstance();
|
}).AsSelf().SingleInstance();
|
||||||
builder.RegisterInstance(SystemClock.Instance).As<IClock>();
|
builder.RegisterInstance(SystemClock.Instance).As<IClock>();
|
||||||
builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance();
|
builder.RegisterType<SerilogGatewayEnricherFactory>().AsSelf().SingleInstance();
|
||||||
|
@ -10,7 +10,7 @@ using SixLabors.ImageSharp;
|
|||||||
|
|
||||||
namespace PluralKit.Bot {
|
namespace PluralKit.Bot {
|
||||||
public static class AvatarUtils {
|
public static class AvatarUtils {
|
||||||
public static async Task VerifyAvatarOrThrow(string url, bool isFullSizeImage = false)
|
public static async Task VerifyAvatarOrThrow(HttpClient client, string url, bool isFullSizeImage = false)
|
||||||
{
|
{
|
||||||
if (url.Length > Limits.MaxUriLength)
|
if (url.Length > Limits.MaxUriLength)
|
||||||
throw Errors.UrlTooLong(url);
|
throw Errors.UrlTooLong(url);
|
||||||
@ -24,8 +24,6 @@ namespace PluralKit.Bot {
|
|||||||
// TODO: add image/webp once ImageSharp supports this
|
// TODO: add image/webp once ImageSharp supports this
|
||||||
};
|
};
|
||||||
|
|
||||||
using (var client = new HttpClient())
|
|
||||||
{
|
|
||||||
if (!PluralKit.Core.MiscUtils.TryMatchUri(url, out var uri))
|
if (!PluralKit.Core.MiscUtils.TryMatchUri(url, out var uri))
|
||||||
throw Errors.InvalidUrl(url);
|
throw Errors.InvalidUrl(url);
|
||||||
|
|
||||||
@ -53,7 +51,6 @@ namespace PluralKit.Bot {
|
|||||||
if (image.Width > Limits.AvatarDimensionLimit || image.Height > Limits.AvatarDimensionLimit) // Check image size
|
if (image.Width > Limits.AvatarDimensionLimit || image.Height > Limits.AvatarDimensionLimit) // Check image size
|
||||||
throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height);
|
throw Errors.AvatarDimensionsTooLarge(image.Width, image.Height);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files
|
// Rewrite cdn.discordapp.com URLs to media.discordapp.net for jpg/png files
|
||||||
// This lets us add resizing parameters to "borrow" their media proxy server to downsize the image
|
// This lets us add resizing parameters to "borrow" their media proxy server to downsize the image
|
||||||
|
Loading…
x
Reference in New Issue
Block a user