コード例 #1
0
 private Task Run()
 {
     return(Task.Run(() =>
     {
         Task task;
         string message;
         string errorMessage;
         string result;
         double amount;
         string usage;
         foreach (var command in queue.GetConsumingEnumerable())
         {
             if (!command.HasParameters || (command.HasParameters && command.Parameters.Count != 2))
             {
                 usage = $"@{command.Message.TwitchUser.DisplayName} usage: '{PrimaryCommand} <source currency code> <destination currency code>' example: '{PrimaryCommand} USD INR'";
                 task = _ircClient.SendPublicChatMessageAsync(Message: usage, Channel: command.Message.IrcChannel);
                 continue;
             }
             result = string.Empty;
             message = string.Empty;
             errorMessage = $"Sorry @{command.Message.TwitchUser.DisplayName}, I was unable to get an exchange rate.";
             _logger.LogInformation($"Processing {command.Command}...");
             try
             {
                 result = _httpClient.GetStringAsync(string.Format(ApiUri, command.Parameters[0].ToUpper(), command.Parameters[1].ToUpper())).GetAwaiter().GetResult();
                 _logger.LogInformation($"Received HTTP message: '{result}'");
             }
             catch (System.Exception ex)
             {
                 _logger.LogError(ex, "HttpClient Failed");
             }
             try
             {
                 var exchange = JsonSerializer.Deserialize <ExchangeResponse>(result, options);
                 if (exchange.Rates.TryGetValue(command.Parameters[1], out amount))
                 {
                     message = $"1 {command.Parameters[0]} is {amount} {command.Parameters[1]}";
                     _logger.LogInformation($"Parsed Fact: '{message}'");
                 }
             }
             catch (System.Exception ex)
             {
                 _logger.LogError(ex, "JSON Deserialization Failed");
             }
             if (string.IsNullOrWhiteSpace(message))
             {
                 message = errorMessage;
                 _logger.LogError("Message is null or white space.");
             }
             else
             {
                 message = $"@{command.Message.TwitchUser.DisplayName} {message}";
             }
             task = _ircClient.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
         }
     }));
 }
コード例 #2
0
 private Task Run()
 {
     return(Task.Run(() =>
     {
         Task task;
         string message;
         foreach (var command in queue.GetConsumingEnumerable())
         {
             _logger.LogInformation($"Processing {command.Command}...");
             if (command.HasParameters && command.Parameters[0] == "123")
             {
                 message = $"Valid !test Received from @{command.Message.TwitchUser.DisplayName}";
             }
             else if (command.HasParameters && (command.Parameters[0] == "69" || command.Parameters[0] == "420"))
             {
                 message = $"Are you happy now, @flamingketchup1, you filthy D-gen.";
             }
             else
             {
                 message = $"Invalid !test Received from @{command.Message.TwitchUser.DisplayName}";
             }
             task = _client.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
         }
     }));
 }
コード例 #3
0
 private Task Run()
 {
     return(Task.Run(() => {
         TwitchChatCommand command;
         Task task;
         StringBuilder builder;
         List <string> commandList;
         string commandName;
         foreach (var message in queue.GetConsumingEnumerable())
         {
             command = new TwitchChatCommand(message);
             var paramCount = command.HasParameters ? command.Parameters.Count : 0;
             _logger.LogInformation($"Command '{command.Command}' parsed with {paramCount} parameters.");
             task = NotifyCommandObservers(command);
             if (CommandRegex.IsMatch(command.Command))
             {
                 builder = new StringBuilder();
                 builder.Append($"@{command.Message.TwitchUser.DisplayName} my available commands are ");
                 commandList = new List <string>();
                 foreach (var observer in _observers)
                 {
                     commandName = observer.GetPrimaryCommand(command);
                     if (!string.IsNullOrWhiteSpace(commandName))
                     {
                         commandList.Add(commandName);
                     }
                 }
                 builder.Append(string.Join(" ", commandList.OrderBy(q => q).ToArray()));
                 task = _client.SendPublicChatMessageAsync(builder.ToString(), command.Message.IrcChannel);
             }
         }
     }));
 }
