public static async Task UpdateAsync(long id, DatabaseSettingId settingId, int value) { using (var context = new MikiContext()) { Setting s = await context.Settings.FindAsync(id, settingId); if (s == null) { await context.AddAsync(new Setting() { EntityId = id, SettingId = settingId, Value = value }); } else { s.Value = value; } await Global.RedisClient.AddAsync(GetKey(id, settingId), value); await context.SaveChangesAsync(); } }
public static async Task SendPM(IDiscordUser user, IDiscordEmbed embed, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { if (CanSendNotification(user.Id, DatabaseEntityType.USER, settingId)) { await user.SendMessage(embed); } }
public static async Task <int> GetAsync(long id, DatabaseSettingId settingId) { if (await Global.RedisClient.ExistsAsync(GetKey(id, settingId))) { return(await Global.RedisClient.GetAsync <int>(GetKey(id, settingId))); } using (var context = new MikiContext()) { Setting s = await context.Settings.FindAsync(id, settingId); if (s == null) { s = (await context.Settings.AddAsync(new Setting() { EntityId = id, SettingId = settingId, Value = 0 })).Entity; } await Global.RedisClient.AddAsync(GetKey(id, settingId), s.Value); return(s.Value); } }
public static async Task SendPM(IDiscordUser user, IDiscordEmbed embed, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { if (CanSendNotification(user.Id, settingId)) { await embed.QueueToUser(user); } }
public static async Task UpdateGuildAsync(IDiscordGuild guild, DatabaseSettingId settingId, int newSetting) { var channels = await guild.GetChannelsAsync(); foreach (var channel in channels) { await UpdateAsync((long)channel.Id, settingId, newSetting); } }
public async Task SetupNotificationsInteractive <T>(EventContext e, DatabaseSettingId settingId) { List <string> options = Enum.GetNames(typeof(T)) .Select(x => x.ToLower() .Replace('_', ' ')) .ToList(); string settingName = settingId.ToString().ToLower().Replace('_', ' '); var sEmbed = SettingsBaseEmbed; sEmbed.Description = ($"What kind of {settingName} do you want"); sEmbed.AddInlineField("Options", string.Join("\n", options)); var sMsg = await sEmbed.ToEmbed().SendToChannel(e.Channel); int newSetting; IDiscordMessage msg = null; while (true) { msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); if (Enum.TryParse <LevelNotificationsSetting>(msg.Content.Replace(" ", "_"), true, out var setting)) { newSetting = (int)setting; break; } await sMsg.EditAsync(new EditMessageArgs() { embed = e.ErrorEmbed("Oh, that didn't seem right! Try again") .AddInlineField("Options", string.Join("\n", options)) .ToEmbed() }); } sMsg = await SettingsBaseEmbed .SetDescription("Do you want this to apply for every channel? say `yes` if you do.") .ToEmbed().SendToChannel(e.Channel as IDiscordGuildChannel); msg = await e.EventSystem.GetCommandHandler <MessageListener>().WaitForNextMessage(e.CreateSession()); bool global = (msg.Content.ToLower()[0] == 'y'); await SettingsBaseEmbed .SetDescription($"Setting `{settingName}` Updated!") .ToEmbed().SendToChannel(e.Channel as IDiscordGuildChannel); if (!global) { await Setting.UpdateAsync(e.Channel.Id, settingId, newSetting); } else { await Setting.UpdateGuildAsync(e.Guild, settingId, newSetting); } }
public static async Task SendPM(ulong userId, IDiscordEmbed embed, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { IUser m = Bot.instance.Client.GetUser(userId); IDiscordUser user = new RuntimeUser(m); if (CanSendNotification(userId, settingId)) { await embed.QueueToUser(user); } }
public static async Task SendPM(IDiscordUser user, string message, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { if (CanSendNotification(user.Id, settingId)) { RuntimeEmbed e = new RuntimeEmbed(new Discord.EmbedBuilder()); e.Title = "NOTIFICATION"; e.Description = message; await e.QueueToUser(user); } }
public async Task SetupNotificationsInteractive <T>(EventContext e, DatabaseSettingId settingId) { List <string> options = Enum.GetNames(typeof(T)) .Select(x => x.ToLower() .Replace('_', ' ')) .ToList(); string settingName = settingId.ToString().ToLower().Replace('_', ' '); var sMsg = await SettingsBaseEmbed .SetDescription($"What kind of {settingName} do you want") .AddInlineField("Options", string.Join("\n", options)) .SendToChannel(e.Channel); int newSetting; while (true) { var msg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); if (Enum.TryParse <LevelNotificationsSetting>(msg.Content.Replace(" ", "_"), true, out var setting)) { newSetting = (int)setting; break; } sMsg.Modify(null, e.ErrorEmbed("Oh, that didn't seem right! Try again") .AddInlineField("Options", string.Join("\n", options))); } sMsg = await SettingsBaseEmbed .SetDescription("Do you want this to apply for every channel? say `yes` if you do.") .SendToChannel(e.Channel); var cMsg = await EventSystem.Instance.ListenNextMessageAsync(e.Channel.Id, e.Author.Id); bool global = (cMsg.Content.ToLower()[0] == 'y'); await SettingsBaseEmbed .SetDescription($"Setting `{settingName}` Updated!") .SendToChannel(e.Channel); if (!global) { await Setting.UpdateAsync(e.Channel.Id, settingId, newSetting); } else { await Setting.UpdateGuildAsync(e.Guild, settingId, newSetting); } }
public static bool CanSendNotification(ulong id, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { bool output = true; Setting setting = null; using (var context = new MikiContext()) { setting = context.Settings.Find(id.ToDbLong(), settingId); if (setting != null) { output = setting.IsEnabled; } } return(output); }
public static async Task SendPM(ulong userId, string message, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { IUser m = Bot.instance.Client.GetUser(userId); IDiscordUser user = new RuntimeUser(m); if (CanSendNotification(userId, settingId)) { RuntimeEmbed e = new RuntimeEmbed(new Discord.EmbedBuilder()); e.Title = "NOTIFICATION"; e.Description = message; await e.QueueToUser(userId); Log.Message("Sent notification to " + user.Username); } }
public static async Task <int> GetAsync( DbContext context, long id, DatabaseSettingId settingId) { Setting s = await context.Set <Setting>() .FindAsync(id, settingId); if (s == null) { s = (await context.Set <Setting>() .AddAsync(new Setting() { EntityId = id, SettingId = settingId, Value = 0 })).Entity; } return(s.Value); }
public static async Task UpdateAsync( DbContext context, long id, DatabaseSettingId settingId, int value) { Setting s = await context.Set <Setting>().FindAsync(id, settingId); if (s == null) { await context.AddAsync(new Setting() { EntityId = id, SettingId = settingId, Value = value }); } else { s.Value = value; } }
public static async Task UpdateAsync(ulong id, DatabaseSettingId settingId, int value) => await UpdateAsync((long)id, settingId, value);
public static bool CanSendNotification(ulong id, DatabaseEntityType entityType = DatabaseEntityType.USER, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { using (var context = new MikiContext()) { Setting setting = context.Settings.Find(id.ToDbLong(), entityType, settingId); if (setting != null) { return(setting.IsEnabled); } return(true); } }
public static async Task SendPM(ulong userId, IDiscordEmbed embed, DatabaseEntityType entityType = DatabaseEntityType.USER, DatabaseSettingId settingId = DatabaseSettingId.PERSONALMESSAGE) { IUser m = Bot.instance.Client.GetUser(userId); IDiscordUser user = new RuntimeUser(m); if (CanSendNotification(userId, entityType, settingId)) { await user?.SendMessage(embed); } }
public static async Task <int> GetAsync( DbContext context, ulong id, DatabaseSettingId settingId) => await GetAsync(context, (long)id, settingId);
public static async Task UpdateAsync( DbContext context, ulong id, DatabaseSettingId settingId, int value) => await UpdateAsync(context, (long)id, settingId, value);
public static async Task <T> GetAsync <T>(ulong id, DatabaseSettingId settingId) where T : struct, IConvertible => (T)(object) await GetAsync((long)id, settingId);
private static string GetKey(long id, DatabaseSettingId setting) => $"miki:settings:{id}:{(int)setting}";
public static async Task <int> GetAsync(ulong id, DatabaseSettingId settingId) => await GetAsync((long)id, settingId);