Ejemplo n.º 1
0
        private void AddArtifact(ref Dictionary <string, UserStorage> storage, string userID, Artifact newArtifact)
        {
            // Get the user's personal storage
            if (!storage.TryGetValue(userID, out UserStorage personalStorage))
            {
                Console.WriteLine($"Could not find a spice account for user ID: {userID}, failed to add artifact to storage");
                return;
            }

            // Add the new artifact and save to file
            personalStorage.artifacts.Add(newArtifact);
            storage[userID] = personalStorage;
            SaveSpiceStorage(storage);
        }
Ejemplo n.º 2
0
        public Bot()
        {
            // Get connection info from config.json and initialize channels
            configInfo   = ConfigInfo.LoadInfo(configDataPath);
            chatCommands = ChatCommand.LoadFromJson(commandDataPath);
            ConnectionCredentials credentials = new ConnectionCredentials(configInfo.identity.username, configInfo.identity.password);
            var clientOptions = new ClientOptions
            {
                MessagesAllowedInPeriod = 750,
                ThrottlingPeriod        = TimeSpan.FromSeconds(30)
            };
            WebSocketClient customClient = new WebSocketClient(clientOptions);

            botClient = new TwitchClient(customClient);
            foreach (string channel in configInfo.channels)
            {
                botClient.Initialize(credentials, channel);
            }

            // Client setup
            botClient.OnLog                 += OnLog;
            botClient.OnJoinedChannel       += OnJoinedChannel;
            botClient.OnMessageReceived     += OnMessageReceived;
            botClient.OnWhisperReceived     += OnWhisperReceived;
            botClient.OnNewSubscriber       += OnNewSubscriber;
            botClient.OnConnected           += OnConnected;
            botClient.OnChatCommandReceived += OnChatCommandReceived;

            botClient.Connect();

            // PubSub setup
            botPubSub = new TwitchPubSub();

            botPubSub.OnPubSubServiceConnected += OnPubSubServiceConnected;
            botPubSub.OnListenResponse         += OnListenResponse;
            botPubSub.OnStreamUp       += OnStreamUp;
            botPubSub.OnStreamDown     += OnStreamDown;
            botPubSub.OnRewardRedeemed += OnRewardRedeemed;

            // Diadonic's channel name/ID is just hard coded cause it's public anyways
            botPubSub.ListenToVideoPlayback("Diadonic");
            botPubSub.ListenToRewards("24384880");

            botPubSub.Connect();

            // API setup
            botAPI = new TwitchAPI();
            botAPI.Settings.ClientId    = configInfo.clientID;
            botAPI.Settings.AccessToken = configInfo.accessToken;

            // Load user storage and artifact history state
            userStorage  = LoadStorage();
            curArtifacts = Artifact.GenerateArticats(out prevArtifacts);
            if (userStorage == null)
            {
                userStorage = new Dictionary <string, UserStorage>();
            }

            // Set up timer callback
            ArtifactCooldownTimer.Elapsed  += ResetArtifactCooldown;
            ArtifactCooldownTimer.AutoReset = false;
        }
