Create GetXXX utils wrapping try blocks

This commit is contained in:
Ske
2020-07-02 18:29:04 +02:00
parent 7fef8c1dde
commit c87e67245d
8 changed files with 95 additions and 49 deletions

View File

@@ -15,7 +15,7 @@ namespace PluralKit.Bot
{
var text = ctx.PeekArgument();
if (text.TryParseMention(out var id))
return await ctx.Shard.GetUserAsync(id);
return await ctx.Shard.GetUser(id);
return null;
}
@@ -116,25 +116,14 @@ namespace PluralKit.Bot
public static async Task<DiscordChannel> MatchChannel(this Context ctx)
{
if (!MentionUtils.TryParseChannel(ctx.PeekArgument(), out var channel))
if (!MentionUtils.TryParseChannel(ctx.PeekArgument(), out var id))
return null;
try
{
var discordChannel = await ctx.Shard.GetChannelAsync(channel);
if (discordChannel.Type != ChannelType.Text) return null;
ctx.PopArgument();
return discordChannel;
}
catch (NotFoundException)
{
return null;
}
catch (UnauthorizedException)
{
return null;
}
var channel = await ctx.Shard.GetChannel(id);
if (channel == null || channel.Type != ChannelType.Text) return null;
ctx.PopArgument();
return channel;
}
}
}