示例#1
0
        public static async Task <DbMessage> CreateAsync(string content, DateTimeOffset createTime, bool isSuccessfulCommand, ulong authorId, CollectionReference playerCollection, string docId)
        {
            DbMessage dbMessage = new DbMessage();
            await dbMessage.InitializeAsync(content, createTime, isSuccessfulCommand, authorId, playerCollection, docId);

            return(dbMessage);
        }
示例#2
0
        public async Task LogMessageAsync(SocketUserMessage msg, bool isSuccessfulCommand)
        {
            RecentMessages.Add(await DbMessage.CreateAsync(msg, PlayerCollection, isSuccessfulCommand));

            while (RecentMessages.Count > 5)
            {
                var oldest = RecentMessages.Aggregate((l, r) => l.CreateTime < r.CreateTime ? l : r);

                await oldest.DeleteAsync();

                RecentMessages.Remove(oldest);
            }
        }
示例#3
0
        private async Task OnMessageRecievedAsync(SocketMessage s)
        {
            var msg = s as SocketUserMessage;

            if (msg is null || msg.Author.IsBot)
            {
                return;
            }

            var context = new SocketCommandContext(_discord, msg);

            int  argPos    = 0;
            bool isCommand = msg.HasStringPrefix(_config["prefix"], ref argPos) ||
                             msg.HasMentionPrefix(_discord.CurrentUser, ref argPos);

            bool isSuccess = false;

            var enoughTimePassed = await GetEnoughTimePassedAsync(context.User.Id, context.Message.Timestamp);

            if (isCommand && enoughTimePassed)
            {
                var result = await _commands.ExecuteAsync(context, argPos, _provider);

                if (!result.IsSuccess)
                {
                    Emoji errorEmoji = null;

                    switch (result.Error)
                    {
                    case CommandError.UnknownCommand:
                        errorEmoji = new Emoji("❓");
                        break;

                    case CommandError.ParseFailed:
                    case CommandError.Exception:
                    case CommandError.Unsuccessful:
                        errorEmoji = new Emoji("⚡");
                        break;

                    case CommandError.BadArgCount:
                        errorEmoji = new Emoji("❌");
                        break;

                    case CommandError.UnmetPrecondition:
                        errorEmoji = new Emoji("🤬");
                        break;

                    default:
                        await context.Channel.SendMessageAsync(result.ErrorReason);

                        break;
                    }

                    if (errorEmoji != null)
                    {
                        await context.Message.AddReactionAsync(errorEmoji);
                    }

                    Console.WriteLine
                        ($"[{DateTime.UtcNow.ToString("MM/dd/yyyy hh:mm:ss")}] " +
                        $"{context.Guild.Name} in {context.Channel.Name} " +
                        $"from {context.User.Username}: {result.ErrorReason}");
                }

                isSuccess = result.IsSuccess;
            }

            else if (isCommand && !enoughTimePassed)
            {
                await context.Message.AddReactionAsync(Bot.TimeEmote);
            }

            Player player = await Player.GetAsync(msg.Author.Id, _database.Collection("players"));

            DbMessage mostRecentMsg = player.RecentMessages.Count > 0 ? player.RecentMessages.Aggregate((l, r) => l.CreateTime > r.CreateTime ? l : r) : null;

            if (player.RecentMessages.TrueForAll(m => m.Content != msg.Content) && enoughTimePassed)
            {
                await player.AddBoostAsync(context);
            }

            await player.LogMessageAsync(msg, isCommand&& isSuccess);
        }
示例#4
0
        private async Task InitializeAsync(ulong id, CollectionReference playerCollection)
        {
            PlayerCollection = playerCollection;
            PlayerDoc        = PlayerCollection.Document(id.ToString());

            var playerDocSnap = await PlayerDoc.GetSnapshotAsync();

            if (!playerDocSnap.Exists)
            {
                await PlayerDoc.CreateAsync(new Dictionary <string, object>()
                {
                    { "boost", 0 },
                    { "demo-count", 0 },
                    { "death-count", 0 },
                    { "demo-rate", (DemoData.MinDemoChance + DemoData.MaxDemoChance) / 2 },
                    { "avoid-rate", (DemoData.MinAvoidChance + DemoData.MaxAvoidChance) / 2 },
                    { "avoid-count", 0 },
                    { "miss-count", 0 }
                });

                playerDocSnap = await PlayerDoc.GetSnapshotAsync();
            }

            if (!playerDocSnap.ContainsField("avoid-count"))
            {
                await PlayerDoc.UpdateAsync("avoid-count", 0);

                await PlayerDoc.UpdateAsync("miss-count", 0);
            }

            if (!playerDocSnap.TryGetValue("avoid-count", out int avoidCount))
            {
                avoidCount = 0;
            }

            if (!playerDocSnap.TryGetValue("miss-count", out int missCount))
            {
                missCount = 0;
            }

            if (!playerDocSnap.TryGetValue("boost-used", out int boostUsed))
            {
                boostUsed = 0;
            }

            Data = new DemoData(
                playerDocSnap.GetValue <int>("boost"),
                playerDocSnap.GetValue <int>("demo-count"),
                playerDocSnap.GetValue <int>("death-count"),
                playerDocSnap.GetValue <double>("demo-rate"),
                playerDocSnap.GetValue <double>("avoid-rate"),
                avoidCount,
                missCount);

            RecentMessages = new List <DbMessage>();

            var recentMsgSnap = await PlayerDoc.Collection("recent-messages").GetSnapshotAsync();

            if (recentMsgSnap.Count > 0)
            {
                foreach (var message in recentMsgSnap)
                {
                    //content, createTime
                    if (!message.TryGetValue("content", out string content))
                    {
                        content = "";
                    }
                    if (!message.TryGetValue("create-time", out DateTime createTime))
                    {
                        createTime = new DateTime(0);
                    }
                    if (!message.TryGetValue("is-successful-command", out bool isSuccessfulCommand))
                    {
                        isSuccessfulCommand = false;
                    }

                    RecentMessages.Add(await DbMessage.CreateAsync(content, createTime, isSuccessfulCommand, id, PlayerCollection, message.Id));
                }
            }
        }