Ejemplo n.º 3
0
        /*
         * Central function to parse through an handle all artifact chat commands
         * Needs cleanup/modularization but I can't be bothered to do that right now
         */
        private async void HandleArtifactChatCommands(OnChatCommandReceivedArgs e)
        {
            if (e.Command.ArgumentsAsList.Count == 0) // No arguments just sends a list of current artifacts for sale
            {
                if (ArtifactDisplayCooldown)
                {
                    return;
                }                                        // Ignore this command while on cooldown

                foreach (Artifact art in curArtifacts)
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, Artifact.ToChat(art));
                }

                // Have this on an arbitrary 30 second cooldown to avoid chat spam
                ArtifactDisplayCooldown = true;
                ArtifactCooldownTimer.Start();
            }

            if (e.Command.ArgumentsAsList.Count == 1 && e.Command.ArgumentsAsList[0].ToLower() == "help") // !artifacts help
            {
                botClient.SendMessage(e.Command.ChatMessage.Channel, ArtifactHelp);
            }

            if (e.Command.ArgumentsAsList.Count == 2 && e.Command.ArgumentsAsList[0].ToLower() == "buy") // !artifacts buy <ID>
            {
                // Check if the ID they are trying to buy is valid
                bool isValid = false;
                int  index   = 0;
                for (int i = 0; i < curArtifacts.Count; i++)
                {
                    if (!Int32.TryParse(e.Command.ArgumentsAsList[1], out int artID))
                    {
                        break;
                    }                                                                            // if the input ID isn't even an int don't try to find it
                    if (curArtifacts[i].ID == artID)
                    {
                        // If the ID is a valid int and matches an artifact for sale set the valid flag and stop searching
                        isValid = true;
                        index   = i;
                        break;
                    }
                }

                // Response for invalid ID
                if (!isValid)
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, string.Format(ArtifactInvalidPurchase, e.Command.ArgumentsAsList[1]));
                    return;
                }

                // Check first if the user even has a spice account
                if (!userStorage.TryGetValue(e.Command.ChatMessage.UserId, out UserStorage storage))
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, NoStorageReply);
                    return;
                }

                // If everything checks out, attempt to make the transaction
                if (curArtifacts[index].Value <= storage.spice) // If has enough spice to buy the artifact
                {
                    // Update the user's new spice amount and add the artifact to their list
                    UpdateSpiceStorage(ref userStorage, e.Command.ChatMessage.UserId, e.Command.ChatMessage.DisplayName, -curArtifacts[index].Value);
                    AddArtifact(ref userStorage, e.Command.ChatMessage.UserId, curArtifacts[index]);
                    Artifact.SaveToHistory(curArtifacts[index]);
                    botClient.SendMessage(e.Command.ChatMessage.Channel,
                                          string.Format(ArtifactBought,
                                                        e.Command.ArgumentsAsList[1],
                                                        curArtifacts[index].Name,
                                                        curArtifacts[index].Value));
                }
                else
                {
                    // Shame the user for trying to buy an artifact they can't afford
                    botClient.SendMessage(e.Command.ChatMessage.Channel, string.Format(
                                              ArtifactNotEnoughSpice,
                                              curArtifacts[index].Name,
                                              curArtifacts[index].Value,
                                              e.Command.ChatMessage.DisplayName,
                                              storage.spice));
                }
            }

            if ((e.Command.ArgumentsAsList.Count == 1 || e.Command.ArgumentsAsList.Count == 2) &&
                e.Command.ArgumentsAsList[0].ToLower() == "check") // !artifacts check <username>
            {
                // Check if the user being checked has a spice account
                string userName = "";
                string userID   = "";

                // If the command has a user argument check to see if that user exists
                if (e.Command.ArgumentsAsList.Count == 2)
                {
                    try
                    {
                        TwitchLib.Api.V5.Models.Users.Users users = await UsernameToUser(e.Command.ArgumentsAsList[1]);

                        userName = users.Matches[0].Name;
                        userID   = users.Matches[0].Id;
                    }
                    catch
                    {
                        botClient.SendMessage(e.Command.ChatMessage.Channel, string.Format(ArtifactUserNotFound, e.Command.ArgumentsAsList[1]));
                        return;
                    }
                }
                else // Otherwise set the user as the caller
                {
                    userName = e.Command.ChatMessage.DisplayName;
                    userID   = e.Command.ChatMessage.UserId;
                }


                if (!userStorage.TryGetValue(userID, out UserStorage storage))
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, string.Format(NoStorageReply, userName));
                    return;
                }

                // Check if the user has any artifacts
                if (storage.artifacts.Count == 0)
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, string.Format(
                                              ArtifactNoneInAccount,
                                              userName));
                    return;
                }

                foreach (Artifact art in storage.artifacts)
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, Artifact.ToChat(art));
                }
            }

            if (e.Command.ArgumentsAsList.Count == 2 && e.Command.ArgumentsAsList[0].ToLower() == "history") // !artifacts history <ID>
            {
                // If the ID is valid send the artifact
                if (Int32.TryParse(e.Command.ArgumentsAsList[1], out int artID) &&
                    prevArtifacts.TryGetValue(artID, out Artifact art))
                {
                    botClient.SendMessage(e.Command.ChatMessage.Channel, Artifact.ToChat(art));
                }
                else
                {
                    // If the ID is invalid/artifact doesn't exist then send an error and exit
                    botClient.SendMessage(e.Command.ChatMessage.Channel, string.Format(
                                              ArtifactInvalid,
                                              artID));
                    return;
                }
            }
        }