コード例 #1
0
        private async void GlobalEvents_OnChatCommandMessageReceived(object sender, ChatMessageViewModel message)
        {
            try
            {
                if (this.TimeLeft > 0 && this.Winner == null && this.giveawayCommand.DoesMessageMatchTriggers(message, out IEnumerable <string> arguments))
                {
                    CommandParametersModel parameters = new CommandParametersModel(message);
                    parameters.Arguments = new List <string>(arguments);
                    int entries = 1;

                    if (pastWinners.Contains(message.User.ID))
                    {
                        await ChannelSession.Services.Chat.SendMessage("You have already won a giveaway and can not enter this one");

                        return;
                    }

                    if (arguments.Count() > 0 && ChannelSession.Settings.GiveawayMaximumEntries > 1)
                    {
                        if (!int.TryParse(arguments.ElementAt(0), out entries) || entries < 1)
                        {
                            entries = 1;
                        }
                    }

                    int currentEntries = 0;
                    if (this.enteredUsers.ContainsKey(message.User.ID))
                    {
                        currentEntries = this.enteredUsers[message.User.ID].Entries;
                    }

                    if ((entries + currentEntries) > ChannelSession.Settings.GiveawayMaximumEntries)
                    {
                        await ChannelSession.Services.Chat.SendMessage(string.Format("You may only enter {0} time(s), you currently have entered {1} time(s)", ChannelSession.Settings.GiveawayMaximumEntries, currentEntries));

                        return;
                    }

                    Result result = await ChannelSession.Settings.GiveawayRequirementsSet.Validate(parameters);

                    if (result.Success)
                    {
                        foreach (CurrencyRequirementModel requirement in ChannelSession.Settings.GiveawayRequirementsSet.Currency)
                        {
                            int totalAmount = requirement.MinAmount * entries;
                            result = requirement.ValidateAmount(message.User, totalAmount);
                            if (!result.Success)
                            {
                                await requirement.SendErrorChatMessage(message.User, result);

                                return;
                            }
                        }

                        foreach (InventoryRequirementModel requirement in ChannelSession.Settings.GiveawayRequirementsSet.Inventory)
                        {
                            int totalAmount = requirement.Amount * entries;
                            result = requirement.ValidateAmount(message.User, totalAmount);
                            if (!result.Success)
                            {
                                await requirement.SendErrorChatMessage(message.User, result);

                                return;
                            }
                        }

                        await ChannelSession.Settings.GiveawayRequirementsSet.Perform(parameters);

                        // Do additional currency / inventory performs passed on how many additional entries they put in
                        for (int i = 1; i < entries; i++)
                        {
                            foreach (CurrencyRequirementModel requirement in ChannelSession.Settings.GiveawayRequirementsSet.Currency)
                            {
                                await requirement.Perform(parameters);
                            }

                            foreach (InventoryRequirementModel requirement in ChannelSession.Settings.GiveawayRequirementsSet.Inventory)
                            {
                                await requirement.Perform(parameters);
                            }
                        }

                        if (!this.enteredUsers.ContainsKey(message.User.ID))
                        {
                            this.enteredUsers[message.User.ID] = new GiveawayUser()
                            {
                                User = message.User, Entries = 0
                            };
                            this.previousEnteredUsers[message.User.ID] = this.enteredUsers[message.User.ID];
                        }
                        GiveawayUser giveawayUser = this.enteredUsers[message.User.ID];

                        if (giveawayUser != null)
                        {
                            giveawayUser.Entries += entries;

                            Dictionary <string, string> specialIdentifiers = this.GetSpecialIdentifiers();
                            specialIdentifiers["usergiveawayentries"]      = entries.ToString();
                            specialIdentifiers["usergiveawaytotalentries"] = giveawayUser.Entries.ToString();

                            await ChannelSession.Settings.GetCommand(ChannelSession.Settings.GiveawayUserJoinedCommandID).Perform(new CommandParametersModel(message.User, arguments, specialIdentifiers));

                            GlobalEvents.GiveawaysChangedOccurred(usersUpdated: true);
                        }
                    }
                }
                else if (this.Winner != null && this.Winner.Equals(message.User) && message.PlainTextMessage.Equals("!claim", StringComparison.InvariantCultureIgnoreCase))
                {
                    await ChannelSession.Settings.GetCommand(ChannelSession.Settings.GiveawayWinnerSelectedCommandID).Perform(new CommandParametersModel(this.Winner, this.GetSpecialIdentifiers()));

                    await this.End();
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }
コード例 #2
0
        private async void GlobalEvents_OnChatCommandMessageReceived(object sender, ChatMessageViewModel message)
        {
            try
            {
                if (this.TimeLeft > 0 && this.Winner == null && this.giveawayCommand.DoesTextMatchCommand(message.PlainTextMessage, out IEnumerable <string> arguments))
                {
                    int entries = 1;

                    if (pastWinners.Contains(message.User.ID))
                    {
                        await ChannelSession.Services.Chat.SendMessage("You have already won a giveaway and can not enter this one");

                        return;
                    }

                    if (arguments.Count() > 0 && ChannelSession.Settings.GiveawayMaximumEntries > 1)
                    {
                        if (!int.TryParse(arguments.ElementAt(0), out entries) || entries < 1)
                        {
                            entries = 1;
                        }
                    }

                    int currentEntries = 0;
                    if (this.enteredUsers.ContainsKey(message.User.ID))
                    {
                        currentEntries = this.enteredUsers[message.User.ID].Entries;
                    }

                    if ((entries + currentEntries) > ChannelSession.Settings.GiveawayMaximumEntries)
                    {
                        await ChannelSession.Services.Chat.SendMessage(string.Format("You may only enter {0} time(s), you currently have entered {1} time(s)", ChannelSession.Settings.GiveawayMaximumEntries, currentEntries));

                        return;
                    }

                    if (await this.giveawayCommand.CheckAllRequirements(message.User))
                    {
                        if (ChannelSession.Settings.GiveawayRequirements.Currency != null && ChannelSession.Settings.GiveawayRequirements.Currency.GetCurrency() != null)
                        {
                            int totalAmount = ChannelSession.Settings.GiveawayRequirements.Currency.RequiredAmount * entries;
                            if (!ChannelSession.Settings.GiveawayRequirements.TrySubtractCurrencyAmount(message.User, totalAmount, requireAmount: true))
                            {
                                await ChannelSession.Services.Chat.SendMessage(string.Format("You do not have the required {0} {1} to do this", totalAmount, ChannelSession.Settings.GiveawayRequirements.Currency.GetCurrency().Name));

                                return;
                            }
                        }

                        if (ChannelSession.Settings.GiveawayRequirements.Inventory != null)
                        {
                            int totalAmount = ChannelSession.Settings.GiveawayRequirements.Inventory.Amount * entries;
                            if (!ChannelSession.Settings.GiveawayRequirements.TrySubtractInventoryAmount(message.User, totalAmount, requireAmount: true))
                            {
                                await ChannelSession.Services.Chat.SendMessage(string.Format("You do not have the required {0} {1} to do this", totalAmount, ChannelSession.Settings.GiveawayRequirements.Inventory.GetInventory().Name));

                                return;
                            }
                        }

                        if (!this.enteredUsers.ContainsKey(message.User.ID))
                        {
                            this.enteredUsers[message.User.ID] = new GiveawayUser()
                            {
                                User = message.User, Entries = 0
                            };
                        }
                        GiveawayUser giveawayUser = this.enteredUsers[message.User.ID];

                        if (giveawayUser != null)
                        {
                            giveawayUser.Entries += entries;

                            Dictionary <string, string> specialIdentifiers = this.GetSpecialIdentifiers();
                            specialIdentifiers["usergiveawayentries"]      = entries.ToString();
                            specialIdentifiers["usergiveawaytotalentries"] = giveawayUser.Entries.ToString();

                            await ChannelSession.Settings.GiveawayUserJoinedCommand.Perform(message.User, arguments : arguments, extraSpecialIdentifiers : specialIdentifiers);

                            GlobalEvents.GiveawaysChangedOccurred(usersUpdated: true);
                        }
                    }
                }
                else if (this.Winner != null && this.Winner.Equals(message.User) && message.PlainTextMessage.Equals("!claim", StringComparison.InvariantCultureIgnoreCase))
                {
                    await ChannelSession.Settings.GiveawayWinnerSelectedCommand.Perform(this.Winner, extraSpecialIdentifiers : this.GetSpecialIdentifiers());

                    await this.End();
                }
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }
        }