Remove message query reaction AND open DB connection when obtaining one

This commit is contained in:
Ske
2019-07-14 05:23:27 +02:00
parent a41e20a0a3
commit ebc311ecc3
7 changed files with 68 additions and 38 deletions

View File

@@ -34,7 +34,7 @@ namespace PluralKit.Bot
using (var services = BuildServiceProvider())
{
Console.WriteLine("- Connecting to database...");
using (var conn = services.GetRequiredService<DbConnectionFactory>().Obtain())
using (var conn = await services.GetRequiredService<DbConnectionFactory>().Obtain())
await Schema.CreateTables(conn);
Console.WriteLine("- Connecting to Discord...");
@@ -179,7 +179,7 @@ namespace PluralKit.Bot
// and start command execution
// Note system may be null if user has no system, hence `OrDefault`
PKSystem system;
using (var conn = serviceScope.ServiceProvider.GetService<DbConnectionFactory>().Obtain())
using (var conn = await serviceScope.ServiceProvider.GetService<DbConnectionFactory>().Obtain())
system = await conn.QueryFirstOrDefaultAsync<PKSystem>("select systems.* from systems, accounts where accounts.uid = @Id and systems.id = accounts.system", new { Id = arg.Author.Id });
await _commands.ExecuteAsync(new PKCommandContext(_client, arg, system), argPos, serviceScope.ServiceProvider);
}

View File

@@ -30,7 +30,7 @@ namespace PluralKit.Bot {
}
public async Task<ITextChannel> GetLogChannel(IGuild guild) {
using (var conn = _conn.Obtain())
using (var conn = await _conn.Obtain())
{
var server =
await conn.QueryFirstOrDefaultAsync<ServerDefinition>("select * from servers where id = @Id",
@@ -46,7 +46,7 @@ namespace PluralKit.Bot {
LogChannel = newLogChannel?.Id
};
using (var conn = _conn.Obtain())
using (var conn = await _conn.Obtain())
{
await conn.QueryAsync(
"insert into servers (id, log_channel) values (@Id, @LogChannel) on conflict (id) do update set log_channel = @LogChannel",

View File

@@ -79,7 +79,7 @@ namespace PluralKit.Bot
public async Task HandleMessageAsync(IMessage message)
{
IEnumerable<ProxyDatabaseResult> results;
using (var conn = _conn.Obtain())
using (var conn = await _conn.Obtain())
{
results = await conn.QueryAsync<PKMember, PKSystem, ProxyDatabaseResult>(
"select members.*, systems.* from members, systems, accounts where members.system = systems.id and accounts.system = systems.id and accounts.uid = @Uid",
@@ -168,21 +168,29 @@ namespace PluralKit.Bot
return HandleMessageDeletionByReaction(message, reaction.UserId);
case "\u2753": // Red question mark
case "\u2754": // White question mark
return HandleMessageQueryByReaction(message, reaction.UserId);
return HandleMessageQueryByReaction(message, reaction.UserId, reaction.Emote);
default:
return Task.CompletedTask;
}
}
private async Task HandleMessageQueryByReaction(Cacheable<IUserMessage, ulong> message, ulong userWhoReacted)
private async Task HandleMessageQueryByReaction(Cacheable<IUserMessage, ulong> message, ulong userWhoReacted, IEmote reactedEmote)
{
// Find the user who sent the reaction, so we can DM them
var user = await _client.GetUserAsync(userWhoReacted);
if (user == null) return;
// Find the message in the DB
var msg = await _messageStorage.Get(message.Id);
if (msg == null) return;
// DM them the message card
await user.SendMessageAsync(embed: await _embeds.CreateMessageInfoEmbed(msg));
// And finally remove the original reaction (if we can)
var msgObj = await message.GetOrDownloadAsync();
if (await msgObj.Channel.HasPermission(ChannelPermission.ManageMessages))
await msgObj.RemoveReactionAsync(reactedEmote, user);
}
public async Task HandleMessageDeletionByReaction(Cacheable<IUserMessage, ulong> message, ulong userWhoReacted)

View File

@@ -86,6 +86,25 @@ namespace PluralKit.Bot
public static string Sanitize(this string input) =>
Regex.Replace(Regex.Replace(input, "<@[!&]?(\\d{17,19})>", "<\\@$1>"), "@(everyone|here)", "@\u200B$1");
public static async Task<ChannelPermissions> PermissionsIn(this IChannel channel)
{
switch (channel)
{
case IDMChannel _:
return ChannelPermissions.DM;
case IGroupChannel _:
return ChannelPermissions.Group;
case IGuildChannel gc:
var currentUser = await gc.Guild.GetCurrentUserAsync();
return currentUser.GetPermissions(gc);
default:
return ChannelPermissions.None;
}
}
public static async Task<bool> HasPermission(this IChannel channel, ChannelPermission permission) =>
(await PermissionsIn(channel)).Has(permission);
}
class PKSystemTypeReader : TypeReader