public ISocketMessage AddMessage(ISocketUser author, MessageModel model) { var msg = _messages.Create(author, model); _messages.Add(msg); return(msg); }
/// <summary> /// Creates a ban infraction for the user if they do not already have one. /// </summary> /// <param name="guild">The guild that the user was banned from.</param> /// <param name="user">The user who was banned.</param> /// <returns> /// A <see cref="Task"/> that will complete when the operation completes. /// </returns> private async Task TryCreateBanInfractionAsync(ISocketUser user, ISocketGuild guild) { if (await _moderationService.AnyInfractionsAsync(GetBanSearchCriteria(guild, user))) { return; } var restGuild = await _restClient.GetGuildAsync(guild.Id); var auditLogs = (await restGuild.GetAuditLogsAsync(10) .FlattenAsync()) .Where(x => x.Action == ActionType.Ban && x.Data is BanAuditLogData) .Select(x => (Entry: x, Data: (BanAuditLogData)x.Data)); var banLog = auditLogs.FirstOrDefault(x => x.Data.Target.Id == user.Id); var reason = string.IsNullOrWhiteSpace(banLog.Entry.Reason) ? $"Banned by {banLog.Entry.User.GetFullUsername()}." : banLog.Entry.Reason; var guildUser = guild.GetUser(banLog.Entry.User.Id); await _authorizationService.OnAuthenticatedAsync(guildUser); await _moderationService.CreateInfractionAsync(guild.Id, banLog.Entry.User.Id, InfractionType.Ban, user.Id, reason, null); }
private Task OnUserBannedAsync(ISocketUser user, ISocketGuild guild) { try { MessageDispatcher.Dispatch(new UserBannedNotification(user, guild)); } finally { UpdateGuildPopulationCounter(guild, "user_banned"); } return(Task.CompletedTask); }
/// <summary> /// Creates a ban infraction for the user if they do not already have one. /// </summary> /// <param name="guild">The guild that the user was banned from.</param> /// <param name="user">The user who was banned.</param> /// <returns> /// A <see cref="Task"/> that will complete when the operation completes. /// </returns> private async Task TryCreateBanInfractionAsync(ISocketUser user, ISocketGuild guild) { if (await _moderationService.AnyInfractionsAsync(GetBanSearchCriteria(guild, user))) { return; } var restGuild = await _restClient.GetGuildAsync(guild.Id); var allAudits = await restGuild.GetAuditLogsAsync(10).FlattenAsync(); var banLog = allAudits .Where(x => x.Action == ActionType.Ban && x.Data is BanAuditLogData) .Select(x => (Entry: x, Data: (BanAuditLogData)x.Data)) .FirstOrDefault(x => x.Data.Target.Id == user.Id); // We're in a scenario in where the guild does not have a Discord audit of the // ban MODiX just received, if that's the case something has gone wrong and we // need to investigate. if (banLog.Data is null || banLog.Entry is null) { // Snapshot the most amount of information possible about this event // to log this incident and investigate further var mostRecentAudit = allAudits.OrderByDescending(x => x.CreatedAt).FirstOrDefault(); Log.Error("No ban audit found when handling {message} for user {userId}, in guild {guild} - " + "the most recent audit was created at {mostRecentAuditTime}: {mostRecentAuditAction} for user: {mostRecentAuditUserId}", nameof(UserBannedNotification), user.Id, guild.Id, mostRecentAudit?.CreatedAt, mostRecentAudit?.Action, mostRecentAudit?.User.Id); return; } var reason = string.IsNullOrWhiteSpace(banLog.Entry.Reason) ? $"Banned by {banLog.Entry.User.GetFullUsername()}." : banLog.Entry.Reason; var guildUser = guild.GetUser(banLog.Entry.User.Id); await _authorizationService.OnAuthenticatedAsync(guildUser); await _moderationService.CreateInfractionAsync(guild.Id, banLog.Entry.User.Id, InfractionType.Ban, user.Id, reason, null); }
/// <summary> /// Constructs a new <see cref="UserBannedNotification"/> from the given values. /// </summary> /// <param name="user">The value to use for <see cref="User"/>.</param> /// <param name="guild">The value to use for <see cref="Guild"/>.</param> /// <exception cref="ArgumentNullException">Throws for <paramref name="user"/> and <paramref name="guild"/>.</exception> public UserBannedNotification(ISocketUser user, ISocketGuild guild) { User = user ?? throw new ArgumentNullException(nameof(user)); Guild = guild ?? throw new ArgumentNullException(nameof(guild)); }
private Task OnUserBannedAsync(ISocketUser user, ISocketGuild guild) { MessageDispatcher.Dispatch(new UserBannedNotification(user, guild)); return(Task.CompletedTask); }
public ISocketMessage CreateMessage(ISocketUser author, MessageModel model) { return(_messages.Create(author, model)); }