コード例 #1
0
        public async Task DropUser(ulong userId)
        {
            using var database = new SkuldDbContextFactory().CreateDbContext();

            await database.DropUserAsync(userId).ConfigureAwait(false);

            await database.SaveChangesAsync().ConfigureAwait(false);
        }
コード例 #2
0
        public async Task Merge(ulong oldId, ulong newId)
        {
            if (Context.Client.GetUser(newId) == null)
            {
                await $"No. {newId} is not a valid user Id"
                .QueueMessageAsync(Context).ConfigureAwait(false);
                return;
            }
            if (newId == oldId)
            {
                await $"No.".QueueMessageAsync(Context).ConfigureAwait(false);
                return;
            }
            try
            {
                //UserAccount
                {
                    using var db = new SkuldDbContextFactory()
                                   .CreateDbContext();

                    var oldUser = db.Users.Find(oldId);
                    var newUser = db.Users.Find(newId);

                    if (oldUser != null && newUser != null)
                    {
                        TransactionService.DoTransaction(new TransactionStruct
                        {
                            Amount   = oldUser.Money,
                            Receiver = newUser
                        });
                        newUser.Title          = oldUser.Title;
                        newUser.Language       = oldUser.Language;
                        newUser.Patted         = oldUser.Patted;
                        newUser.Pats           = oldUser.Pats;
                        newUser.UnlockedCustBG = oldUser.UnlockedCustBG;
                        newUser.Background     = oldUser.Background;

                        await db.SaveChangesAsync().ConfigureAwait(false);
                    }
                }

                //Reputation
                {
                    using var db = new SkuldDbContextFactory()
                                   .CreateDbContext();

                    var repee = db.Reputations.AsQueryable()
                                .Where(x => x.Repee == oldId);
                    var reper = db.Reputations.AsQueryable()
                                .Where(x => x.Reper == oldId);

                    if (repee.Any())
                    {
                        foreach (var rep in repee)
                        {
                            if (!db.Reputations
                                .Any(x =>
                                     x.Repee == newId &&
                                     x.Reper == rep.Reper
                                     )
                                )
                            {
                                rep.Repee = newId;
                            }
                        }
                    }

                    if (reper.Any())
                    {
                        foreach (var rep in reper)
                        {
                            if (!db.Reputations
                                .Any(x =>
                                     x.Reper == newId &&
                                     x.Repee == rep.Repee
                                     )
                                )
                            {
                                rep.Reper = newId;
                            }
                        }
                    }

                    if (repee.Any() || reper.Any())
                    {
                        await db.SaveChangesAsync().ConfigureAwait(false);
                    }
                }

                //Pastas
                {
                    using var db = new SkuldDbContextFactory()
                                   .CreateDbContext();

                    var pastas = db.Pastas.AsQueryable()
                                 .Where(x => x.OwnerId == oldId);

                    if (pastas.Any())
                    {
                        foreach (var pasta in pastas)
                        {
                            pasta.OwnerId = newId;
                        }

                        await db.SaveChangesAsync().ConfigureAwait(false);
                    }
                }

                //PastaVotes
                {
                    using var db = new SkuldDbContextFactory()
                                   .CreateDbContext();

                    var pastaVotes = db.PastaVotes.AsQueryable()
                                     .Where(x => x.VoterId == oldId);

                    if (pastaVotes.Any())
                    {
                        foreach (var pasta in pastaVotes)
                        {
                            pasta.VoterId = newId;
                        }
                    }

                    await db.SaveChangesAsync().ConfigureAwait(false);
                }

                //CommandUsage
                {
                    using var db = new SkuldDbContextFactory()
                                   .CreateDbContext();

                    var commands = db.UserCommandUsage.AsQueryable()
                                   .Where(x => x.UserId == oldId);

                    if (commands.Any())
                    {
                        foreach (var command in commands)
                        {
                            command.UserId = newId;
                        }

                        await db.SaveChangesAsync().ConfigureAwait(false);
                    }
                }

                //Experience
                {
                    using var db = new SkuldDbContextFactory()
                                   .CreateDbContext();

                    var experiences = db.UserXp.AsQueryable()
                                      .Where(x => x.UserId == oldId);

                    var newExperiences = db.UserXp.AsQueryable()
                                         .Where(x => x.UserId == newId);

                    if (experiences.Any() && !newExperiences.Any())
                    {
                        foreach (var experience in experiences)
                        {
                            experience.UserId = newId;
                        }
                    }
                    else if (experiences.Any() && newExperiences.Any())
                    {
                        foreach (var experience in experiences)
                        {
                            if (newExperiences
                                .Any(x =>
                                     x.GuildId == experience.GuildId
                                     )
                                )
                            {
                                var xp = newExperiences
                                         .FirstOrDefault(x =>
                                                         x.GuildId == experience.GuildId
                                                         );

                                xp.TotalXP = xp.TotalXP
                                             .Add(experience.TotalXP);
                            }
                        }
                    }

                    await db.SaveChangesAsync().ConfigureAwait(false);
                }

                //Prune Old User
                {
                    using var db = new SkuldDbContextFactory().CreateDbContext();

                    await db.DropUserAsync(oldId).ConfigureAwait(false);
                }

                await $"Successfully merged data from {oldId} into {newId}".QueueMessageAsync(Context).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Log.Error("MergeCmd", ex.Message, Context, ex);
                await "Check the console log".QueueMessageAsync(Context).ConfigureAwait(false);
            }
        }