Ejemplo n.º 1
0
        public override ValueTask <MessageReceivedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, MessageJsonModel model)
        {
            CachedMember    author  = null;
            IGatewayMessage message = null;

            if (model.GuildId.HasValue && !model.WebhookId.HasValue && model.Member.HasValue &&
                Client.CacheProvider.TryGetUsers(out var userCache) &&
                Client.CacheProvider.TryGetMembers(model.GuildId.Value, out var memberCache))
            {
                model.Member.Value.User = model.Author;
                author = Dispatcher.GetOrAddMember(userCache, memberCache, model.GuildId.Value, model.Member.Value);
                foreach (var userModel in model.Mentions)
                {
                    var memberModel = userModel["member"]?.ToType <MemberJsonModel>();
                    if (memberModel == null)
                    {
                        continue;
                    }

                    memberModel.User = userModel;
                    Dispatcher.GetOrAddMember(userCache, memberCache, model.GuildId.Value, memberModel);
                }

                if (CacheProvider.TryGetMessages(model.ChannelId, out var messageCache) &&
                    model.Type is UserMessageType.Default or UserMessageType.Reply or UserMessageType.SlashCommand or UserMessageType.ThreadStarterMessage or UserMessageType.ContextMenuCommand)
                {
                    message = new CachedUserMessage(Client, author, model);
                    messageCache.Add(model.Id, message as CachedUserMessage);
                }
            }

            message ??= TransientGatewayMessage.Create(Client, model);

            CachedMessageGuildChannel channel = null;

            if (model.GuildId.HasValue && CacheProvider.TryGetChannels(model.GuildId.Value, out var channelCache))
            {
                channel = channelCache.GetValueOrDefault(model.ChannelId) as CachedMessageGuildChannel;
                if (channel != null)
                {
                    channel.LastMessageId = model.Id;
                }
            }

            var e = new MessageReceivedEventArgs(message, channel, author);

            return(new(e));
        }
Ejemplo n.º 2
0
        public override ValueTask <ChannelPinsUpdatedEventArgs> HandleDispatchAsync(IGatewayApiClient shard, ChannelPinsUpdateJsonModel model)
        {
            CachedMessageGuildChannel channel = null;

            if (model.GuildId.HasValue)
            {
                if (CacheProvider.TryGetChannels(model.GuildId.Value, out var cache))
                {
                    channel = cache.GetValueOrDefault(model.ChannelId) as CachedMessageGuildChannel;
                    channel?.Update(model);
                }
            }

            var e = new ChannelPinsUpdatedEventArgs(model.GuildId.GetValueOrNullable(), model.ChannelId, channel);

            return(new(e));
        }
Ejemplo n.º 3
0
        internal async ValueTask <bool> ProcessCommandsAsync(IGatewayUserMessage message, CachedMessageGuildChannel channel)
        {
            // We check if the message is suitable for execution.
            // By default excludes bot messages.
            try
            {
                if (!await CheckMessageAsync(message).ConfigureAwait(false))
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "An exception occurred while executing the check message callback.");
                return(false);
            }

            // We get the prefixes from the prefix provider.
            IEnumerable <IPrefix> prefixes;

            try
            {
                prefixes = await Prefixes.GetPrefixesAsync(message).ConfigureAwait(false);

                if (prefixes == null)
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "An exception occurred while getting the prefixes.");
                return(false);
            }

            // We try to find a prefix in the message.
            IPrefix foundPrefix = null;
            string  output      = null;

            try
            {
                foreach (var prefix in prefixes)
                {
                    if (prefix == null)
                    {
                        continue;
                    }

                    if (prefix.TryFind(message, out output))
                    {
                        foundPrefix = prefix;
                        break;
                    }
                }

                if (foundPrefix == null)
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "An exception occurred while finding the prefixes in the message.");
                return(false);
            }

            // We create a command context for Qmmands.
            DiscordCommandContext context;

            try
            {
                context = CreateCommandContext(foundPrefix, output, message, channel);
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "An exception occurred while creating the command context.");
                return(false);
            }

            // We check the before execution callback, by default returns true.
            try
            {
                if (!await BeforeExecutedAsync(context).ConfigureAwait(false))
                {
                    return(false);
                }
            }
            catch (Exception ex)
            {
                await DisposeContextAsync(context).ConfigureAwait(false);

                Logger.LogError(ex, "An exception occurred while executing the before executed callback.");
                return(false);
            }

            // We post the execution to the command queue.
            // See the Post() method in the default queue for more information.
            try
            {
                Queue.Post(context, context => context.Bot.ExecuteAsync(context));
                return(true);
            }
            catch (Exception ex)
            {
                await DisposeContextAsync(context).ConfigureAwait(false);

                Logger.LogError(ex, "An exception occurred while posting the execution to the command queue.");
                return(false);
            }
        }