示例#1
0
        /// <summary>
        /// Main method where actual guild processing occurs.
        /// </summary>
        private async Task<PGDiagnostic> ProcessGuildAsync(SocketGuild guild)
        {
            var diag = new PGDiagnostic();

            // Load guild information - stop if there is none (bot never previously used in guild)
            var gc = await GuildConfiguration.LoadAsync(guild.Id, true).ConfigureAwait(false);
            if (gc == null) return diag;

            // Check if role settings are correct before continuing with further processing
            SocketRole role = null;
            if (gc.RoleId.HasValue) role = guild.GetRole(gc.RoleId.Value);
            diag.RoleCheck = CheckCorrectRoleSettings(guild, role);
            if (diag.RoleCheck != null) return diag;

            // Determine who's currently having a birthday
            var users = await GuildUserConfiguration.LoadAllAsync(guild.Id).ConfigureAwait(false);
            var tz = gc.TimeZone;
            var birthdays = GetGuildCurrentBirthdays(users, tz);
            // Note: Don't quit here if zero people are having birthdays. Roles may still need to be removed by BirthdayApply.
            diag.CurrentBirthdays = birthdays.Count.ToString();

            IEnumerable<SocketGuildUser> announcementList;
            // Update roles as appropriate
            try
            {
                var updateResult = await UpdateGuildBirthdayRoles(guild, role, birthdays).ConfigureAwait(false);
                announcementList = updateResult.Item1;
                diag.RoleApplyResult = updateResult.Item2; // statistics
            }
            catch (Discord.Net.HttpException ex)
            {
                diag.RoleApply = ex.Message;
                return diag;
            }
            diag.RoleApply = null;

            // Birthday announcement
            var announce = gc.AnnounceMessages;
            var announceping = gc.AnnouncePing;
            SocketTextChannel channel = null;
            if (gc.AnnounceChannelId.HasValue) channel = guild.GetTextChannel(gc.AnnounceChannelId.Value);
            if (announcementList.Count() != 0)
            {
                var announceResult =
                    await AnnounceBirthdaysAsync(announce, announceping, channel, announcementList).ConfigureAwait(false);
                diag.Announcement = announceResult;
            }
            else
            {
                diag.Announcement = "No new role additions. Announcement not needed.";
            }

            return diag;
        }
示例#2
0
        /// <summary>
        /// Determines if the incoming message is an incoming command, and dispatches to the appropriate handler if necessary.
        /// </summary>
        private async Task Client_MessageReceived(SocketMessage msg)
        {
            if (!(msg.Channel is SocketTextChannel channel))
            {
                return;
            }
            if (msg.Author.IsBot || msg.Author.IsWebhook)
            {
                return;
            }
            if (((IMessage)msg).Type != MessageType.Default)
            {
                return;
            }
            var author = (SocketGuildUser)msg.Author;

            // Limit 3:
            // For all cases: base command, 2 parameters.
            // Except this case: "bb.config", subcommand name, subcommand parameters in a single string
            var csplit = msg.Content.Split(" ", 3, StringSplitOptions.RemoveEmptyEntries);

            if (csplit.Length > 0 && csplit[0].StartsWith(CommandPrefix, StringComparison.OrdinalIgnoreCase))
            {
                // Determine if it's something we're listening for.
                if (!_dispatchCommands.TryGetValue(csplit[0].Substring(CommandPrefix.Length), out CommandHandler command))
                {
                    return;
                }

                // Load guild information here
                var gconf = await GuildConfiguration.LoadAsync(channel.Guild.Id, false);

                // Ban check
                if (!gconf.IsBotModerator(author)) // skip check if user is a moderator
                {
                    if (await gconf.IsUserBlockedAsync(author.Id))
                    {
                        return;                                            // silently ignore
                    }
                }

                // Execute the command
                try
                {
                    Log("Command", $"{channel.Guild.Name}/{author.Username}#{author.Discriminator}: {msg.Content}");
                    await command(this, gconf, csplit, channel, author);
                }
                catch (Exception ex)
                {
                    if (ex is HttpException)
                    {
                        return;
                    }
                    Log("Command", ex.ToString());
                    try
                    {
                        channel.SendMessageAsync(":x: An unknown error occurred. It has been reported to the bot owner.").Wait();
                        // TODO webhook report
                    }
                    catch (HttpException) { } // Fail silently
                }
            }
        }