public static async Task MessageTextReactionEventHandlerAsync(TheGodfatherShard shard, MessageCreateEventArgs e) { if (e.Author.IsBot || e.Channel.IsPrivate || string.IsNullOrWhiteSpace(e.Message?.Content)) { return; } if (e.Message.Content.StartsWith(shard.SharedData.GetGuildPrefix(e.Guild.Id))) { return; } if (shard.SharedData.BlockedChannels.Contains(e.Channel.Id) || shard.SharedData.BlockedUsers.Contains(e.Author.Id)) { return; } if (!e.Channel.PermissionsFor(e.Guild.CurrentMember).HasFlag(Permissions.SendMessages)) { return; } if (!shard.SharedData.TextReactions.TryGetValue(e.Guild.Id, out ConcurrentHashSet <TextReaction> treactions)) { return; } TextReaction tr = treactions?.FirstOrDefault(r => r.IsMatch(e.Message.Content)); if (!tr?.IsCooldownActive() ?? false) { await e.Channel.SendMessageAsync(tr.Response.Replace("%user%", e.Author.Mention)); } }
public static async Task GuildDeleteEventHandlerAsync(TheGodfatherShard shard, GuildDeleteEventArgs e) { shard.Log(LogLevel.Info, $"| Left guild: {e.Guild.ToString()}"); shard.SharedData.GuildConfigurations.TryRemove(e.Guild.Id, out _); await shard.DatabaseService.UnregisterGuildAsync(e.Guild.Id); }
public static Task ClientErrorEventHandlerAsync(TheGodfatherShard shard, ClientErrorEventArgs e) { Exception ex = e.Exception; while (ex is AggregateException) { ex = ex.InnerException; } if (ex.InnerException is null) { shard.LogMany(LogLevel.Critical, $"Client errored with exception: {ex.GetType()}", $"Message: {ex.Message}"); } else { shard.LogMany(LogLevel.Critical, $"Client errored with exception: {ex.GetType()}", $"Message: {ex.Message}", $"Inner exception: {ex.InnerException.GetType()}", $"Inner exception message: {ex.InnerException.Message}" ); } return(Task.CompletedTask); }
public static async Task GuildRoleDeleteEventHandlerAsync(TheGodfatherShard shard, GuildRoleDeleteEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Role, "Role deleted", e.Role.ToString()); var entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.RoleDelete); if (entry != null && entry is DiscordAuditLogRoleUpdateEntry rentry) { emb.AddField("User responsible", rentry.UserResponsible.Mention, inline: true); if (!string.IsNullOrWhiteSpace(rentry.Reason)) { emb.AddField("Reason", rentry.Reason); } emb.WithFooter(rentry.CreationTimestamp.ToUtcTimestamp(), rentry.UserResponsible.AvatarUrl); } else { emb.AddField("Error", "Failed to read audit log information. Please check my permissions"); } await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task ChannelPinsUpdateEventHandlerAsync(TheGodfatherShard shard, ChannelPinsUpdateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Channel.Guild); if (logchn == null) { return; } if (await shard.DatabaseService.IsExemptedAsync(e.Channel.Guild.Id, e.Channel.Id, EntityType.Channel)) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Channel, "Channel pins updated", e.Channel.ToString()); emb.AddField("Channel", e.Channel.Mention, inline: true); var pinned = await e.Channel.GetPinnedMessagesAsync(); if (pinned.Any()) { emb.WithDescription(Formatter.MaskedUrl("Jump to top pin", pinned.First().JumpLink)); string content = string.IsNullOrWhiteSpace(pinned.First().Content) ? "<embedded message>" : pinned.First().Content; emb.AddField("Top pin content", Formatter.BlockCode(Formatter.Sanitize(content.Truncate(1000)))); } if (e.LastPinTimestamp != null) { emb.AddField("Last pin timestamp", e.LastPinTimestamp.Value.ToUtcTimestamp(), inline: true); } await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task MessageCreateEventHandlerAsync(TheGodfatherShard shard, MessageCreateEventArgs e) { if (e.Author.IsBot || e.Channel.IsPrivate) { return; } if (shard.SharedData.BlockedChannels.Contains(e.Channel.Id) || shard.SharedData.BlockedUsers.Contains(e.Author.Id)) { return; } if (!e.Channel.PermissionsFor(e.Guild.CurrentMember).HasFlag(Permissions.SendMessages)) { return; } if (!string.IsNullOrWhiteSpace(e.Message?.Content) && !e.Message.Content.StartsWith(shard.SharedData.GetGuildPrefix(e.Guild.Id))) { ushort rank = shard.SharedData.IncrementMessageCountForUser(e.Author.Id); if (rank != 0) { string rankname = await shard.DatabaseService.GetRankAsync(e.Guild.Id, rank); await e.Channel.EmbedAsync($"GG {e.Author.Mention}! You have advanced to level {Formatter.Bold(rank.ToString())} {(string.IsNullOrWhiteSpace(rankname) ? "" : $": {Formatter.Italic(rankname)}")} !", StaticDiscordEmoji.Medal); } } }
public static async Task MessageTextReactionEventHandlerAsync(TheGodfatherShard shard, MessageCreateEventArgs e) { if (e.Author.IsBot || e.Channel.IsPrivate || string.IsNullOrWhiteSpace(e.Message?.Content)) { return; } if (shard.SharedData.BlockedChannels.Contains(e.Channel.Id) || shard.SharedData.BlockedUsers.Contains(e.Author.Id)) { return; } if (!e.Channel.PermissionsFor(e.Guild.CurrentMember).HasFlag(Permissions.SendMessages)) { return; } if (!shard.SharedData.TextReactions.ContainsKey(e.Guild.Id)) { return; } TextReaction tr = shard.SharedData.TextReactions[e.Guild.Id]?.FirstOrDefault(r => r.IsMatch(e.Message.Content)); if (tr != null && !tr.IsCooldownActive()) { await e.Channel.SendMessageAsync(tr.Response.Replace("%user%", e.Author.Mention)); } }
public static async Task GuildUnbanEventHandlerAsync(TheGodfatherShard shard, GuildBanRemoveEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.KickOrBan, "Member unbanned"); var entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.Unban); if (entry == null || !(entry is DiscordAuditLogBanEntry bentry)) { emb.WithDescription(e.Member?.ToString() ?? _unknown); emb.AddField("Error", "Failed to read audit log information. Please check my permissions"); } else { emb.WithDescription(bentry.Target.ToString()); emb.AddField("User responsible", bentry.UserResponsible.Mention, inline: true); if (!string.IsNullOrWhiteSpace(bentry.Reason)) { emb.AddField("Reason", bentry.Reason); } emb.WithFooter(bentry.CreationTimestamp.ToUtcTimestamp(), bentry.UserResponsible.AvatarUrl); } await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task MessageCreateEventHandlerAsync(TheGodfatherShard shard, MessageCreateEventArgs e) { if (e.Author.IsBot || e.Channel.IsPrivate) { return; } if (shard.SharedData.BlockedChannels.Contains(e.Channel.Id) || shard.SharedData.BlockedUsers.Contains(e.Author.Id)) { return; } if (!e.Channel.PermissionsFor(e.Guild.CurrentMember).HasFlag(Permissions.SendMessages)) { return; } if (!string.IsNullOrWhiteSpace(e.Message?.Content) && !e.Message.Content.StartsWith(shard.SharedData.GetGuildPrefix(e.Guild.Id))) { short rank = shard.SharedData.IncrementMessageCountForUser(e.Author.Id); if (rank != 0) { DatabaseGuildRank rankInfo; using (DatabaseContext db = shard.Database.CreateContext()) rankInfo = db.GuildRanks.SingleOrDefault(r => r.GuildId == e.Guild.Id && r.Rank == rank); await e.Channel.EmbedAsync($"GG {e.Author.Mention}! You have advanced to level {Formatter.Bold(rank.ToString())} {(rankInfo is null ? "" : $": {Formatter.Italic(rankInfo.Name)}")} !", StaticDiscordEmoji.Medal); } } }
public RatelimitService(TheGodfatherShard shard) : base(shard) { this.guildSpamInfo = new ConcurrentDictionary <ulong, ConcurrentDictionary <ulong, UserRatelimitInfo> >(); this.refreshTimer = new Timer(RefreshCallback, this, TimeSpan.FromSeconds(20), TimeSpan.FromSeconds(20)); this.reason = "_gf: Ratelimit hit"; }
public static async Task GuildRoleUpdateEventHandlerAsync(TheGodfatherShard shard, GuildRoleUpdateEventArgs e) { if (e.RoleBefore.Position != e.RoleAfter.Position) { return; } DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Role, "Role updated"); var entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.RoleUpdate); if (entry != null && entry is DiscordAuditLogRoleUpdateEntry rentry) { emb.WithDescription(rentry.Target.Id.ToString()); emb.AddField("User responsible", rentry.UserResponsible.Mention, inline: true); if (rentry.NameChange != null) { emb.AddField("Name change", $"{rentry.NameChange.Before ?? _unknown} -> {rentry.NameChange.After ?? _unknown}", inline: true); } if (rentry.ColorChange != null) { emb.AddField("Color changed", $"{rentry.ColorChange.Before?.ToString() ?? _unknown} -> {rentry.ColorChange.After?.ToString() ?? _unknown}", inline: true); } if (rentry.HoistChange != null) { emb.AddField("Hoist", rentry.HoistChange.After?.ToString() ?? _unknown, inline: true); } if (rentry.MentionableChange != null) { emb.AddField("Mentionable", rentry.MentionableChange.After?.ToString() ?? _unknown, inline: true); } if (rentry.PermissionChange != null) { emb.AddField("Permissions changed to", rentry.PermissionChange.After?.ToPermissionString() ?? _unknown, inline: true); } if (rentry.PositionChange != null) { emb.AddField("Position changed to", rentry.PositionChange.After?.ToString() ?? _unknown, inline: true); } if (!string.IsNullOrWhiteSpace(rentry.Reason)) { emb.AddField("Reason", rentry.Reason); } emb.WithFooter(rentry.CreationTimestamp.ToUtcTimestamp(), rentry.UserResponsible.AvatarUrl); } else { emb.AddField("Error", "Failed to read audit log information. Please check my permissions"); emb.AddField("Role", e.RoleBefore?.ToString() ?? _unknown); } await logchn.SendMessageAsync(embed : emb.Build()); }
public AntispamService(TheGodfatherShard shard) : base(shard) { this.guildExempts = new ConcurrentDictionary <ulong, ConcurrentHashSet <ExemptedEntity> >(); this.guildSpamInfo = new ConcurrentDictionary <ulong, ConcurrentDictionary <ulong, UserSpamInfo> >(); this.refreshTimer = new Timer(RefreshCallback, this, TimeSpan.FromMinutes(3), TimeSpan.FromMinutes(3)); this.reason = "_gf: Antispam"; }
public static async Task MessageUpdateEventHandlerAsync(TheGodfatherShard shard, MessageUpdateEventArgs e) { if (e.Author is null || e.Author.IsBot || e.Channel is null || e.Channel.IsPrivate || e.Message is null) { return; } if (shard.SharedData.BlockedChannels.Contains(e.Channel.Id)) { return; } if (e.Message.Author == e.Client.CurrentUser && shard.SharedData.IsEventRunningInChannel(e.Channel.Id)) { return; } if (!(e.Message.Content is null) && shard.SharedData.MessageContainsFilter(e.Guild.Id, e.Message.Content)) { try { await e.Message.DeleteAsync("_gf: Filter hit after update"); await e.Channel.SendMessageAsync($"{e.Author.Mention} said: {FormatterExtensions.Spoiler(Formatter.BlockCode(FormatterExtensions.StripMarkdown(e.Message.Content)))}"); } catch { } } DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn is null || !e.Message.IsEdited || e.Channel.IsExempted(shard)) { return; } DiscordMember member = await e.Guild.GetMemberAsync(e.Author.Id); if (member.IsExempted(shard)) { return; } string pcontent = string.IsNullOrWhiteSpace(e.MessageBefore?.Content) ? "" : e.MessageBefore.Content.Truncate(700); string acontent = string.IsNullOrWhiteSpace(e.Message?.Content) ? "" : e.Message.Content.Truncate(700); string ctime = e.Message.CreationTimestamp == null ? _unknown : e.Message.CreationTimestamp.ToUtcTimestamp(); string etime = e.Message.EditedTimestamp is null ? _unknown : e.Message.EditedTimestamp.Value.ToUtcTimestamp(); string bextra = $"Embeds: {e.MessageBefore?.Embeds?.Count ?? 0}, Reactions: {e.MessageBefore?.Reactions?.Count ?? 0}, Attachments: {e.MessageBefore?.Attachments?.Count ?? 0}"; string aextra = $"Embeds: {e.Message.Embeds.Count}, Reactions: {e.Message.Reactions.Count}, Attachments: {e.Message.Attachments.Count}"; DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Message, "Message updated"); emb.WithDescription(Formatter.MaskedUrl("Jump to message", e.Message.JumpLink)); emb.AddField("Location", e.Channel.Mention, inline: true); emb.AddField("Author", e.Message.Author?.Mention ?? _unknown, inline: true); emb.AddField("Before update", $"Created {ctime}\n{bextra}\nContent:{Formatter.BlockCode(FormatterExtensions.StripMarkdown(pcontent))}"); emb.AddField("After update", $"Edited {etime}\n{aextra}\nContent:{Formatter.BlockCode(FormatterExtensions.StripMarkdown(acontent))}"); await logchn.SendMessageAsync(embed : emb.Build()); }
public Task DeleteAsync(CommandContext ctx, [RemainingText, Description("Command to remove.")] string command) { Command cmd = ctx.CommandsNext.FindCommand(command, out _); ctx.CommandsNext.UnregisterCommands(cmd); TheGodfatherShard.UpdateCommandList(ctx.CommandsNext); return(this.InformAsync(ctx, $"Removed command {Formatter.Bold(cmd.QualifiedName)}.", important: false)); }
public static Task CommandExecutionEventHandler(TheGodfatherShard shard, CommandExecutionEventArgs e) { shard.Log(LogLevel.Info, $"| Executed: {e.Command?.QualifiedName ?? "<unknown command>"}\n" + $"| {e.Context.User.ToString()}\n" + $"| {e.Context.Guild.ToString()}; {e.Context.Channel.ToString()}" ); return(Task.CompletedTask); }
public static async Task MemberUpdateEventHandlerAsync(TheGodfatherShard shard, GuildMemberUpdateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Member, "Member updated", e.Member.ToString()); emb.WithThumbnailUrl(e.Member.AvatarUrl); DiscordAuditLogEntry entry = null; if (e.RolesBefore.Count == e.RolesAfter.Count) { entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.MemberUpdate); } else { entry = await e.Guild.GetFirstAuditLogEntryAsync(AuditLogActionType.MemberRoleUpdate); } if (entry != null && entry is DiscordAuditLogMemberUpdateEntry mentry) { emb.AddField("User responsible", mentry.UserResponsible.Mention, inline: true); if (mentry.NicknameChange != null) { emb.AddField("Nickname change", $"{mentry.NicknameChange.Before} -> {mentry.NicknameChange.After}", inline: true); } if (mentry.AddedRoles != null && mentry.AddedRoles.Any()) { emb.AddField("Added roles", string.Join(",", mentry.AddedRoles.Select(r => r.Name)), inline: true); } if (mentry.RemovedRoles != null && mentry.RemovedRoles.Any()) { emb.AddField("Removed roles", string.Join(",", mentry.RemovedRoles.Select(r => r.Name)), inline: true); } if (!string.IsNullOrWhiteSpace(mentry.Reason)) { emb.AddField("Reason", mentry.Reason); } emb.WithFooter(mentry.CreationTimestamp.ToUtcTimestamp(), mentry.UserResponsible.AvatarUrl); } else { emb.AddField("Error", "Failed to read audit log information. Please check my permissions"); emb.AddField("Name before", e.NicknameBefore ?? _unknown, inline: true); emb.AddField("Name after", e.NicknameAfter ?? _unknown, inline: true); emb.AddField("Roles before", e.RolesBefore?.Count.ToString() ?? _unknown, inline: true); emb.AddField("Roles after", e.RolesAfter?.Count.ToString() ?? _unknown, inline: true); } await logchn.SendMessageAsync(embed : emb.Build()); }
public static Task GuildAvailableEventHandlerAsync(TheGodfatherShard shard, GuildCreateEventArgs e) { shard.Log(LogLevel.Debug, $"Guild available: {e.Guild.ToString()}"); if (shard.SharedData.GuildConfigurations.ContainsKey(e.Guild.Id)) { return(Task.CompletedTask); } return(RegisterGuildAsync(shard.SharedData, shard.Database, e.Guild.Id)); }
public static Task GuildAvailableEventHandlerAsync(TheGodfatherShard shard, GuildCreateEventArgs e) { shard.Log(LogLevel.Info, $"| Guild available: {e.Guild.ToString()}"); if (shard.SharedData.GuildConfigurations.ContainsKey(e.Guild.Id)) { return(Task.CompletedTask); } shard.SharedData.GuildConfigurations.TryAdd(e.Guild.Id, CachedGuildConfig.Default); return(shard.DatabaseService.RegisterGuildAsync(e.Guild.Id)); }
public static async Task BulkDeleteEventHandlerAsync(TheGodfatherShard shard, MessageBulkDeleteEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Channel.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Message, $"Bulk message deletion occured ({e.Messages.Count} total)", $"In channel {e.Channel.Mention}"); await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task GuildUpdateEventHandlerAsync(TheGodfatherShard shard, GuildUpdateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.GuildAfter); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Role, "Guild settings updated"); var entry = await e.GuildAfter.GetFirstAuditLogEntryAsync(AuditLogActionType.GuildUpdate); if (entry != null && entry is DiscordAuditLogGuildEntry gentry) { emb.AddField("User responsible", gentry.UserResponsible.Mention, inline: true); if (gentry.NameChange != null) { emb.AddField("Name change", $"{gentry.NameChange.Before ?? _unknown} -> {gentry.NameChange.After ?? _unknown}", inline: true); } if (gentry.AfkChannelChange != null) { emb.AddField("AFK channel changed to", gentry.AfkChannelChange.After?.ToString() ?? _unknown, inline: true); } if (gentry.EmbedChannelChange != null) { emb.AddField("Embed channel changed to", gentry.EmbedChannelChange.After?.ToString() ?? _unknown, inline: true); } if (gentry.IconChange != null) { emb.AddField("Icon changed to", gentry.IconChange.After ?? _unknown, inline: true); } if (gentry.NotificationSettingsChange != null) { emb.AddField("Notifications changed to", gentry.NotificationSettingsChange.After.HasFlag(DefaultMessageNotifications.AllMessages) ? "All messages" : "Mentions only", inline: true); } if (gentry.OwnerChange != null) { emb.AddField("Owner changed to", gentry.OwnerChange.After?.ToString() ?? _unknown, inline: true); } if (!string.IsNullOrWhiteSpace(gentry.Reason)) { emb.AddField("Reason", gentry.Reason); } emb.WithFooter(gentry.CreationTimestamp.ToUtcTimestamp(), gentry.UserResponsible.AvatarUrl); } else { emb.AddField("Error", "Failed to read audit log information. Please check my permissions"); } await logchn.SendMessageAsync(embed : emb.Build()); }
public static bool IsExempted(this DiscordChannel channel, TheGodfatherShard shard) { using (DatabaseContext db = shard.Database.CreateContext()) { if (db.LoggingExempts.Any(ee => ee.GuildId == channel.GuildId && ee.Type == ExemptedEntityType.Channel && (ee.Id == channel.Id || ee.Id == channel.Parent.Id))) { return(true); } } return(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 static async Task WebhooksUpdateEventHandlerAsync(TheGodfatherShard shard, WebhooksUpdateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Guild, "Webhooks updated", $"For {e.Channel.ToString()}"); await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task GuildIntegrationsUpdateEventHandlerAsync(TheGodfatherShard shard, GuildIntegrationsUpdateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Guild, "Guild integrations updated"); await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task MemberJoinEventHandlerAsync(TheGodfatherShard shard, GuildMemberAddEventArgs e) { DatabaseGuildConfig gcfg = e.Guild.GetGuildSettings(shard.Database); await Task.Delay(TimeSpan.FromSeconds(gcfg.AntiInstantLeaveSettings.Cooldown + 1)); if (e.Member.Guild is null) { return; } DiscordChannel wchn = e.Guild.GetChannel(gcfg.WelcomeChannelId); if (!(wchn is null)) { if (string.IsNullOrWhiteSpace(gcfg.WelcomeMessage)) { await wchn.EmbedAsync($"Welcome to {Formatter.Bold(e.Guild.Name)}, {e.Member.Mention}!", StaticDiscordEmoji.Wave); } else { await wchn.EmbedAsync(gcfg.WelcomeMessage.Replace("%user%", e.Member.Mention), StaticDiscordEmoji.Wave); } } try { using (DatabaseContext db = shard.Database.CreateContext()) { IQueryable <ulong> rids = db.AutoAssignableRoles .Where(dbr => dbr.GuildId == e.Guild.Id) .Select(dbr => dbr.RoleId); foreach (ulong rid in rids.ToList()) { try { DiscordRole role = e.Guild.GetRole(rid); if (!(role is null)) { await e.Member.GrantRoleAsync(role); } else { db.AutoAssignableRoles.Remove(db.AutoAssignableRoles.Single(r => r.GuildId == e.Guild.Id && r.RoleId == 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}" ); } }
public static async Task MessageFilterEventHandlerAsync(TheGodfatherShard shard, MessageCreateEventArgs e) { if (e.Author.IsBot || e.Channel.IsPrivate || string.IsNullOrWhiteSpace(e.Message?.Content)) { return; } if (shard.SharedData.BlockedChannels.Contains(e.Channel.Id)) { return; } CachedGuildConfig gcfg = shard.SharedData.GetGuildConfig(e.Guild.Id); if (gcfg.LinkfilterSettings.Enabled) { if (await shard.CNext.Services.GetService <LinkfilterService>().HandleNewMessageAsync(e, gcfg.LinkfilterSettings)) { return; } } if (!shard.SharedData.MessageContainsFilter(e.Guild.Id, e.Message.Content)) { return; } if (!e.Channel.PermissionsFor(e.Guild.CurrentMember).HasFlag(Permissions.ManageMessages)) { return; } await e.Message.DeleteAsync("_gf: Filter hit"); DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Message, $"Filter triggered"); emb.AddField("User responsible", e.Message.Author.Mention); emb.AddField("Channel", e.Channel.Mention); emb.AddField("Content", Formatter.BlockCode(Formatter.Sanitize(e.Message.Content.Truncate(1020)))); await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task VoiceServerUpdateEventHandlerAsync(TheGodfatherShard shard, VoiceServerUpdateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn == null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Guild, "Voice server updated"); emb.AddField("Endpoint", Formatter.Bold(e.Endpoint)); await logchn.SendMessageAsync(embed : emb.Build()); }
public static async Task ChannelCreateEventHandlerAsync(TheGodfatherShard shard, ChannelCreateEventArgs e) { DiscordChannel logchn = shard.SharedData.GetLogChannelForGuild(shard.Client, e.Guild); if (logchn is null) { return; } DiscordEmbedBuilder emb = FormEmbedBuilder(EventOrigin.Channel, "Channel created", e.Channel.ToString()); DiscordAuditLogEntry entry = await e.Guild.GetLatestAuditLogEntryAsync(AuditLogActionType.ChannelCreate); if (entry is null || !(entry is DiscordAuditLogChannelEntry centry)) { emb.AddField("Error", "Failed to read audit log information. Please check my permissions"); }
public static void RegisterEventListeners(IDiscordClient client, TheGodfatherShard shard) { ListenerMethods = from types in Assembly.GetExecutingAssembly().GetTypes() from methods in types.GetMethods() let attribute = methods.GetCustomAttribute(typeof(AsyncEventListenerAttribute), inherit: true) where attribute != null select new ListenerMethod { Method = methods, Attribute = attribute as AsyncEventListenerAttribute }; foreach (ListenerMethod lm in ListenerMethods) { lm.Attribute.Register(shard, client, lm.Method); } }
public static Task ClientErrorEventHandlerAsync(TheGodfatherShard shard, ClientErrorEventArgs e) { var ex = e.Exception; while (ex is AggregateException) { ex = ex.InnerException; } shard.Log( LogLevel.Critical, $"| Client errored with exception: {ex.GetType()}\n" + $"| Message: {ex.Message}" + (ex.InnerException != null ? $"| Inner exception: {ex.InnerException.GetType()}\n| Inner exception message: {ex.InnerException.Message}" : "") ); return(Task.CompletedTask); }