/// <summary> /// Mutes a guild for the given user. /// </summary> /// <param name="userId">User's ID</param> /// <param name="guildId">Guild's ID</param> public async Task MuteGuildAsync(ulong userId, ulong guildId) { var userRecord = _mutedGuilds.Find(c => c.UserId == userId); if (userRecord?.GuildIds.Contains(guildId) == true) { return; } if (userRecord == null) { userRecord = new UserMutedGuilds { UserId = userId, GuildIds = new List <ulong>() } } ; userRecord.GuildIds.Add(guildId); _mutedGuilds.Add(userRecord); using (var db = new WbContext()) { db.MutedGuilds.Add(new DbUserMutedGuild { UserId = userId, GuildId = guildId }); await db.SaveChangesAsync(); } }
/// <summary> /// Unmutes a guild for the given user. /// </summary> /// <param name="userId"></param> /// <param name="guildId"></param> /// <returns></returns> public async Task UnmuteGuildAsync(ulong userId, ulong guildId) { var userRecord = _mutedGuilds.Find(c => c.UserId == userId); if (userRecord == null || !userRecord.GuildIds.Contains(guildId)) { return; } userRecord.GuildIds.Remove(guildId); using (var db = new WbContext()) { var record = await db.MutedGuilds .FirstOrDefaultAsync(c => c.UserId == userId && c.GuildId == guildId); if (record == null) { return; } db.MutedGuilds.Remove(record); await db.SaveChangesAsync(); } }
/// <summary> /// Removes a keyword from a user. /// </summary> /// <param name="userId">ID of the user to remove the keyword from</param> /// <param name="keyword">Keyword to remove</param> /// <returns>true if the keyword existed and was removed, or false if the user was not subscribed</returns> public async Task <bool> RemoveKeywordAsync(ulong userId, string keyword) { if (!IsKeywordRegex(keyword)) { keyword = keyword.ToLowerInvariant(); } var record = GetRecord(userId, keyword); if (record == null) { return(false); } _keywords.Remove(record); using (var db = new WbContext()) { var dbRecord = await db.Keywords.FirstOrDefaultAsync(k => k.UserId == userId && k.Keyword == keyword); if (dbRecord == null) { return(true); } db.Keywords.Remove(dbRecord); await db.SaveChangesAsync(); } return(true); }
/// <summary> /// Mutes a channel for the given user. /// </summary> /// <param name="userId">User's ID</param> /// <param name="channelId">Channel's ID</param> public async Task MuteChannelAsync(ulong userId, ulong channelId) { var userRecord = _mutedChannels.Find(c => c.UserId == userId); if (userRecord?.ChannelIds.Contains(channelId) == true) { return; } if (userRecord == null) { userRecord = new UserMutedChannels { UserId = userId, ChannelIds = new List <ulong>() } } ; userRecord.ChannelIds.Add(channelId); _mutedChannels.Add(userRecord); using (var db = new WbContext()) { db.MutedChannels.Add(new DbUserMutedChannel { UserId = userId, ChannelId = channelId }); await db.SaveChangesAsync(); } }
public async Task <bool> AddOrModifyAutoreply(Autoreply reply) { var existing = GetExactAutoreply(reply.ChannelId, reply.GuildId, reply.Trigger); if (existing == null) { _autoreplies.Add(reply); using (var db = new WbContext()) { db.Autoreplies.Add(reply); await db.SaveChangesAsync(); } return(true); } existing.Reply = reply.Reply; existing.AddedById = reply.AddedById; existing.AddedAt = reply.AddedAt; using (var db = new WbContext()) { db.Autoreplies.Update(existing); await db.SaveChangesAsync(); } return(false); }
/// <summary> /// Ignores guilds for a given user keyword. /// </summary> /// <param name="userId">ID of the user to add the ignores to</param> /// <param name="keyword">Keyword that is being ignored</param> /// <param name="guildIds">Guild IDs that are being ignored</param> /// <returns>True if success, false if the user isn't subscribed to the provided keyword or /// it is already being ignored</returns> public async Task <IgnoreResult> IgnoreGuildsAsync(ulong userId, string keyword, params ulong[] guildIds) { if (!IsKeywordRegex(keyword)) { keyword = keyword.ToLowerInvariant(); } var record = GetRecord(userId, keyword); if (record == null) { return(IgnoreResult.NotSubscribed); } if (guildIds.All(g => record.IgnoredGuilds.Contains(g))) { return(IgnoreResult.AlreadyIgnored); } var guilds = guildIds.Distinct().Except(record.IgnoredGuilds); var dbGuilds = guilds.Select(c => new DbKeywordIgnoredGuild { KeywordId = record.Id, GuildId = c }); using (var db = new WbContext()) { var dbRecord = await db.Keywords .Include(k => k.IgnoredGuilds) .FirstOrDefaultAsync(k => k.Id == record.Id); if (dbRecord == null) { return(IgnoreResult.NotSubscribed); } foreach (var c in dbGuilds) { dbRecord.IgnoredGuilds.Add(c); } await db.SaveChangesAsync(); } record.IgnoredGuilds.AddRange(guilds); return(IgnoreResult.Success); }
/// <summary> /// Unignores guilds for a given user keyword. /// </summary> /// <param name="userId">ID of the user to remove the ignores from</param> /// <param name="keyword">Keyword that is being unignored</param> /// <param name="guildIds">Guild IDs that are being unignored</param> /// <returns>True if success, false if the user isn't subscribed to the provided keyword</returns> public async Task <UnignoreResult> UnignoreGuildsAsync(ulong userId, string keyword, params ulong[] guildIds) { if (!IsKeywordRegex(keyword)) { keyword = keyword.ToLowerInvariant(); } var record = GetRecord(userId, keyword); if (record == null) { return(UnignoreResult.NotSubscribed); } if (guildIds.All(g => !record.IgnoredGuilds.Contains(g))) { return(UnignoreResult.NotIgnored); } var guilds = guildIds.Distinct().Intersect(record.IgnoredGuilds); using (var db = new WbContext()) { var dbRecord = await db.Keywords .Include(k => k.IgnoredGuilds) .FirstOrDefaultAsync(k => k.Id == record.Id); if (dbRecord == null) { return(UnignoreResult.NotSubscribed); } foreach (var g in dbRecord.IgnoredGuilds.Where(g => guilds.Contains(g.GuildId)).ToList()) { dbRecord.IgnoredGuilds.Remove(g); } await db.SaveChangesAsync(); } record.IgnoredGuilds.RemoveAll(g => guilds.Contains(g)); return(UnignoreResult.Success); }
public async Task <bool> RemoveAutoreply(ulong channelId, ulong guildId, string trigger) { var autoreply = GetExactAutoreply(channelId, guildId, trigger); if (autoreply == null) { return(false); } _autoreplies.Remove(autoreply); using (var db = new WbContext()) { db.Autoreplies.Remove(autoreply); await db.SaveChangesAsync(); } return(true); }
/// <summary> /// Adds a new keyword to a user. /// </summary> /// <param name="userId">ID of the user to add the keyword to</param> /// <param name="keyword">Keyword to add</param> /// <returns>Tuple of the keyword and whether user was already subscribed</returns> public async Task <(KeywordRecord Keyword, bool AlreadyExisted)> AddKeywordAsync(ulong userId, string keyword) { var isKeywordRegex = IsKeywordRegex(keyword); if (!isKeywordRegex) { keyword = keyword.ToLowerInvariant(); } var record = GetRecord(userId, keyword); if (record != null) { return(record, true); } if (isKeywordRegex) { record = new KeywordRecord(userId, keyword, CreateRegex(keyword)); } else { record = new KeywordRecord(userId, keyword); } var dbKeyword = new DbKeyword { UserId = userId, Keyword = keyword }; using (var db = new WbContext()) { db.Keywords.Add(dbKeyword); await db.SaveChangesAsync(); } record.Id = dbKeyword.Id; _keywords.Add(record); return(record, false); }