Exemplo n.º 1
0
        private async Task CheckReservation(string message, DiscordChannel channel, BroadCaptureML.Model.Enum.MessageType messageType)
        {
            var guild = channel.Guild;

            foreach (var reserve in this._dbContext.Reservations)
            {
                if (reserve.Expired)
                {
                    await this._dbContext.Reservations.DeleteAsync(reserve);

                    continue;
                }
                if (message.ToLower().Contains(reserve.Keyword))
                {
                    var member = await guild.GetMemberAsync((ulong)reserve.OwnerId);

                    if (member != null && !shortLiveUidBuffer.Contains((ulong)reserve.OwnerId))
                    {
                        shortLiveUidBuffer.Add((ulong)reserve.OwnerId);
                        var embed = DiscordEmbedHelpers.GenerateEmbedMessage($"Notification for {reserve.Keyword}", message, "Brought to you by Coalescense with love <3", (await discordClientFactory.Client.GetOwnerAsync()).AvatarUrl, DiscordColorHelpers.GetColorForMessage(messageType));
                        var dm    = await member.CreateDmChannelAsync();

                        await dm.SendMessageAsync(embed : embed);
                    }
                }
            }
        }
Exemplo n.º 2
0
        private async Task performOperationTask(CommandContext ctx, string keyword, MessageType type)
        {
            var    author        = ctx.Message.Author;
            var    id            = author.Id;
            string appendMessage = string.Empty;

            try
            {
                var stw = new System.Diagnostics.Stopwatch();
                stw.Start();
                string filter, broadType;
                System.Text.StringBuilder keywordsFilter = new System.Text.StringBuilder();
                switch (type)
                {
                case MessageType.Sell:
                    filter    = $"Type IN ({(long)MessageType.Sell},{(long)MessageType.SellOrTrade},{(long)MessageType.BuyAndSell})";
                    broadType = "sell messages";
                    break;

                case MessageType.Buy:
                    filter    = $"Type IN ({(long)MessageType.Buy},{(long)MessageType.BuyAndSell})";
                    broadType = "buy messages";
                    break;

                case MessageType.Trade:
                    filter    = $"Type IN ({(long)MessageType.Trade},{(long)MessageType.SellOrTrade})";
                    broadType = "trade messages";
                    break;

                default:
                    filter    = "1 = 1";
                    broadType = "everything";
                    break;
                }
                var lookupId    = (long)author.Id;
                var preference  = this._dbContext.Preferences.Query(x => x.UserId == lookupId).FirstOrDefault();
                var keywords    = keyword.Split(',').Select(x => x.Trim()).ToArray();
                var range       = preference?.SearchRange ?? 1;
                var limit       = preference?.SearchLimit ?? 10;
                var param       = new List <RDapter.Entities.DatabaseParameter>();
                var firstAppend = true;
                for (var i = 0; i < keywords.Length; i++)
                {
                    var bindingKey = string.Empty;
                    if (firstAppend)
                    {
                        firstAppend = false;
                        bindingKey  = "AND";
                    }
                    else
                    {
                        bindingKey = "OR";
                    }
                    var kw = keywords[i].Trim();
                    keywordsFilter.AppendLine($" {bindingKey} LOWER(content) LIKE @keyword{i}");
                    param.Add(new RDapter.Entities.DatabaseParameter($"keyword{i}", $"%{kw.ToLower()}%"));
                }
                if (string.IsNullOrWhiteSpace(keyword))
                {
                    await ctx.RespondAsync("search keyword must not be empty.");

                    return;
                }
                var            retry = 0;
                List <Message> result;
                var            limitDate = DateTime.Today.AddDays(-range);
                do
                {
                    var copyParam = param.ConvertAll(x => x);
                    range += (retry * 30);
                    copyParam.Add(new RDapter.Entities.DatabaseParameter("date", DateTime.Today.AddDays(-range)));
                    result = (List <Message>) await this._dbContext.Messages.ExecuteDirectFunctionAsync("search", new object[] {
                        filter,
                        keywordsFilter.ToString(),
                        limitDate,
                        limit,
                        copyParam
                    });
                } while (result.Count == 0 && retry++ < 2);
                if (retry == 1)
                {
                    appendMessage = " (Automatic Extended Range)";
                }
                if (result.Count() == 0)
                {
                    await ctx.Channel.SendDisposableMessageAsync($"No data related to '{keyword}' found.");

                    return;
                }
                stw.Stop();
                Action <DiscordEmbedBuilder> replyAction;
                if (ctx.Member != null)
                {
                    var dmChannel = await ctx.Member.CreateDmChannelAsync();

                    replyAction = async(s) =>
                    {
                        try
                        {
                            await dmChannel.SendMessageAsync("", embed : s);
                        }
                        catch
                        {
                            await ctx.RespondAsync("", embed : s);
                        }
                    };
                }
                else
                {
                    replyAction = async(s) =>
                    {
                        await ctx.RespondAsync("", embed : s);
                    };
                }
                var now   = DateTime.Now;
                var embed = DiscordEmbedHelpers.GenerateEmbedMessage($"Result for {broadType} related to {keyword} :",
                                                                     null,
                                                                     $"Brought to you by Coalescense with love <3 ({(float)stw.ElapsedMilliseconds / 1000} seconds processed)\nwith {range} days{appendMessage} {limit} messages preset.",
                                                                     (await ctx.Client.GetOwnerAsync()).AvatarUrl,
                                                                     DiscordColorHelpers.GetRandomColor());
                foreach (var broad in result)
                {
                    var prefixTime = string.Empty;
                    var diff       = now.Subtract(broad.createdate.Value);
                    if (diff.Days > 0)
                    {
                        prefixTime = $"{diff.Days} days ago";
                    }
                    else if (diff.Hours > 0)
                    {
                        prefixTime = $"{diff.Hours} hours ago";
                    }
                    else
                    {
                        prefixTime = $"{diff.Minutes} minutes ago";
                    }
                    if (embed.Fields.Count == 25)
                    {
                        replyAction(embed);
                        embed = DiscordEmbedHelpers.GenerateEmbedMessage($"Result for {broadType} related to {keyword} :",
                                                                         null,
                                                                         $"Brought to you by Coalescense with love <3 ({(float)stw.ElapsedMilliseconds / 1000} seconds processed)\nwith {range} days{appendMessage} {limit} messages preset.",
                                                                         (await ctx.Client.GetOwnerAsync()).AvatarUrl,
                                                                         DiscordColorHelpers.GetRandomColor());
                    }
                    embed.AddField($"{prefixTime} by {broad.createby}", broad.content);
                }
                replyAction(embed);
            }
            finally
            {
                //await ctx.Message.DeleteAsync();
            }
        }