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; }
private async void OnChatCommandReceived(object sender, OnChatCommandReceivedArgs e) { TwitchLib.Api.V5.Models.Users.Users user; string message = ""; switch (e.Command.CommandText.ToLower()) { case "spicebot": Console.WriteLine("!SpiceBot command received."); botClient.SendMessage(e.Command.ChatMessage.Channel, SpiceBotReply); break; case "redspice": Console.WriteLine("!RedSpice command received."); botClient.SendMessage(e.Command.ChatMessage.Channel, RedSpiceReply); break; case "myspice": case "yourspice": Console.WriteLine("!MySpice/!YourSpice command received."); if (e.Command.CommandText.ToLower() == "yourspice") { if (e.Command.ArgumentsAsList.Count != 1) { return; } user = await UsernameToUser(e.Command.ArgumentsAsList[0]); if (user.Matches.Length == 1) { DisplaySpice(user.Matches[0].Id, user.Matches[0].DisplayName, e.Command.ChatMessage.Channel); } } else { DisplaySpice(e.Command.ChatMessage.UserId, e.Command.ChatMessage.DisplayName, e.Command.ChatMessage.Channel); } break; case "artifacts": Console.WriteLine("!artifacts command received."); HandleArtifactChatCommands(e); break; case "modspice": // !modspice <name> <amount> if (!e.Command.ChatMessage.IsModerator || e.Command.ArgumentsAsList.Count != 2) { return; } // Mod-only command to manually fix people's spice Console.WriteLine("!ModSpice command received."); user = await UsernameToUser(e.Command.ArgumentsAsList[0]); if (!Int32.TryParse(e.Command.ArgumentsAsList[1], out int spiceChange)) { return; } if (user.Matches.Length == 1) { UpdateSpiceStorage(ref userStorage, user.Matches[0].Id, user.Matches[0].DisplayName, spiceChange); } break; case "addcommand": if (!(e.Command.ChatMessage.IsModerator || e.Command.ChatMessage.IsBroadcaster) || e.Command.ArgumentsAsList.Count <= 1) { return; } if (chatCommands.ContainsKey(e.Command.ArgumentsAsList[0])) { return; } chatCommands.Add(e.Command.ArgumentsAsList[0], e.Command.ArgumentsAsString.Substring(e.Command.ArgumentsAsString.IndexOf(' '))); ChatCommand.SaveToJson(commandDataPath, chatCommands); break; case "removecommand": if (!(e.Command.ChatMessage.IsModerator || e.Command.ChatMessage.IsBroadcaster) || e.Command.ArgumentsAsList.Count != 1) { return; } chatCommands.Remove(e.Command.ArgumentsAsList[0]); ChatCommand.SaveToJson(commandDataPath, chatCommands); break; case "updatecommand": if (!(e.Command.ChatMessage.IsModerator || e.Command.ChatMessage.IsBroadcaster) || e.Command.ArgumentsAsList.Count <= 1) { return; } if (chatCommands.ContainsKey(e.Command.ArgumentsAsList[0])) { chatCommands[e.Command.ArgumentsAsList[0]] = e.Command.ArgumentsAsString.Substring(e.Command.ArgumentsAsString.IndexOf(' ')); } ChatCommand.SaveToJson(commandDataPath, chatCommands); break; case "commands": message = "Chat Commands\n"; foreach (string key in chatCommands.Keys) { message += $"!{key}\n"; } botClient.SendMessage(e.Command.ChatMessage.Channel, message); break; default: if (chatCommands.TryGetValue(e.Command.CommandText.ToLower(), out message)) { botClient.SendMessage(e.Command.ChatMessage.Channel, message); } else { Console.WriteLine($"Received unknown command: {e.Command.CommandText}, from {e.Command.ChatMessage.Username}."); } break; } }