Exemple #1
0
    private async ValueTask <bool> TryHandleProxy(MessageCreateEvent evt, Guild guild, Channel channel,
                                                  MessageContext ctx)
    {
        if (ctx.IsDeleting)
        {
            return(false);
        }

        var botPermissions = await _cache.PermissionsIn(channel.Id);

        try
        {
            return(await _proxy.HandleIncomingMessage(evt, ctx, guild, channel, ctx.AllowAutoproxy,
                                                      botPermissions));
        }

        // Catch any failed proxy checks so they get ignored in the global error handler
        catch (ProxyService.ProxyChecksFailedException) { }

        catch (PKError e)
        {
            // User-facing errors, print to the channel properly formatted
            if (botPermissions.HasFlag(PermissionSet.SendMessages))
            {
                await _rest.CreateMessage(evt.ChannelId,
                                          new MessageRequest { Content = $"{Emojis.Error} {e.Message}" });
            }
        }

        return(false);
    }
Exemple #2
0
    public async Task Handle(int shardId, MessageUpdateEvent evt)
    {
        if (evt.Author.Value?.Id == await _cache.GetOwnUser())
        {
            return;
        }

        // Edit message events sometimes arrive with missing data; double-check it's all there
        if (!evt.Content.HasValue || !evt.Author.HasValue || !evt.Member.HasValue)
        {
            return;
        }

        var channel = await _cache.GetChannel(evt.ChannelId);

        if (!DiscordUtils.IsValidGuildChannel(channel))
        {
            return;
        }
        var guild = await _cache.GetGuild(channel.GuildId !.Value);

        var lastMessage = _lastMessageCache.GetLastMessage(evt.ChannelId)?.Current;

        // Only react to the last message in the channel
        if (lastMessage?.Id != evt.Id)
        {
            return;
        }

        // Just run the normal message handling code, with a flag to disable autoproxying
        MessageContext ctx;

        using (_metrics.Measure.Timer.Time(BotMetrics.MessageContextQueryTime))
            ctx = await _repo.GetMessageContext(evt.Author.Value !.Id, channel.GuildId !.Value, evt.ChannelId);

        var equivalentEvt = await GetMessageCreateEvent(evt, lastMessage, channel);

        var botPermissions = await _cache.PermissionsIn(channel.Id);

        try
        {
            await _proxy.HandleIncomingMessage(equivalentEvt, ctx, allowAutoproxy : false, guild : guild,
                                               channel : channel, botPermissions : botPermissions);
        }
        // Catch any failed proxy checks so they get ignored in the global error handler
        catch (ProxyService.ProxyChecksFailedException) { }
    }