コード例 #4
0
ファイル: SoundsCommand.cs プロジェクト: markekraus/TwitchBot
        private Task Run()
        {
            return(Task.Run(() =>
            {
                Regex regex;
                string channel;
                foreach (var command in queue.GetConsumingEnumerable())
                {
                    channel = command.Message.ChannelOwner.UserName.ToLower();
                    if (channel != _config.SoundsChannel.ToLower())
                    {
                        _logger.LogInformation($"Command not sent from correct chan. SoundsChan: '{_config.SoundsChannel}', SourceChan: {channel}");
                        continue;
                    }
                    if (_commandRex.IsMatch(command.Command))
                    {
                        _ircClient.SendPublicChatMessageAsync(Message: GetDefaultCommandMessage(), Channel: command.Message.IrcChannel);
                        continue;
                    }

                    foreach (var sound in _config.Sounds)
                    {
                        if (!sound.Enabled)
                        {
                            continue;
                        }

                        if (string.Equals(command.Command, sound.PrimaryCommand, StringComparison.InvariantCultureIgnoreCase))
                        {
                            _logger.LogInformation($"playing {sound.PrimaryCommand}");
                            Play(sound.FilePath);
                            break;
                        }

                        try
                        {
                            regex = new Regex(sound.CommandRegex, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                            if (regex.IsMatch(command.Command))
                            {
                                _logger.LogInformation($"playing {sound.CommandRegex}");
                                Play(sound.FilePath);
                                break;
                            }
                        }
                        catch (System.Exception ex)
                        {
                            _logger.LogError(ex, $"Unable to parse regex '{sound.CommandRegex}' for sound command '{sound.PrimaryCommand}'.");
                        }
                    }
                }
            }));
        }
コード例 #5
0
 private Task Run()
 {
     return(Task.Run(() =>
     {
         Task task;
         string message;
         foreach (var command in queue.GetConsumingEnumerable())
         {
             _logger.LogInformation($"Processing {command.Command}...");
             message = "Oh hai Mark!";
             task = _client.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
         }
     }));
 }
コード例 #6
0
ファイル: BrbCommand.cs プロジェクト: markekraus/TwitchBot
 private Task Run()
 {
     return(Task.Run(() =>
     {
         Task task;
         string message;
         foreach (var command in queue.GetConsumingEnumerable())
         {
             _logger.LogInformation($"Processing {command.Command}...");
             message = $"We'll see you soon enough, @{command.Message.TwitchUser.DisplayName}!";
             task = _client.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
         }
     }));
 }
コード例 #7
0
 private Task Run()
 {
     return(Task.Run(() =>
     {
         Task task;
         string message;
         string errorMessage;
         string result;
         foreach (var command in queue.GetConsumingEnumerable())
         {
             result = string.Empty;
             message = string.Empty;
             errorMessage = $"Sorry @{command.Message.TwitchUser.DisplayName}, I was unable to get the ISS location.";
             _logger.LogInformation($"Processing {command.Command}...");
             try
             {
                 result = _httpClient.GetStringAsync(CatfactUri).GetAwaiter().GetResult();
                 _logger.LogInformation($"Received HTTP message: '{result}'");
             }
             catch (System.Exception ex)
             {
                 _logger.LogError(ex, "HttpClient Failed");
             }
             try
             {
                 var loc = JsonSerializer.Deserialize <IisLocation>(result, options);
                 message = $"The international Space Station is currently at Longitude: {loc.IisPosition.Longitude}, Latitude: {loc.IisPosition.Latitude}.";
                 _logger.LogInformation($"Parsed Fact: '{message}'");
             }
             catch (System.Exception ex)
             {
                 _logger.LogError(ex, "JSON Deserialization Failed");
             }
             if (string.IsNullOrWhiteSpace(message))
             {
                 message = errorMessage;
                 _logger.LogError("Message is null or white space.");
             }
             else
             {
                 message = $"@{command.Message.TwitchUser.DisplayName} {message}";
             }
             task = _ircClient.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
         }
     }));
 }
コード例 #8
0
        private Task Run()
        {
            return(Task.Run(() =>
            {
                Task task;
                string message;
                foreach (var command in queue.GetConsumingEnumerable())
                {
                    _logger.LogInformation($"Processing {command.Command}...");
                    // Support: !roll ndm
                    // Support: !roll ndm+o
                    // Support: !roll ndm-o
                    // Support: !roll nDm
                    // Support: !roll Dm
                    // Support: !roll Dm+o
                    // Support: !roll Dm-o
                    // Support: !roll m
                    // Support: !roll m+o
                    // Support: !roll m-o
                    // support: !roll
                    int numberOfDice;
                    int maxDiceSides;
                    int offset;
                    if (!TryParseParameter(command, out numberOfDice, out maxDiceSides, out offset))
                    {
                        message = $"@{command.Message.TwitchUser.DisplayName} invalid command. Examples: '!roll 3d6', '!roll 3d6+5', '!roll 3d6-5', '!roll d6', '!roll 6'.";
                    }
                    else
                    {
                        var results = Roll(numberOfDice, maxDiceSides, offset);
                        message = $"@{command.Message.TwitchUser.DisplayName} Results: {results}";
                    }

                    task = _client.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
                }
            }));
        }
コード例 #9
0
 private Task Run()
 {
     return(Task.Run(() =>
     {
         Task task;
         string message;
         string errorMessage;
         string result;
         foreach (var command in queue.GetConsumingEnumerable())
         {
             var usage = $"@{command.Message.TwitchUser.DisplayName} usage: '{PrimaryCommand} <word>' example: '{PrimaryCommand} face' or '{PrimaryCommand} \"double negative\"'";
             if (!command.HasParameters || (command.HasParameters && command.Parameters.Count != 1))
             {
                 task = _ircClient.SendPublicChatMessageAsync(
                     Message: usage,
                     Channel: command.Message.IrcChannel);
                 continue;
             }
             result = string.Empty;
             message = string.Empty;
             errorMessage = $"Sorry @{command.Message.TwitchUser.DisplayName}, I was unable to get an Urban Dictionary Definition. {usage}";
             _logger.LogInformation($"Processing {command.Command}...");
             try
             {
                 result = _httpClient.GetStringAsync(string.Format(ApiUri, command.Parameters[0])).GetAwaiter().GetResult();
                 _logger.LogInformation($"Received HTTP message: '{result}'");
             }
             catch (System.Exception ex)
             {
                 _logger.LogError(ex, "HttpClient Failed");
             }
             try
             {
                 var definition = JsonSerializer.Deserialize <UrbanDictionaryResponse>(result, options);
                 if (!string.IsNullOrWhiteSpace(definition.List[0].definition))
                 {
                     message = Regex.Replace(definition.List[0].definition, @"\r\n?|\n|\r", " ");
                     if (message.Length > 400)
                     {
                         message = message.Substring(0, 400) + "...";
                     }
                     message = $"{command.Parameters[0]} - {message}";
                     _logger.LogInformation($"Parsed Fact: '{message}'");
                 }
             }
             catch (System.Exception ex)
             {
                 _logger.LogError(ex, "JSON Deserialization Failed");
             }
             if (string.IsNullOrWhiteSpace(message))
             {
                 message = errorMessage;
                 _logger.LogError("Message is null or white space.");
             }
             else
             {
                 message = $"@{command.Message.TwitchUser.DisplayName} {message}";
             }
             task = _ircClient.SendPublicChatMessageAsync(Message: message, Channel: command.Message.IrcChannel);
         }
     }));
 }