public TempRoleModel AddTempRole(ulong guildId, ulong userId, ulong roleId, TimeSpan span) { using (var session = Store.OpenSession()) { var query = session.Query <TempRoleModel>().ToList(); int count; if (query.Any()) { count = int.Parse(query.Max(x => x.Id)) + 1; } else { count = 1; } var newTemp = new TempRoleModel(userId, guildId, roleId, span) { Id = $"TempRole-{count}" }; session.Store(newTemp); session.SaveChanges(); TempRoleModels.Add(newTemp); return(newTemp); } }
public async void TimerEvent(object _) { if (!Initialized) { return; } foreach (var model in TempRoleModels.Where(x => x.ExpiresOn < DateTime.UtcNow.AddMinutes(10)).ToList()) { if (model.ExpiresOn < DateTime.UtcNow) { TempRoleModels = TempRoleModels.Where(x => x.Id == model.Id).ToList(); using (var session = Store.OpenSession()) { var query = session.Query <TempRoleModel>().Where(x => x.Id == model.Id && x.UserId == model.UserId); if (query.Any()) { ReminderModels = ReminderModels.Where(x => x.Id != query.First().Id).ToList(); session.Delete(query.First()); session.SaveChanges(); } } var guild = Client.GetGuild(model.GuildId); var role = guild?.GetRole(model.RoleId); var user = guild?.GetUser(model.UserId); if (guild != null && role != null && user != null) { try { await user.RemoveRoleAsync(role); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } } foreach (var model in ReminderModels.Where(x => x.ExpiresOn < DateTime.UtcNow.AddMinutes(10)).ToList()) { if (model.ExpiresOn < DateTime.UtcNow) { ReminderModels = ReminderModels.Where(x => x.Id != model.Id).ToList(); DelReminder(model.UserId, model.Id); var guild = Client.GetGuild(model.GuildId); var channel = guild?.GetTextChannel(model.ChannelId); var user = Client.GetUser(model.UserId); if (user != null) { var embed = new EmbedBuilder { Description = $"{model.Message}", Footer = new EmbedFooterBuilder { Text = $"Created: {model.CreatedOn.ToShortDateString()}", IconUrl = user.GetAvatarUrl() }, Color = Color.Blue, Title = "Reminder" }; if (channel != null) { try { await channel.SendMessageAsync($"{user.Mention}", false, embed.Build()); } catch { try { await user.SendMessageAsync($"{user.Mention}", false, embed.Build()); } catch { // } } } else { try { await user.SendMessageAsync($"{user.Mention}", false, embed.Build()); } catch { // } } } } } }