public async void SetPermissionAsync(IGuild guild, Permissions permission, ulong roleId) { var discordGuild = await ObtainAsync(guild); using (var db = new ModixContext()) { if (discordGuild.Config == null) { discordGuild.Config = new Data.Models.GuildConfig { GuildId = guild.Id.ToLong(), AdminRoleId = permission == Permissions.Administrator ? roleId.ToLong() : 0, ModeratorRoleId = permission == Permissions.Moderator ? roleId.ToLong() : 0, }; db.Guilds.Update(discordGuild); await db.SaveChangesAsync(); return; } if (permission == Permissions.Administrator) { discordGuild.Config.AdminRoleId = roleId.ToLong(); } else { discordGuild.Config.ModeratorRoleId = roleId.ToLong(); } db.Guilds.Update(discordGuild); await db.SaveChangesAsync(); } }
public async Task <ServiceResponse> AddPattern(ulong guildId, string regexPattern, MessageContentPatternType patternType) { if (!_authorizationService.HasClaim(AuthorizationClaim.ManageMessageContentPatterns)) { return(ServiceResponse.Fail("User does not have claim to manage patterns!")); } if (!IsValidRegex(regexPattern)) { return(ServiceResponse.Fail("Pattern is not a valid Regex!")); } if (await DoesPatternExist(guildId, regexPattern)) { return(ServiceResponse.Fail("Pattern already exists!")); } var entity = new MessageContentPatternEntity { GuildId = guildId, Pattern = regexPattern, PatternType = patternType, }; _db.Add(entity); await _db.SaveChangesAsync(); ClearCacheForGuild(guildId); return(ServiceResponse.Ok()); }
public async Task <DiscordGuild> AddAsync(IGuild guild) { var service = new DiscordUserService(_context); var owner = await guild.GetOwnerAsync(); var discordGuild = new DiscordGuild() { Config = new GuildConfig(), DiscordId = guild.Id.ToLong(), Name = guild.Name, CreatedAt = guild.CreatedAt.DateTime, Owner = await service.ObtainAsync(owner), }; var res = (await _context.Guilds.AddAsync(discordGuild)).Entity; try { await _context.SaveChangesAsync(); } catch (Exception) { return(null); } return(res); }
/// <inheritdoc /> public async Task <bool> TryUpdateAsync(ulong userId, ulong guildId, Action <GuildUserMutationData> updateAction) { if (updateAction == null) { throw new ArgumentNullException(nameof(updateAction)); } var entity = await ModixContext.GuildUsers .Where(x => x.UserId == userId) .Where(x => x.GuildId == guildId) .Include(x => x.User) .FirstOrDefaultAsync(); if (entity == null) { return(false); } var data = GuildUserMutationData.FromEntity(entity); updateAction.Invoke(data); data.ApplyTo(entity); ModixContext.UpdateProperty(entity.User, x => x.Username); ModixContext.UpdateProperty(entity.User, x => x.Discriminator); ModixContext.UpdateProperty(entity, x => x.Nickname); ModixContext.UpdateProperty(entity, x => x.LastSeen); await ModixContext.SaveChangesAsync(); return(true); }
public async Task BanAsync(IGuildUser author, IGuildUser user, IGuild guild, string reason) { var dbGuild = await _guildService.ObtainAsync(guild); var ban = new Data.Models.Ban { CreatorId = author.Id.ToLong(), UserId = user.Id.ToLong(), Reason = reason, Guild = dbGuild, }; await _context.Bans.AddAsync(ban); await _context.SaveChangesAsync(); }
/// <inheritdoc /> public async Task CreateAsync( GuildUserCreationData data, CancellationToken cancellationToken) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var guildDataEntity = data.ToGuildDataEntity(); guildDataEntity.User = await ModixContext .Set <UserEntity>() .AsQueryable() .FirstOrDefaultAsync(x => x.Id == data.UserId, cancellationToken) ?? data.ToUserEntity(); await ModixContext.Set <GuildUserEntity>().AddAsync(guildDataEntity, cancellationToken); if ((guildDataEntity.User.Username != data.Username) && !(data.Username is null)) { guildDataEntity.User.Username = data.Username; } if ((guildDataEntity.User.Discriminator != data.Discriminator) && !(data.Discriminator is null)) { guildDataEntity.User.Discriminator = data.Discriminator; } await ModixContext.SaveChangesAsync(cancellationToken); }
/// <inheritdoc /> public async Task <long> CreateAsync(InfractionCreationData data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var entity = data.ToEntity(); await ModixContext.Infractions.AddAsync(entity); await ModixContext.SaveChangesAsync(); entity.CreateAction.InfractionId = entity.Id; await ModixContext.SaveChangesAsync(); await RaiseInfractionCreatedAsync(entity.Id, data); await RaiseModerationActionCreatedAsync(entity.CreateActionId, new ModerationActionCreationData() { GuildId = (ulong)entity.CreateAction.GuildId, Type = entity.CreateAction.Type, Created = entity.CreateAction.Created, CreatedById = (ulong)entity.CreateAction.CreatedById }); return(entity.Id); }
/// <inheritdoc /> public async Task <bool> TryRescindAsync(long infractionId, ulong rescindedById) { var longRescindedById = (long)rescindedById; var entity = await ModixContext.Infractions .Where(x => x.Id == infractionId) .FirstOrDefaultAsync(); if ((entity == null) || (entity.RescindActionId != null)) { return(false); } entity.RescindAction = new ModerationActionEntity() { GuildId = entity.GuildId, Type = ModerationActionType.InfractionRescinded, Created = DateTimeOffset.Now, CreatedById = longRescindedById, InfractionId = entity.Id }; await ModixContext.SaveChangesAsync(); await RaiseModerationActionCreatedAsync(entity.RescindActionId.Value, new ModerationActionCreationData() { GuildId = (ulong)entity.RescindAction.GuildId, Type = entity.RescindAction.Type, Created = entity.RescindAction.Created, CreatedById = (ulong)entity.RescindAction.CreatedById }); return(true); }
/// <inheritdoc /> public async Task CreateAsync(DeletedMessageBatchCreationData data) { if (data is null) { throw new ArgumentNullException(nameof(data)); } var entity = data.ToEntity(); await ModixContext.DeletedMessageBatches.AddAsync(entity); await ModixContext.SaveChangesAsync(); entity.CreateAction.DeletedMessageBatchId = entity.Id; await ModixContext.SaveChangesAsync(); var deletedMessageEntities = data.Data.Select(x => { x.BatchId = entity.Id; return(x.ToBatchEntity()); }); await ModixContext.DeletedMessages.AddRangeAsync(deletedMessageEntities); await ModixContext.SaveChangesAsync(); await RaiseModerationActionCreatedAsync(entity.CreateAction); }
/// <inheritdoc /> public async Task <PromotionActionSummary?> TryCloseAsync(long campaignId, ulong closedById, PromotionCampaignOutcome outcome) { var entity = await ModixContext.PromotionCampaigns .Where(x => x.Id == campaignId) .FirstOrDefaultAsync(); if ((entity == null) || (entity.CloseActionId != null)) { return(null); } entity.Outcome = outcome; entity.CloseAction = new PromotionActionEntity() { GuildId = entity.GuildId, Type = PromotionActionType.CampaignClosed, Created = DateTimeOffset.Now, CreatedById = closedById, CampaignId = entity.Id }; await ModixContext.SaveChangesAsync(); var action = await ModixContext.PromotionActions.AsNoTracking() .Where(x => x.Id == entity.CloseActionId) .AsExpandable() .Select(PromotionActionSummary.FromEntityProjection) .FirstAsync(); return(action); }
/// <inheritdoc /> public async Task <bool> UpdateAsync(long infractionId, Action <InfractionMutationData> updateAction) { if (updateAction == null) { throw new ArgumentNullException(nameof(updateAction)); } var entity = await ModixContext.Infractions .Where(x => x.Id == infractionId) .FirstOrDefaultAsync(); if (entity == null) { return(false); } var data = InfractionMutationData.FromEntity(entity); updateAction.Invoke(data); data.ApplyTo(entity); ModixContext.UpdateProperty(entity, x => x.RescindActionId); await ModixContext.SaveChangesAsync(); return(true); }
public async Task <bool> AddModuleLimitAsync(DiscordGuild guild, IMessageChannel channel, string module) { using (var db = new ModixContext()) { var limit = await db.ChannelLimits .Where(c => c.ModuleName.ToUpper() == module.ToUpper() && c.Guild.Id == guild.Id && c.ChannelId == channel.Id.ToLong()) .FirstOrDefaultAsync(); if (limit == null) { await db.ChannelLimits.AddAsync(new ChannelLimit { ModuleName = module, Guild = guild, ChannelId = channel.Id.ToLong() }); await db.SaveChangesAsync(); return(true); } } return(false); }
/// <inheritdoc /> public async Task CreateAsync(GuildUserCreationData data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var longUserId = (long)data.UserId; if (!(await ModixContext.Users.AsNoTracking() .AnyAsync(x => x.Id == longUserId))) { var userEntity = data.ToUserEntity(); await ModixContext.Users.AddAsync(userEntity); await ModixContext.SaveChangesAsync(); } var guildDataEntity = data.ToGuildDataEntity(); await ModixContext.GuildUsers.AddAsync(guildDataEntity); await ModixContext.SaveChangesAsync(); }
/// <inheritdoc /> public async Task CreateAsync(GuildUserCreationData data) { if (data == null) { throw new ArgumentNullException(nameof(data)); } var guildDataEntity = data.ToGuildDataEntity(); guildDataEntity.User = await ModixContext.Users.FirstOrDefaultAsync(x => x.Id == data.UserId) ?? data.ToUserEntity(); await ModixContext.GuildUsers.AddAsync(guildDataEntity); if ((guildDataEntity.User.Username != data.Username) && !(data.Username is null)) { guildDataEntity.User.Username = data.Username; } if ((guildDataEntity.User.Discriminator != data.Discriminator) && !(data.Discriminator is null)) { guildDataEntity.User.Discriminator = data.Discriminator; } await ModixContext.SaveChangesAsync(); }
/// <inheritdoc /> public async Task <bool> TryDeleteAsync(long infractionId, ulong deletedById) { var entity = await ModixContext.Infractions .Where(x => x.Id == infractionId) .FirstOrDefaultAsync(); if ((entity == null) || (entity.DeleteActionId != null)) { return(false); } entity.DeleteAction = new ModerationActionEntity() { GuildId = entity.GuildId, Type = ModerationActionType.InfractionDeleted, Created = DateTimeOffset.Now, CreatedById = deletedById, InfractionId = entity.Id }; await ModixContext.SaveChangesAsync(); await RaiseModerationActionCreatedAsync(entity.DeleteAction); return(true); }
/// <inheritdoc /> public async Task <bool> TryDeleteAsync(long claimMappingId, ulong rescindedById) { var longRescindedById = (long)rescindedById; if (!(await ModixContext.Users.AsNoTracking() .AnyAsync(x => x.Id == longRescindedById))) { return(false); } var entity = await ModixContext.ClaimMappings .Where(x => x.Id == claimMappingId) .FirstOrDefaultAsync(); if ((entity == null) || (entity.DeleteActionId != null)) { return(false); } entity.DeleteAction = new ConfigurationActionEntity() { GuildId = entity.GuildId, Type = ConfigurationActionType.ClaimMappingDeleted, Created = DateTimeOffset.Now, CreatedById = longRescindedById, ClaimMappingId = entity.Id }; await ModixContext.SaveChangesAsync(); return(true); }
/// <inheritdoc /> public async Task <bool> TryRescindAsync(long infractionId, ulong rescindedById, string?rescindReason = null) { var entity = await ModixContext.Set <InfractionEntity>() .Where(x => x.Id == infractionId) .FirstOrDefaultAsync(); if ((entity == null) || (entity.RescindActionId != null)) { return(false); } entity.RescindAction = new ModerationActionEntity() { GuildId = entity.GuildId, Type = ModerationActionType.InfractionRescinded, Created = DateTimeOffset.Now, CreatedById = rescindedById, InfractionId = entity.Id }; entity.RescindReason = rescindReason; await ModixContext.SaveChangesAsync(); await RaiseModerationActionCreatedAsync(entity.RescindAction); return(true); }
/// <inheritdoc /> public async Task <bool> TryUpdateAsync(ulong channelId, Action <GuildChannelMutationData> updateAction) { if (updateAction == null) { throw new ArgumentNullException(nameof(updateAction)); } var entity = await ModixContext.GuildChannels .Where(x => x.ChannelId == channelId) .FirstOrDefaultAsync(); if (entity == null) { return(false); } var data = GuildChannelMutationData.FromEntity(entity); updateAction.Invoke(data); data.ApplyTo(entity); ModixContext.UpdateProperty(entity, x => x.Name); await ModixContext.SaveChangesAsync(); return(true); }
/// <inheritdoc /> public async Task <bool> TryCloseAsync(long campaignId, ulong closedById, PromotionCampaignOutcome outcome) { var entity = await ModixContext.PromotionCampaigns .Where(x => x.Id == campaignId) .FirstOrDefaultAsync(); if ((entity == null) || (entity.CloseActionId != null)) { return(false); } entity.Outcome = outcome; entity.CloseAction = new PromotionActionEntity() { GuildId = entity.GuildId, Type = PromotionActionType.CampaignClosed, Created = DateTimeOffset.Now, CreatedById = closedById, CampaignId = entity.Id }; await ModixContext.SaveChangesAsync(); await RaisePromotionActionCreatedAsync(entity.CloseAction); return(true); }
/// <inheritdoc /> public async Task <bool> TryDeleteAsync(ulong guildId, string name, ulong deletedByUserId) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("The supplied name cannot be null or whitespace.", nameof(name)); } var entity = await ModixContext.Tags.FirstOrDefaultAsync(x => x.GuildId == guildId && x.Name == name.ToLower() && x.DeleteActionId == null); if (entity is null) { return(false); } var deleteAction = new TagActionEntity() { GuildId = entity.GuildId, Created = DateTimeOffset.Now, Type = TagActionType.TagDeleted, CreatedById = deletedByUserId, OldTagId = entity.Id, }; await ModixContext.TagActions.AddAsync(deleteAction); await ModixContext.SaveChangesAsync(); entity.DeleteActionId = deleteAction.Id; await ModixContext.SaveChangesAsync(); return(true); }
/// <inheritdoc /> public async Task <bool> UpdateAsync(ulong guildId, Action <ModerationConfigMutationData> updateAction) { if (updateAction == null) { throw new ArgumentNullException(nameof(updateAction)); } var entity = await ModixContext.ModerationConfigs .Where(x => x.GuildId == (long)guildId) .FirstOrDefaultAsync(); if (entity == null) { return(false); } var data = ModerationConfigMutationData.FromEntity(entity); updateAction.Invoke(data); data.ApplyTo(entity); ModixContext.UpdateProperty(entity, x => x.MuteRoleId); await ModixContext.SaveChangesAsync(); return(true); }
/// <inheritdoc /> public async Task CreateAsync(MessageCreationData data) { var entity = data.ToEntity(); await ModixContext.Messages.AddAsync(entity); await ModixContext.SaveChangesAsync(); }
public async Task <bool> TryUpdateAync(long infractionId, string newReason, ulong updatedById) { var entity = await ModixContext.Infractions .Where(x => x.Id == infractionId) .FirstOrDefaultAsync(); if (entity == null) { return(false); } var originalReason = entity.Reason; entity.Reason = newReason; entity.UpdateAction = new ModerationActionEntity() { GuildId = entity.GuildId, Type = ModerationActionType.InfractionUpdated, Created = DateTimeOffset.Now, CreatedById = updatedById, InfractionId = entity.Id, OriginalInfractionReason = originalReason, }; await ModixContext.SaveChangesAsync(); await RaiseModerationActionCreatedAsync(entity.UpdateAction); return(true); }
public async Task DeleteAsync(ulong messageId) { if (await GetMessage(messageId) is MessageEntity message) { ModixContext.Messages.Remove(message); await ModixContext.SaveChangesAsync(); } }
/// <inheritdoc /> public async Task CreateAsync(GuildRoleCreationData data) { if (data == null) throw new ArgumentNullException(nameof(data)); var entity = data.ToEntity(); await ModixContext.GuildRoles.AddAsync(entity); await ModixContext.SaveChangesAsync(); }
/// <inheritdoc /> public async Task <bool> TryModifyAsync(ulong guildId, string name, ulong modifiedByUserId, Action <TagMutationData> modifyAction) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentException("The supplied name cannot be null or whitespace.", nameof(name)); } if (modifyAction is null) { throw new ArgumentNullException(nameof(modifyAction)); } var oldTag = await ModixContext.Tags.FirstOrDefaultAsync(x => x.GuildId == guildId && x.Name == name.ToLower() && x.DeleteActionId == null); if (oldTag is null) { return(false); } var action = new TagActionEntity() { GuildId = oldTag.GuildId, Type = TagActionType.TagModified, Created = DateTimeOffset.Now, CreatedById = modifiedByUserId, OldTagId = oldTag.Id, }; await ModixContext.TagActions.AddAsync(action); await ModixContext.SaveChangesAsync(); var newTagData = TagMutationData.FromEntity(oldTag); modifyAction(newTagData); var newTag = newTagData.ToEntity(); newTag.CreateActionId = action.Id; await ModixContext.Tags.AddAsync(newTag); await ModixContext.SaveChangesAsync(); action.NewTagId = newTag.Id; oldTag.DeleteActionId = action.Id; await ModixContext.SaveChangesAsync(); return(true); }
/// <inheritdoc /> public async Task UpdateStarboardColumn(ulong messageId, ulong?starboardEntryId) { var entity = await ModixContext.Messages .Where(x => x.Id == messageId) .FirstOrDefaultAsync(); entity.StarboardEntryId = starboardEntryId; ModixContext.Messages.Update(entity); await ModixContext.SaveChangesAsync(); }
public async Task AddCampaign(PromotionCampaignEntity campaign, SocketGuildUser user) { var promoUser = await _context.Users.FirstOrDefaultAsync(u => (ulong)u.Id == user.Id); if (promoUser == null) { // TODO: This needs to be done through IUserService. There are concurrency issues if anyone else manages users in the DB directly. await _context.Users.AddAsync(new UserEntity { Username = $"{user.Username}#{user.Discriminator}", Id = (long)user.Id, Nickname = user.Nickname, }); } campaign.PromotionFor = promoUser; await _context.PromotionCampaigns.AddAsync(campaign); await _context.SaveChangesAsync(); }
/// <inheritdoc /> public async Task DeleteAsync(EmojiSearchCriteria criteria) { if (criteria is null) { throw new ArgumentNullException(nameof(criteria)); } var entities = ModixContext.Emoji.FilterBy(criteria); ModixContext.RemoveRange(entities); await ModixContext.SaveChangesAsync(); }
/// <inheritdoc /> public async Task DeleteAsync(ulong messageId) { var entity = await ModixContext.Messages .Where(x => x.Id == messageId) .FirstOrDefaultAsync(); if (entity is MessageEntity) { ModixContext.Messages.Remove(entity); await ModixContext.SaveChangesAsync(); } }