public async Task <WarningPunishment> Warn(IGuild guild, ulong userId, IUser mod, string reason) { var modName = mod.ToString(); if (string.IsNullOrWhiteSpace(reason)) { reason = "-"; } var guildId = guild.Id; var warn = new Warning() { UserId = userId, GuildId = guildId, Forgiven = false, Reason = reason, Moderator = modName, }; int warnings = 1; List <WarningPunishment> ps; using (var uow = _db.GetDbContext()) { ps = uow.GuildConfigs.ForId(guildId, set => set.Include(x => x.WarnPunishments)) .WarnPunishments; warnings += uow.Warnings .ForId(guildId, userId) .Where(w => !w.Forgiven && w.UserId == userId) .Count(); uow.Warnings.Add(warn); uow.SaveChanges(); } var p = ps.FirstOrDefault(x => x.Count == warnings); if (p != null) { var user = await guild.GetUserAsync(userId).ConfigureAwait(false); if (user == null) { return(null); } var muteReason = "Warning punishment - " + reason; switch (p.Punishment) { case PunishmentAction.Mute: if (p.Time == 0) { await _mute.MuteUser(user, mod, reason : muteReason).ConfigureAwait(false); } else { await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(p.Time), reason : muteReason).ConfigureAwait(false); } break; case PunishmentAction.VoiceMute: if (p.Time == 0) { await _mute.MuteUser(user, mod, MuteType.Voice, muteReason).ConfigureAwait(false); } else { await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(p.Time), MuteType.Voice, muteReason).ConfigureAwait(false); } break; case PunishmentAction.ChatMute: if (p.Time == 0) { await _mute.MuteUser(user, mod, MuteType.Chat, muteReason).ConfigureAwait(false); } else { await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(p.Time), MuteType.Chat, muteReason).ConfigureAwait(false); } break; case PunishmentAction.Kick: await user.KickAsync("Warned too many times.").ConfigureAwait(false); break; case PunishmentAction.Ban: if (p.Time == 0) { await guild.AddBanAsync(user, reason : "Warned too many times.").ConfigureAwait(false); } else { await _mute.TimedBan(user, TimeSpan.FromMinutes(p.Time), "Warned too many times.").ConfigureAwait(false); } break; case PunishmentAction.Softban: await guild.AddBanAsync(user, 7, reason : "Softban | Warned too many times").ConfigureAwait(false); try { await guild.RemoveBanAsync(user).ConfigureAwait(false); } catch { await guild.RemoveBanAsync(user).ConfigureAwait(false); } break; case PunishmentAction.RemoveRoles: await user.RemoveRolesAsync(user.GetRoles().Where(x => x.Id != guild.EveryoneRole.Id)).ConfigureAwait(false); break; case PunishmentAction.AddRole: var role = guild.GetRole(p.RoleId.Value); if (!(role is null)) { if (p.Time == 0) { await user.AddRoleAsync(role).ConfigureAwait(false); } else { await _mute.TimedRole(user, TimeSpan.FromMinutes(p.Time), "Warned too many times.", role).ConfigureAwait(false); } } else { _log.Warn($"Warnpunish can't find role {p.RoleId.Value} on server {guild.Id}"); } break;
public async Task <PunishmentAction?> Warn(IGuild guild, ulong userId, string modName, string reason) { if (string.IsNullOrWhiteSpace(reason)) { reason = "-"; } var guildId = guild.Id; var warn = new Warning() { UserId = userId, GuildId = guildId, Forgiven = false, Reason = reason, Moderator = modName, }; int warnings = 1; List <WarningPunishment> ps; using (var uow = _db.UnitOfWork) { ps = uow.GuildConfigs.For(guildId, set => set.Include(x => x.WarnPunishments)) .WarnPunishments; warnings += uow.Warnings .For(guildId, userId) .Where(w => !w.Forgiven && w.UserId == userId) .Count(); uow.Warnings.Add(warn); uow.Complete(); } var p = ps.FirstOrDefault(x => x.Count == warnings); if (p != null) { var user = await guild.GetUserAsync(userId); if (user == null) { return(null); } switch (p.Punishment) { case PunishmentAction.Mute: if (p.Time == 0) { await _mute.MuteUser(user).ConfigureAwait(false); } else { await _mute.TimedMute(user, TimeSpan.FromMinutes(p.Time)).ConfigureAwait(false); } break; case PunishmentAction.Kick: await user.KickAsync("Warned too many times.").ConfigureAwait(false); break; case PunishmentAction.Ban: if (p.Time == 0) { await guild.AddBanAsync(user, reason : "Warned too many times.").ConfigureAwait(false); } else { await _mute.TimedBan(user, TimeSpan.FromMinutes(p.Time), "Warned too many times.").ConfigureAwait(false); } break; case PunishmentAction.Softban: await guild.AddBanAsync(user, 7, reason : "Warned too many times").ConfigureAwait(false); try { await guild.RemoveBanAsync(user).ConfigureAwait(false); } catch { await guild.RemoveBanAsync(user).ConfigureAwait(false); } break; case PunishmentAction.RemoveRoles: await user.RemoveRolesAsync(user.GetRoles().Where(x => x.Id != guild.EveryoneRole.Id)); break; default: break; } return(p.Punishment); } return(null); }
public async Task ApplyPunishment(IGuild guild, IGuildUser user, IUser mod, PunishmentAction p, int minutes, ulong?roleId, string reason) { switch (p) { case PunishmentAction.Mute: if (minutes == 0) { await _mute.MuteUser(user, mod, reason : reason).ConfigureAwait(false); } else { await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), reason : reason) .ConfigureAwait(false); } break; case PunishmentAction.VoiceMute: if (minutes == 0) { await _mute.MuteUser(user, mod, MuteType.Voice, reason).ConfigureAwait(false); } else { await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Voice, reason) .ConfigureAwait(false); } break; case PunishmentAction.ChatMute: if (minutes == 0) { await _mute.MuteUser(user, mod, MuteType.Chat, reason).ConfigureAwait(false); } else { await _mute.TimedMute(user, mod, TimeSpan.FromMinutes(minutes), MuteType.Chat, reason) .ConfigureAwait(false); } break; case PunishmentAction.Kick: await user.KickAsync(reason).ConfigureAwait(false); break; case PunishmentAction.Ban: if (minutes == 0) { await guild.AddBanAsync(user, reason : reason).ConfigureAwait(false); } else { await _mute.TimedBan(user.Guild, user, TimeSpan.FromMinutes(minutes), reason) .ConfigureAwait(false); } break; case PunishmentAction.Softban: await guild.AddBanAsync(user, 7, reason : $"Softban | {reason}").ConfigureAwait(false); try { await guild.RemoveBanAsync(user).ConfigureAwait(false); } catch { await guild.RemoveBanAsync(user).ConfigureAwait(false); } break; case PunishmentAction.RemoveRoles: await user.RemoveRolesAsync(user.GetRoles().Where(x => !x.IsManaged && x != x.Guild.EveryoneRole)) .ConfigureAwait(false); break; case PunishmentAction.AddRole: if (roleId is null) { return; } var role = guild.GetRole(roleId.Value); if (!(role is null)) { if (minutes == 0) { await user.AddRoleAsync(role).ConfigureAwait(false); } else { await _mute.TimedRole(user, TimeSpan.FromMinutes(minutes), reason, role) .ConfigureAwait(false); } } else { Log.Warning($"Can't find role {roleId.Value} on server {guild.Id} to apply punishment."); } break;