Exemple #1
0
        public static async Task <Server> GetOrCreateServerAsync(ulong id)
        {
            using (var db = new KaguyaDb())
            {
                bool exists = db.Servers.Any(x => x.ServerId == id);
                if (!exists)
                {
                    await db.InsertAsync(new Server
                    {
                        ServerId = id
                    });

                    await ConsoleLogger.LogAsync($"Server {id} created.", LogLvl.DEBUG);
                }

                return(await db.Servers
                       .LoadWith(x => x.AntiRaid)
                       .LoadWith(x => x.AutoAssignedRoles)
                       .LoadWith(x => x.BlackListedChannels)
                       .LoadWith(x => x.CommandHistory)
                       .LoadWith(x => x.FilteredPhrases)
                       .LoadWith(x => x.Fish)
                       .LoadWith(x => x.MutedUsers)
                       .LoadWith(x => x.Praise)
                       .LoadWith(x => x.Quotes)
                       .LoadWith(x => x.RoleRewards)
                       .LoadWith(x => x.ServerExp)
                       .LoadWith(x => x.WarnedUsers)
                       .LoadWith(x => x.WarnSettings)
                       .Where(s => s.ServerId == id).FirstAsync());
            }
        }
Exemple #2
0
        public static async Task <int> SafeAddQuoteAsync(Server server, Quote quote)
        {
            using (var db = new KaguyaDb())
            {
                await db.BeginTransactionAsync();

                try
                {
                    int id = await db.Servers
                             .Where(s => s.ServerId == server.ServerId)
                             .Select(s => s.NextQuoteId).FirstOrDefaultAsync();

                    quote.Id = id;
                    int updateQuote = await db.InsertAsync(quote);

                    IUpdatable <Server> statement = db.Servers
                                                    .Where(s => s.ServerId == server.ServerId)
                                                    .Set(i => i.NextQuoteId, id + 1);

                    await db.CommitTransactionAsync();

                    return(id);
                }
                catch (Exception e)
                {
                    await ConsoleLogger.LogAsync(e);

                    await db.RollbackTransactionAsync();
                }
            }

            return(-1);
        }
Exemple #3
0
        public async Task Post([FromBody] TopGgWebhook baseHook, [FromHeader(Name = "Authorization")] string auth)
        {
            if (auth != _cfg.TopGgApiKey)
            {
                return;
            }

            var dbWebhook = new DatabaseUpvoteWebhook
            {
                BotId       = baseHook.BotId.AsUlong(),
                UserId      = baseHook.UserId.AsUlong(),
                UpvoteType  = baseHook.Type,
                IsWeekend   = baseHook.IsWeekend,
                QueryParams = baseHook.Query,
                TimeVoted   = DateTime.Now.ToOADate(),
                VoteId      = Guid.NewGuid().ToString()
            };

            try
            {
                await _db.InsertAsync(dbWebhook);

                _uvNotifier.Enqueue(dbWebhook);
            }
            catch (Exception e)
            {
                await ConsoleLogger.LogAsync(e, "An error occurred when trying to insert a Top.GG authorized " +
                                             $"webhook for user {dbWebhook.UserId}.");

                return;
            }


            await ConsoleLogger.LogAsync($"[Kaguya Api]: Authorized Top.GG Webhook received for user {dbWebhook.UserId}.", LogLvl.INFO);
        }
Exemple #4
0
 public static async Task InsertAsync <T>(IEnumerable <T> arg) where T : class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
     {
         foreach (T element in arg)
         {
             await db.InsertAsync(element);
         }
     }
 }
Exemple #5
0
        /// <summary>
        ///     Inserts the object into the database. If it already exists, an exception will be thrown.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arg">The object to insert, assuming it doesn't already exist.</param>
        /// <param name="throwExceptionIfPresent">Whether to throw an exception if the object was found in the database already.</param>
        /// <returns></returns>
        public static async Task InsertIfNotExistsAsync <T>(T arg, bool throwExceptionIfPresent = true) where T : class, IKaguyaQueryable <T>
        {
            using (var db = new KaguyaDb())
            {
                if (await db.GetTable <T>().AnyAsync(x => x.Equals(arg)))
                {
                    if (throwExceptionIfPresent)
                    {
                        throw new Exception("Item already exists in the database.");
                    }
                }

                await db.InsertAsync(arg);
            }
        }
Exemple #6
0
        public static async Task <User> GetOrCreateUserAsync(ulong id)
        {
            using (var db = new KaguyaDb())
            {
                bool exists = db.Users.Any(x => x.UserId == id);

                if (!exists)
                {
                    try
                    {
                        await db.InsertAsync(new User
                        {
                            UserId = id
                        });
                    }
                    catch (Exception e)
                    {
                        await ConsoleLogger.LogAsync($"Failed to create User with ID {id}\nException: {e}", LogLvl.ERROR);

                        throw;
                    }

                    await ConsoleLogger.LogAsync($"User {id} created.", LogLvl.DEBUG);
                }

                return(await db.Users
                       .LoadWith(x => x.Blacklist)
                       .LoadWith(x => x.CommandHistory)
                       .LoadWith(x => x.Fish)
                       .LoadWith(x => x.GambleHistory)
                       .LoadWith(x => x.Quotes)
                       .LoadWith(x => x.Reminders)
                       .LoadWith(x => x.Rep)
                       .LoadWith(x => x.ServerExp)
                       .Where(u => u.UserId == id).FirstAsync());
            }
        }
Exemple #7
0
 /// <summary>
 ///     Inerts a new <see cref="IKaguyaQueryable{T}" /> object into the database. Do not use if wanting to
 ///     update.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="arg"></param>
 /// <returns></returns>
 public static async Task InsertAsync <T>(T arg) where T : class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
         await db.InsertAsync(arg);
 }