Пример #1
0
        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());
        }
Пример #2
0
        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}"
                                      );
                        }
                    }
Пример #3
0
        public static async Task GuildCreateEventHandlerAsync(TheGodfatherBot bot, GuildCreateEventArgs e)
        {
            LogExt.Information(bot.GetId(e.Guild.Id), "Joined {NewGuild}", e.Guild);

            if (bot.Services.GetRequiredService <BlockingService>().IsGuildBlocked(e.Guild.Id))
            {
                LogExt.Information(bot.GetId(e.Guild.Id), "{Guild} is blocked. Leaving...", e.Guild);
                await e.Guild.LeaveAsync();

                return;
            }

            IReadOnlyCollection <DiscordMember> members = await e.Guild.GetAllMembersAsync();

            int botCount = members.Where(m => m.IsBot).Count();

            if (botCount > 25 || (members.Count - botCount < 0))
            {
                LogExt.Information(bot.GetId(e.Guild.Id), "{Guild} is most likely a bot farm. Leaving and blocking...", e.Guild);
                await e.Guild.LeaveAsync();

                await bot.Services.GetRequiredService <BlockingService>().BlockGuildAsync(e.Guild.Id, "Bot farm");

                return;
            }

            await bot.Services.GetRequiredService <GuildConfigService>().RegisterGuildAsync(e.Guild.Id);

            DiscordChannel defChannel = e.Guild.GetDefaultChannel();

            if (!defChannel.PermissionsFor(e.Guild.CurrentMember).HasPermission(Permissions.SendMessages))
            {
                return;
            }

            string prefix = bot.Services.GetRequiredService <BotConfigService>().CurrentConfiguration.Prefix;
            string owners = bot.Client.CurrentApplication.Owners.Select(o => o.ToDiscriminatorString()).Humanize(", ");
            await defChannel.EmbedAsync(
                $"{Formatter.Bold("Thank you for adding me!")}\n\n" +
                $"{Emojis.SmallBlueDiamond} The default prefix for commands is {Formatter.Bold(prefix)}, but it can be changed " +
                $"via {Formatter.Bold("prefix")} command.\n" +
                $"{Emojis.SmallBlueDiamond} I advise you to run the configuration wizard for this guild in order to quickly configure " +
                $"functions like logging, notifications etc. The wizard can be invoked using {Formatter.Bold("config setup")} command.\n" +
                $"{Emojis.SmallBlueDiamond} You can use the {Formatter.Bold("help")} command as a guide, though it is recommended to " +
                $"read the documentation @ https://github.com/ivan-ristovic/the-godfather \n" +
                $"{Emojis.SmallBlueDiamond} If you have any questions or problems, feel free to use the {Formatter.Bold("report")} " +
                $"command in order to send a message to the bot owners ({owners}). Alternatively, you can create an issue on " +
                $"GitHub or join WorldMafia Discord server for quick support (https://worldmafia.net/discord)."
                , Emojis.Wave
                );
        }
Пример #4
0
        public static async Task GuildCreateEventHandlerAsync(TheGodfatherShard shard, GuildCreateEventArgs e)
        {
            shard.Log(LogLevel.Info, $"Joined guild: {e.Guild.ToString()}");

            await RegisterGuildAsync(shard.SharedData, shard.Database, e.Guild.Id);

            DiscordChannel defChannel = e.Guild.GetDefaultChannel();

            if (!defChannel.PermissionsFor(e.Guild.CurrentMember).HasPermission(Permissions.SendMessages))
            {
                return;
            }

            await defChannel.EmbedAsync(
                $"{Formatter.Bold("Thank you for adding me!")}\n\n" +
                $"{StaticDiscordEmoji.SmallBlueDiamond} The default prefix for commands is {Formatter.Bold(shard.SharedData.BotConfiguration.DefaultPrefix)}, but it can be changed using {Formatter.Bold("prefix")} command.\n" +
                $"{StaticDiscordEmoji.SmallBlueDiamond} I advise you to run the configuration wizard for this guild in order to quickly configure functions like logging, notifications etc. The wizard can be invoked using {Formatter.Bold("guild config setup")} command.\n" +
                $"{StaticDiscordEmoji.SmallBlueDiamond} You can use the {Formatter.Bold("help")} command as a guide, though it is recommended to read the documentation @ https://github.com/ivan-ristovic/the-godfather \n" +
                $"{StaticDiscordEmoji.SmallBlueDiamond} If you have any questions or problems, feel free to use the {Formatter.Bold("report")} command in order to send a message to the bot owners ({string.Join(", ", e.Client.CurrentApplication.Owners.Select(o => $"{o.Username}#{o.Discriminator}"))}). Alternatively, you can create an issue on GitHub or join WorldMafia Discord server for quick support (https://discord.me/worldmafia)."
                , StaticDiscordEmoji.Wave
                );
        }
Пример #5
0
        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());
        }