Exemple #1
0
        // EntityFramework (more like, postgres' lack of array support) only support primitive types, such as string and int, therefore I can't have EditsAsString as a List, which is what I would have preferred.
        // Instead, I encode the string to base64, and separate the edits by a comma, so I can easily decode them.
        // Other libraries such as TypeORM use this method and call them simple-arrays, see https://github.com/typeorm/typeorm/issues/460#issuecomment-299813000.

        public static async Task Update(SocketCommandContext context)
        {
            using (FallprojectContext dbContext = new FallprojectContext()) {
                Message msg = await dbContext.Messages.SingleOrDefaultAsync(x => x.Id == context.Message.Id);

                // Encoding to base64 to guarantee there are no commas, since that's what we're splitting it by later.
                msg.EditsAsString += $"{context.Message.Content.Base64Encode()},";
                await dbContext.SaveChangesAsync();
            }
        }
Exemple #2
0
        // TODO: merge these methods.
        public static async Task Create(SocketCommandContext context)
        {
            using (FallprojectContext dbContext = new FallprojectContext()) {
                // Add the message to the database.
                await dbContext.Messages.AddAsync(new Message {
                    Content   = context.Message.Content,
                    Id        = context.Message.Id,
                    ChannelId = context.Message.Channel.Id,
                    GuildId   = context.Guild.Id,
                    AuthorId  = context.Message.Author.Id
                });

                await dbContext.SaveChangesAsync();
            }
        }
Exemple #3
0
        public static async Task CreateOrUpdate(SocketGuildUser member)
        {
            using (FallprojectContext dbContext = new FallprojectContext()) {
                GuildMember oldMember =
                    await dbContext.GuildMembers.FirstOrDefaultAsync(x => x.Id == member.Id &&
                                                                     x.GuildId == member.Guild.Id);

                if (oldMember == null)
                {
                    oldMember = new GuildMember {
                        Id       = member.Id,
                        GuildId  = member.Guild.Id,
                        UnmuteAt = 0,
                        Xp       = 0
                    };
                    await dbContext.GuildMembers.AddAsync(oldMember);
                }

                await dbContext.SaveChangesAsync();
            }
        }
Exemple #4
0
        public static async Task AddXp(SocketGuildUser member)
        {
            using (FallprojectContext dbContext = new FallprojectContext()) {
                GuildMember oldMember =
                    await dbContext.GuildMembers.FirstOrDefaultAsync(x => x.Id == member.Id &&
                                                                     x.GuildId == member.Guild.OwnerId);

                if (oldMember is null)
                {
                    await CreateOrUpdate(member);

                    oldMember = await dbContext.GuildMembers.FirstOrDefaultAsync(x => x.Id == member.Id &&
                                                                                 x.GuildId ==
                                                                                 member.Guild.OwnerId);
                }

                // We previously create the member if it doesn't exist.
                oldMember.Xp = 10;
                await dbContext.SaveChangesAsync();
            }
        }