public async Task <LoggedUser> GetLoggedUser(ulong userId) { LoggedUser temp = null; using (SQLiteConnection db = new SQLiteConnection(ConnectionString)) { await db.OpenAsync(); using (var cmd = new SQLiteCommand("select * from users where UserId = @1;", db)) { cmd.Parameters.AddWithValue("@1", userId); using (var reader = await cmd.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { temp = new LoggedUser() { UserId = Convert.ToUInt64((string)reader["UserId"]), ApprovedAccess = (long)reader["ApprovedAccess"] == 1 ? true : false, NewAccount = (long)reader["NewAccount"] == 1 ? true : false, ApprovalModId = reader["ApprovalModId"] == DBNull.Value ? 0 : Convert.ToUInt64((string)reader["ApprovalModId"]), ApprovalReason = reader["ApprovalReason"] == DBNull.Value ? null : (string)reader["ApprovalReason"] }; } } } db.Close(); } return(temp); }
public async Task <LoggedUser> AddLoggedUser(ulong userId, bool approvedAccess = false, bool newAccount = false) { LoggedUser temp = new LoggedUser() { UserId = userId, ApprovedAccess = approvedAccess, NewAccount = newAccount }; await BulkAddLoggedUser(new List <LoggedUser> { temp }); return(temp); }
private async Task Client_UserJoined(SocketGuildUser user) { try { if (user.Guild.Id == config.HomeGuildId) { string message = $":wave: " + $"**User Joined** `{DateTime.Now.ToString("d")} {DateTime.Now.ToString("T")}`\n" + $"{user.Username}#{user.Discriminator} ({user.Id}) ({user.Mention})\n" + $"**Account created** `{user.CreatedAt.ToLocalTime().ToString("d")} {user.CreatedAt.ToLocalTime().ToString("T")}`"; if (config.WatchedIds.ContainsKey(user.Id)) { if (config.AlternateStaffMention) { message = $"{message}\n<@&{config.AlternateStaffId}> This user has been flagged! {config.WatchedIds[user.Id]}"; } else { message = $"{message}\n<@&{config.StaffId}> This user has been flagged! {config.WatchedIds[user.Id]}"; } } await(client.GetGuild(config.HomeGuildId).GetChannel(config.MainChannelId) as ISocketMessageChannel) .SendMessageAsync(message); if (user.Guild.VerificationLevel < VerificationLevel.Extreme) { return; } LoggedUser loggedUser = await dbhelper.GetLoggedUser(user.Id); if (loggedUser == null) { loggedUser = await dbhelper.AddLoggedUser ( user.Id, newAccount : user.CreatedAt.Date > DateTimeOffset.Now.AddDays(config.MinimumAccountAge * -1) ); } if (loggedUser.ApprovedAccess) { Console.WriteLine($"Found approved user {loggedUser.UserId}"); try { await user.SendMessageAsync(WelcomeBackMessage); } catch (Exception ex) { Console.WriteLine($"Error sending welcome message to {user.Id}!\nMessage: {ex.Message}\nSource: {ex.Source}\n{ex.InnerException}"); } try { var role = client.GetGuild(config.HomeGuildId).GetRole(config.AccessRoleId); if (client.GetGuild(config.HomeGuildId).GetUser(user.Id) == null) { Console.WriteLine($"{user.Id} isn't in the server anymore!"); return; } await user.AddRoleAsync(role); } catch (Exception ex) { Console.WriteLine($"Error adding role to {user.Id}\nMessage: {ex.Message}\nSource: {ex.Source}\n{ex.InnerException}"); } } else if (!loggedUser.NewAccount) { Console.WriteLine("Found old account but new join"); try { await user.SendMessageAsync(WelcomeMessage); } catch (Exception ex) { Console.WriteLine($"Error sending welcome message to {user.Id}!\nMessage: {ex.Message}\nSource: {ex.Source}\n{ex.InnerException}"); } CancellationTokenSource source = new CancellationTokenSource(); waitingUsers.Add(user.Id, source); Task.Run(async() => DelayAddRole(user.Id, source.Token), source.Token); dbhelper.AutoApproveUser(user.Id); } else { Console.WriteLine("Found new account"); await Task.Delay(1000); await(client.GetGuild(config.HomeGuildId).GetChannel(config.FilteredChannelId) as ISocketMessageChannel) .SendMessageAsync($"<:marxist_think:305877855366152193> " + $"**User Joined** `{DateTime.Now.ToString("d")} {DateTime.Now.ToString("T")}`\n" + $"{user.Username}#{user.Discriminator} ({user.Id}) ({user.Mention})\n" + $"**Account created** `{user.CreatedAt.ToLocalTime().ToString("d")} {user.CreatedAt.ToLocalTime().ToString("T")}`"); try { await user.SendMessageAsync(NewUserNotice); } catch (Exception ex) { Console.WriteLine($"Error sending new user message to {user.Id}!\nMessage: {ex.Message}\nSource: {ex.Source}\n{ex.InnerException}"); } } } } catch (Exception ex) { Log(ex); } }