PluralKit/PluralKit.Bot/CommandSystem/Context.cs

114 lines
4.4 KiB
C#
Raw Normal View History

2019-10-05 05:41:00 +00:00
using System;
using System.Collections.Generic;
2019-10-05 05:41:00 +00:00
using System.Threading.Tasks;
2019-12-21 20:51:41 +00:00
using App.Metrics;
2020-01-26 00:27:45 +00:00
using Autofac;
using DSharpPlus;
using DSharpPlus.Entities;
using PluralKit.Core;
2019-10-05 05:41:00 +00:00
namespace PluralKit.Bot
2019-10-05 05:41:00 +00:00
{
public class Context
{
2020-01-26 00:27:45 +00:00
private ILifetimeScope _provider;
2019-10-05 05:41:00 +00:00
2020-04-24 19:50:28 +00:00
private readonly DiscordRestClient _rest;
2019-10-05 05:41:00 +00:00
private readonly DiscordShardedClient _client;
private readonly DiscordClient _shard;
private readonly DiscordMessage _message;
2019-10-05 05:41:00 +00:00
private readonly Parameters _parameters;
private readonly MessageContext _messageContext;
2019-10-05 05:41:00 +00:00
private readonly IDataStore _data;
2020-06-29 21:51:12 +00:00
private readonly IDatabase _db;
2019-10-05 05:41:00 +00:00
private readonly PKSystem _senderSystem;
2019-12-21 20:51:41 +00:00
private readonly IMetrics _metrics;
2019-10-05 05:41:00 +00:00
private Command _currentCommand;
public Context(ILifetimeScope provider, DiscordClient shard, DiscordMessage message, int commandParseOffset,
PKSystem senderSystem, MessageContext messageContext)
2019-10-05 05:41:00 +00:00
{
2020-04-24 19:50:28 +00:00
_rest = provider.Resolve<DiscordRestClient>();
2020-01-26 00:27:45 +00:00
_client = provider.Resolve<DiscordShardedClient>();
2019-10-05 05:41:00 +00:00
_message = message;
_shard = shard;
2020-01-26 00:27:45 +00:00
_data = provider.Resolve<IDataStore>();
2019-10-05 05:41:00 +00:00
_senderSystem = senderSystem;
_messageContext = messageContext;
2020-06-29 21:51:12 +00:00
_db = provider.Resolve<IDatabase>();
2020-01-26 00:27:45 +00:00
_metrics = provider.Resolve<IMetrics>();
2019-10-05 05:41:00 +00:00
_provider = provider;
_parameters = new Parameters(message.Content.Substring(commandParseOffset));
}
public DiscordUser Author => _message.Author;
public DiscordChannel Channel => _message.Channel;
public DiscordMessage Message => _message;
public DiscordGuild Guild => _message.Channel.Guild;
public DiscordClient Shard => _shard;
2019-10-05 05:41:00 +00:00
public DiscordShardedClient Client => _client;
public MessageContext MessageContext => _messageContext;
2020-04-24 19:50:28 +00:00
public DiscordRestClient Rest => _rest;
2019-10-05 05:41:00 +00:00
public PKSystem System => _senderSystem;
public Parameters Parameters => _parameters;
2019-10-05 05:41:00 +00:00
// TODO: this is just here so the extension methods can access it; should it be public/private/?
internal IDataStore DataStore => _data;
2020-06-29 21:51:12 +00:00
internal IDatabase Database => _db;
2019-10-05 05:41:00 +00:00
public Task<DiscordMessage> Reply(string text = null, DiscordEmbed embed = null, IEnumerable<IMention> mentions = null)
{
if (!this.BotHasAllPermissions(Permissions.SendMessages))
// Will be "swallowed" during the error handler anyway, this message is never shown.
throw new PKError("PluralKit does not have permission to send messages in this channel.");
if (embed != null && !this.BotHasAllPermissions(Permissions.EmbedLinks))
throw new PKError("PluralKit does not have permission to send embeds in this channel. Please ensure I have the **Embed Links** permission enabled.");
return Channel.SendMessageFixedAsync(text, embed: embed, mentions: mentions);
}
2020-01-11 15:49:20 +00:00
2019-10-05 05:41:00 +00:00
public async Task Execute<T>(Command commandDef, Func<T, Task> handler)
{
_currentCommand = commandDef;
try
{
2020-01-26 00:27:45 +00:00
await handler(_provider.Resolve<T>());
2019-12-21 20:51:41 +00:00
_metrics.Measure.Meter.Mark(BotMetrics.CommandsRun);
2019-10-05 05:41:00 +00:00
}
catch (PKSyntaxError e)
{
2019-12-28 11:16:26 +00:00
await Reply($"{Emojis.Error} {e.Message}\n**Command usage:**\n> pk;{commandDef.Usage}");
2019-10-05 05:41:00 +00:00
}
catch (PKError e)
{
await Reply($"{Emojis.Error} {e.Message}");
}
catch (TimeoutException)
{
// Got a complaint the old error was a bit too patronizing. Hopefully this is better?
await Reply($"{Emojis.Error} Operation timed out, sorry. Try again, perhaps?");
}
2019-10-05 05:41:00 +00:00
}
2020-01-11 15:49:20 +00:00
public LookupContext LookupContextFor(PKSystem target) =>
System?.Id == target.Id ? LookupContext.ByOwner : LookupContext.ByNonOwner;
2020-02-27 23:23:54 +00:00
2020-06-14 19:37:04 +00:00
public LookupContext LookupContextFor(SystemId systemId) =>
2020-02-27 23:23:54 +00:00
System?.Id == systemId ? LookupContext.ByOwner : LookupContext.ByNonOwner;
public LookupContext LookupContextFor(PKMember target) =>
System?.Id == target.System ? LookupContext.ByOwner : LookupContext.ByNonOwner;
2020-05-05 14:03:46 +00:00
public IComponentContext Services => _provider;
2019-10-05 05:41:00 +00:00
}
}