Пример #1
0
        internal static async Task BotClientMessageCreated(MessageCreateEventArgs e)
        {
            // Make sure fun is allowed and that this isn't a PM or the bot itself.
            if (Program.Settings.FunAllowed &&
                !e.Channel.IsPrivate &&
                !e.Author.IsBot)
            {
                DiscordMember self = await e.Guild.GetMemberAsync(e.Client.CurrentUser.Id);

                // Make sure we actually have the permissions to send messages in this channel before doing anything.
                if (e.Channel.PermissionsFor(self).HasPermission(DSharpPlus.Permissions.SendMessages))
                {
                    bool regexFound = false;

                    ResponseInfo responseInfo = null;

                    // Search through every regex until we find something.
                    for (int i = 0; i < responses.Length && !regexFound; i++)
                    {
                        ResponseInfo a = responses[i];
                        Match        m = a.Regex.Match(e.Message.Content);

                        if (m.Success)
                        {
                            // Only continue if...
                            //  1) This response can trigger.
                            //  2) This response has...
                            //      a) No required state.
                            //      b) A required state but it's already been fulfilled.
                            if (a.CanTrigger() &&                                                                        // Case 1
                                (!a.RequiresBotState || (a.RequiresBotState && botStates.Contains(a.RequiredBotState)))) // Case 2
                            {
                                responseInfo = a;
                                regexFound   = true;
                            } // end if
                        }     // end if
                    }         // end for

                    // Now that we've searched through every regex, let's continue only if we've found a regex match.
                    if (regexFound && !(responseInfo is null))
                    {   // We can, but now let's check if the response has a trigger condition.
                        // So at this point, we know the bot has a fulfilled state (or doesn't have any, period), so let's send the message.
                        await e.Channel.SendMessageAsync(content :
                                                         String.Format(responseInfo.ResponseString,
                                                                       arg0: e.Author.Mention));

                        var dtoNow = DateTimeOffset.Now;

                        // Set the last triggered time to now.
                        responseInfo.LastTrigger = dtoNow;

                        // If the bot has a bot state let's also set it.
                        if (responseInfo.HasBotState)
                        {
                            botStates.Add(responseInfo.BotState);
                        }

                        // If the bot requires a state, let's unset it if we've made it to this point.
                        if (responseInfo.RequiresBotState)
                        {
                            botStates.Remove(responseInfo.RequiredBotState);
                        } // end if
                    }     // end if
                }         // end if
            }             // end if
        }                 // end method