public async Task ExecuteGroupAsync(CommandContext ctx, [Description("desc-enable")] bool enable, [Description("desc-sens")] short cooldown) { if (cooldown is < AntiInstantLeaveSettings.MinCooldown or > AntiInstantLeaveSettings.MaxCooldown) { throw new CommandFailedException(ctx, "cmd-err-range-cd", AntiInstantLeaveSettings.MinCooldown, AntiInstantLeaveSettings.MaxCooldown); } var settings = new AntiInstantLeaveSettings { Enabled = enable, Cooldown = cooldown }; await ctx.Services.GetRequiredService <GuildConfigService>().ModifyConfigAsync(ctx.Guild.Id, gcfg => gcfg.AntiInstantLeaveSettings = settings); await ctx.GuildLogAsync(emb => { emb.WithLocalizedTitle("evt-cfg-upd"); emb.WithColor(this.ModuleColor); if (enable) { emb.WithLocalizedDescription("evt-il-enabled"); emb.AddLocalizedTitleField("str-cooldown", settings.Cooldown, inline: true); } else { emb.WithLocalizedDescription("evt-il-disabled"); } }); await ctx.InfoAsync(this.ModuleColor, enable? "evt-il-enabled" : "evt-il-disabled"); }
public async Task SetSensitivityAsync(CommandContext ctx, [Description("Sensitivity.")] short sensitivity) { if (sensitivity < 2 || sensitivity > 60) { throw new CommandFailedException("The sensitivity is not in the valid range ([2, 60])."); } AntiInstantLeaveSettings settings = await this.Database.GetAntiInstantLeaveSettingsAsync(ctx.Guild.Id); settings.Sensitivity = sensitivity; DiscordChannel logchn = this.Shared.GetLogChannelForGuild((DiscordClientImpl)ctx.Client, ctx.Guild); if (logchn != null) { var emb = new DiscordEmbedBuilder() { Title = "Guild config changed", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); emb.AddField("Instant leave sensitivity changed to", $"{settings.Sensitivity}s"); await logchn.SendMessageAsync(embed : emb.Build()); } await this.InformAsync(ctx, $"Instant leave sensitivity for this guild has been changed to {Formatter.Bold(settings.Sensitivity.ToString())}s", important : false); }
public async Task ExecuteGroupAsync(CommandContext ctx, [Description("Enable?")] bool enable, [Description("Sensitivity (join-leave max seconds).")] short sensitivity) { if (sensitivity < 2 || sensitivity > 60) { throw new CommandFailedException("The sensitivity is not in the valid range ([2, 60])."); } AntiInstantLeaveSettings settings = await this.Database.GetAntiInstantLeaveSettingsAsync(ctx.Guild.Id); settings.Enabled = enable; settings.Sensitivity = sensitivity; DiscordChannel logchn = this.Shared.GetLogChannelForGuild((DiscordClientImpl)ctx.Client, ctx.Guild); if (logchn != null) { var emb = new DiscordEmbedBuilder() { Title = "Guild config changed", Description = $"AntiInstantLeave {(enable ? "enabled" : "disabled")}", Color = this.ModuleColor }; emb.AddField("User responsible", ctx.User.Mention, inline: true); emb.AddField("Invoked in", ctx.Channel.Mention, inline: true); if (enable) { emb.AddField("Instant leave sensitivity", settings.Sensitivity.ToString(), inline: true); } await logchn.SendMessageAsync(embed : emb.Build()); } await this.InformAsync(ctx, $"{Formatter.Bold(enable ? "Enabled" : "Disabled")} instant leave actions.", important : false); }
public static async Task MemberRemoveEventHandlerAsync(TheGodfatherShard shard, GuildMemberRemoveEventArgs e) { if (e.Member.IsCurrent) { return; } bool punished = false; AntiInstantLeaveSettings antiILSettings = await shard.DatabaseService.GetAntiInstantLeaveSettingsAsync(e.Guild.Id); if (antiILSettings.Enabled) { punished = await shard.CNext.Services.GetService <AntiInstantLeaveService>().HandleMemberLeaveAsync(e, antiILSettings); } if (!punished) { DiscordChannel lchn = await shard.DatabaseService.GetLeaveChannelAsync(e.Guild); if (lchn != null) { string msg = await shard.DatabaseService.GetLeaveMessageAsync(e.Guild.Id); if (string.IsNullOrWhiteSpace(msg)) { await lchn.EmbedAsync($"{Formatter.Bold(e.Member?.Username ?? _unknown)} left the server! Bye!", StaticDiscordEmoji.Wave); } else { await lchn.EmbedAsync(msg.Replace("%user%", e.Member?.Username ?? _unknown), StaticDiscordEmoji.Wave); } } } DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Member, "Member left", e.Member.ToString()); emb.WithThumbnailUrl(e.Member.AvatarUrl); emb.AddField("Registration time", e.Member.CreationTimestamp.ToUtcTimestamp(), inline: true); if (!string.IsNullOrWhiteSpace(e.Member.Email)) { emb.AddField("Email", e.Member.Email); } await logchn.SendMessageAsync(embed : emb.Build()); }
public async Task ExecuteGroupAsync(CommandContext ctx) { AntiInstantLeaveSettings settings = (await this.GetGuildConfigAsync(ctx.Guild.Id)).AntiInstantLeaveSettings; if (settings.Enabled) { await this.InformAsync(ctx, $"Instant leave watch: {Formatter.Bold("enabled")} with {Formatter.Bold(settings.Cooldown.ToString())}s cooldown"); } else { await this.InformAsync(ctx, $"Instant leave watch: {Formatter.Bold("disabled")}"); } }
public async Task ResetAsync(CommandContext ctx) { await ctx.WithGuildConfigAsync(gcfg => { return(!gcfg.AntiInstantLeaveEnabled ? throw new CommandFailedException(ctx, "cmd-err-reset-il-off") : Task.CompletedTask); }); if (!await ctx.WaitForBoolReplyAsync("q-setup-reset")) { return; } var settings = new AntiInstantLeaveSettings(); await this.ExecuteGroupAsync(ctx, true, settings.Cooldown); }
public async Task HandleMemberJoinAsync(GuildMemberAddEventArgs e, AntiInstantLeaveSettings settings) { if (!this.guildNewMembers.ContainsKey(e.Guild.Id) && !this.TryAddGuildToWatch(e.Guild.Id)) { throw new ConcurrentOperationException("Failed to add guild to instant-leave watch list!"); } if (!this.guildNewMembers[e.Guild.Id].Add(e.Member)) { throw new ConcurrentOperationException("Failed to add member to instant-leave watch list!"); } await Task.Delay(TimeSpan.FromSeconds(settings.Cooldown)); if (this.guildNewMembers.ContainsKey(e.Guild.Id) && !this.guildNewMembers[e.Guild.Id].TryRemove(e.Member)) { throw new ConcurrentOperationException("Failed to remove member from instant-leave watch list!"); } }
public static async Task <AntiInstantLeaveSettings> GetAntiInstantLeaveSettingsAsync(this DBService db, ulong gid) { var settings = new AntiInstantLeaveSettings(); await db.ExecuteCommandAsync(async (cmd) => { cmd.CommandText = $"SELECT antijoinleave_enabled, antijoinleave_sens FROM gf.guild_cfg WHERE gid = @gid LIMIT 1;"; cmd.Parameters.Add(new NpgsqlParameter <long>("gid", (long)gid)); using (var reader = await cmd.ExecuteReaderAsync().ConfigureAwait(false)) { if (await reader.ReadAsync().ConfigureAwait(false)) { settings.Enabled = (bool)reader["antijoinleave_enabled"]; settings.Sensitivity = (short)reader["antijoinleave_sens"]; } } }); return(settings); }
public static async Task MemberJoinProtectionEventHandlerAsync(TheGodfatherShard shard, GuildMemberAddEventArgs e) { if (e.Member == null || e.Member.IsBot) { return; } AntifloodSettings antifloodSettings = await shard.DatabaseService.GetAntifloodSettingsAsync(e.Guild.Id); if (antifloodSettings.Enabled) { await shard.CNext.Services.GetService <AntifloodService>().HandleMemberJoinAsync(e, antifloodSettings); } AntiInstantLeaveSettings antiILSettings = await shard.DatabaseService.GetAntiInstantLeaveSettingsAsync(e.Guild.Id); if (antiILSettings.Enabled) { await shard.CNext.Services.GetService <AntiInstantLeaveService>().HandleMemberJoinAsync(e, antiILSettings); } }
public async Task <bool> HandleMemberLeaveAsync(GuildMemberRemoveEventArgs e, AntiInstantLeaveSettings settings) { if (!this.guildNewMembers.ContainsKey(e.Guild.Id) || !this.guildNewMembers[e.Guild.Id].Contains(e.Member)) { return(false); } await this.PunishMemberAsync(e.Guild, e.Member, PunishmentActionType.PermanentBan); return(true); }
public static Task SetAntiInstantLeaveSettingsAsync(this DBService db, ulong gid, AntiInstantLeaveSettings settings) { return(db.ExecuteCommandAsync(cmd => { cmd.CommandText = "UPDATE gf.guild_cfg SET (antijoinleave_enabled, antijoinleave_sens) = " + "(antijoinleave_enabled, antijoinleave_sens) WHERE gid = @gid;"; cmd.Parameters.Add(new NpgsqlParameter <long>("gid", (long)gid)); cmd.Parameters.Add(new NpgsqlParameter <bool>("antijoinleave_enabled", settings.Enabled)); cmd.Parameters.Add(new NpgsqlParameter <short>("antijoinleave_sens", settings.Sensitivity)); return cmd.ExecuteNonQueryAsync(); })); }
public static async Task MemberJoinEventHandlerAsync(TheGodfatherShard shard, GuildMemberAddEventArgs e) { AntiInstantLeaveSettings antiILSettings = await shard.DatabaseService.GetAntiInstantLeaveSettingsAsync(e.Guild.Id); await Task.Delay(TimeSpan.FromSeconds(antiILSettings.Sensitivity + 1)); if (e.Member.Guild == null) { return; } DiscordChannel wchn = await shard.DatabaseService.GetWelcomeChannelAsync(e.Guild); if (wchn != null) { string msg = await shard.DatabaseService.GetWelcomeMessageAsync(e.Guild.Id); if (string.IsNullOrWhiteSpace(msg)) { await wchn.EmbedAsync($"Welcome to {Formatter.Bold(e.Guild.Name)}, {e.Member.Mention}!", StaticDiscordEmoji.Wave); } else { await wchn.EmbedAsync(msg.Replace("%user%", e.Member.Mention), StaticDiscordEmoji.Wave); } } try { IReadOnlyList <ulong> rids = await shard.DatabaseService.GetAutomaticRolesForGuildAsync(e.Guild.Id) .ConfigureAwait(false); foreach (ulong rid in rids) { try { DiscordRole role = e.Guild.GetRole(rid); if (role != null) { await e.Member.GrantRoleAsync(role); } else { await shard.DatabaseService.RemoveAutomaticRoleAsync(e.Guild.Id, rid); } } catch (Exception exc) { shard.Log(LogLevel.Debug, $"| Failed to assign an automatic role to a new member!\n" + $"| {e.Guild.ToString()}\n" + $"| Exception: {exc.GetType()}\n" + $"| Message: {exc.Message}" ); } } } catch (Exception exc) { shard.SharedData.LogProvider.LogException(LogLevel.Debug, exc); } DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Member, "Member joined", e.Member.ToString()); emb.WithThumbnailUrl(e.Member.AvatarUrl); emb.AddField("Registration time", e.Member.CreationTimestamp.ToUtcTimestamp(), inline: true); if (!string.IsNullOrWhiteSpace(e.Member.Email)) { emb.AddField("Email", e.Member.Email); } await logchn.SendMessageAsync(embed : emb.Build()); }
public async Task ExecuteGroupAsync(CommandContext ctx) { AntiInstantLeaveSettings settings = await this.Database.GetAntiInstantLeaveSettingsAsync(ctx.Guild.Id); await this.InformAsync(ctx, $"Instant leave watch for this guild is {Formatter.Bold(settings.Enabled ? "enabled" : "disabled")} with senssitivity: {Formatter.Bold(settings.Sensitivity.ToString())}"); }