public NanoTwitchBotCore(NanoTwitchBotStorage storage, string twitchAuthToken) { // Preconditions if (storage == null) { throw new ArgumentNullException(nameof(storage)); } if (twitchAuthToken == null || string.IsNullOrWhiteSpace(twitchAuthToken)) { throw new ArgumentNullException(nameof(twitchAuthToken)); } // Storage this.Storage = storage; // Messages this.messages = new List <NanoTwitchBotMessage>(); // Nano NanoClient nanoClient = new NanoClient(); this.Listener = new NanoListener(nanoClient, this.Storage.NanoAccount, TimeSpan.FromSeconds(this.Storage.PollingIntervalInSeconds), 100); this.Listener.Start(this.OnNanoListenerTransaction, this.OnNanoListenerError); foreach (string knownHash in this.Storage.KnownHashes) { this.Listener.AddKnownHash(knownHash); } // Twitch ConnectionCredentials credentials = new ConnectionCredentials(this.Storage.TwitchUsername, twitchAuthToken); ClientOptions clientOptions = new ClientOptions { MessagesAllowedInPeriod = 100, ThrottlingPeriod = TimeSpan.FromSeconds(30) }; WebSocketClient customClient = new WebSocketClient(clientOptions); this.Twitch = new TwitchClient(customClient); this.Twitch.Initialize(credentials, this.Storage.TwitchChannel); this.Twitch.Connect(); // Twitch Log this.Twitch.OnLog += (s, e) => { lock (this.messages) { string message = $"{e.DateTime}: {e.Data}"; this.messages.Add(new NanoTwitchBotMessage(NanoTwitchBotMessageType.Information, message)); } }; // Twitch Connected this.Twitch.OnConnected += (s, e) => { if (this.Twitch.IsConnected) { string chatMessageOnline = this.Storage.ChatMessageOnline; this.Twitch.SendMessage(new JoinedChannel(this.Storage.TwitchChannel), chatMessageOnline); } }; // Twitch Message Received this.Twitch.OnMessageReceived += (s, e) => { // Message string message = e.ChatMessage.Message; // !nano Command if (message.StartsWith("!nano")) { if (this.Twitch.IsConnected) { string chatMessageInfo = string.Format(this.Storage.ChatMessageInfo, this.Storage.NanoAccount); this.Twitch.SendMessage(new JoinedChannel(this.Storage.TwitchChannel), chatMessageInfo); } } // !nano_add Command if (message.StartsWith("!nano_add")) { string account = message.Substring("!nano_add".Length); account = account.Trim(); this.AddAddress(e.ChatMessage.DisplayName, account, true); if (this.Twitch.IsConnected) { string chatMessageAdded = string.Format(this.Storage.ChatMessageAdded, e.ChatMessage.DisplayName); this.Twitch.SendMessage(new JoinedChannel(this.Storage.TwitchChannel), chatMessageAdded); } } // !nano_delete Command if (message.StartsWith("!nano_delete")) { this.DeleteAddress(e.ChatMessage.DisplayName, true); if (this.Twitch.IsConnected) { string chatMessageDeleted = string.Format(this.Storage.ChatMessageDeleted, e.ChatMessage.DisplayName); this.Twitch.SendMessage(new JoinedChannel(this.Storage.TwitchChannel), chatMessageDeleted); } } }; }
public static void Main(string[] args) { // Storage NanoTwitchBotStorage storage = NanoTwitchBotStorage.Load(); // Header Console.ForegroundColor = ConsoleColor.DarkMagenta; Console.WriteLine("Nano Twitch Bot version 0.03"); // Information Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("This bot monitors a Nano account for pending receives"); Console.WriteLine("and posts donation messages in a channel's Twitch chat."); // Nano Account storage.NanoAccount = Program.GetString("Nano Account", "The Nano account that will receive donations.", storage.NanoAccount); // Polling Interval In Seconds storage.PollingIntervalInSeconds = Program.GetInt("Polling Interval In Seconds", "The interval at which the Nano network will be polled.", storage.PollingIntervalInSeconds); // Minimum Donation Amount storage.MinimumDonationAmount = Program.GetDouble("Minimum Donation Amount", "The minimum doination amount. Donations below this threshold will not be posted to Twitch.", storage.MinimumDonationAmount); // Twitch Channel storage.TwitchChannel = Program.GetString("Twitch Channel", "The twitch channel for the bot.", storage.TwitchChannel); // Twitch Username storage.TwitchUsername = Program.GetString("Twitch Username", "The twitch username for the bot.", storage.TwitchUsername); // Twitch Auth Token string twitchAuthToken = Program.GetString("Twitch Auth Token", "Go to https://twitchapps.com/tmi to get an auth token.", null); // Save storage.Save(); // Run Console.Clear(); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine("Starting Bot..."); try { NanoTwitchBotCore bot = new NanoTwitchBotCore(storage, twitchAuthToken); while (true) { foreach (NanoTwitchBotMessage message in bot.GetMessages()) { if (message.Type == NanoTwitchBotMessageType.Success) { Console.ForegroundColor = ConsoleColor.Green; } else if (message.Type == NanoTwitchBotMessageType.Error) { Console.ForegroundColor = ConsoleColor.Red; } else { Console.ForegroundColor = ConsoleColor.DarkGray; } Console.WriteLine(message.Message); } Thread.Sleep(1000); } } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("An error occurred."); Console.WriteLine(ex.ToString()); } }