/// <summary> /// Gets information about one specified Twitch users, identified by user ID. /// </summary> public async Task <TwitchUsers.Datum> GetUserById(string id) { RestClient restClient = new RestClient("https://api.twitch.tv/helix/users"); RestRequest request = new RestRequest(Method.GET); request.AddHeader("Client-ID", botConfig.TwitchClientId); request.AddHeader("Authorization", $"Bearer {botConfig.BotAccessToken}"); request.AddOptionalParameter("id", new List <string>() { id }); IRestResponse response = await restClient.ExecuteAsync(request); communication.SendDebugMessage(response.Content); TwitchUsers twitchUsers = JsonSerializer.Deserialize <TwitchUsers>(response.Content); if ((twitchUsers?.Data?.Count ?? 0) == 0) { return(null); } return(twitchUsers.Data[0]); }
public void UserName() { Assert.IsTrue(TwitchUsers.IsValidUserName("soandso")); Assert.IsTrue(TwitchUsers.IsValidUserName("Soandso")); Assert.IsTrue(TwitchUsers.IsValidUserName("SoAndso")); Assert.IsTrue(TwitchUsers.IsValidUserName("SOANDSO")); Assert.IsTrue(TwitchUsers.IsValidUserName("so_and_so_")); Assert.IsTrue(TwitchUsers.IsValidUserName("so_and_so_12345")); }
public void UserNameNegative() { Assert.IsFalse(TwitchUsers.IsValidUserName("")); Assert.IsFalse(TwitchUsers.IsValidUserName("http://hello.com")); Assert.IsFalse(TwitchUsers.IsValidUserName("hello.com")); Assert.IsFalse(TwitchUsers.IsValidUserName("one two")); Assert.IsFalse(TwitchUsers.IsValidUserName("one ")); Assert.IsFalse(TwitchUsers.IsValidUserName(" one")); }
private string getUserID(string username) { try { string userJson = getHttpProxy("https://api.twitch.tv/helix/users?login="******""); } }
private void SaveDataBase(string twitchid, string userid, string message) { HistoryDB db = new HistoryDB(); TwitchUsers user = new TwitchUsers(); user.Username = twitchid; user.Message = message; user.TwitchID = int.Parse(userid); db.TwitchUsers.Add(user); db.SaveChanges(); }
public async Task GetCurrentSubscriptions(IEnumerable <string> channelIds) { var subscriptionEntries = await _twitchHttpClient.GetSubscriptionData(channelIds); foreach (var entry in subscriptionEntries) { var user = TwitchUsers.FirstOrDefault(x => string.Equals(entry.Key, x.Id)); if (user != null) { user.IsStreamSubscribed = (entry.Value & TwitchSubscriptionStatus.StreamSubscription) == TwitchSubscriptionStatus.StreamSubscription; user.IsFollowSubscribed = (entry.Value & TwitchSubscriptionStatus.FollowerSubscription) == TwitchSubscriptionStatus.FollowerSubscription; } } }
public async Task LoadChannelData(string channel = null) { _logger.LogFormattedMessage("Loading Twitch channel data"); if (TwitchUsers == null) { TwitchUsers = new List <TwitchUser>(); } List <string> channels; if (!string.IsNullOrEmpty(channel)) { _logger.LogFormattedMessage($"Getting channel data for {channel}"); channels = new List <string> { channel }; } else { _logger.LogFormattedMessage("Getting channels from Storage"); channels = await _storageService.GetTwitchChannels(); if (channels.Count == 0) { _logger.LogFormattedMessage("No data in storage. Getting channels from Config settings"); channels = _twitchOptions.Channels; _logger.LogFormattedMessage("Completed getting channels from Config settings"); } if (TwitchUsers.Count > 0) { return; } } _logger.LogFormattedMessage("Completed getting channels from Storage"); _logger.LogFormattedMessage("Getting channel data from Twitch"); TwitchUsers.AddRange(await _twitchHttpClient.GetTwitchChannels(channels).ConfigureAwait(false)); var ids = TwitchUsers.Where(x => string.Equals(channel, x.LoginName, StringComparison.InvariantCultureIgnoreCase)).Select(x => x.Id); await GetCurrentSubscriptions(ids); _logger.LogFormattedMessage("Completed logging channel data from Twitch"); }
public async Task <List <string> > GetBannedList(List <string> channels) { if (channels.Count == 0) { return(null); } // Call Twitch and get the list of banned us foreach (var channel in channels) { await LoadChannelData(channel); var user = TwitchUsers.FirstOrDefault(result => string.Equals(result.LoginName, channel, StringComparison.CurrentCultureIgnoreCase)); var response = await _twitchHttpClient.GetBannedUsers(user.Id); } return(null); }
private async Task UpdateStreamChangeSubscription(List <string> channels, bool isSubscribed) { foreach (var channel in channels) { var selected = TwitchUsers.FirstOrDefault(user => string.Equals(channel, user.LoginName, StringComparison.InvariantCultureIgnoreCase)); // TODO: Inject callback url into UpdateSubscription methods var request = new TwitchWebhookRequest { Callback = $"https://twitchchatbotfx20200511150805.azurewebsites.net/api/subscription/streams/{channel}", Mode = isSubscribed ? "subscribe" : "unsubscribe", Topic = $"https://api.twitch.tv/helix/streams?user_id={selected.Id}", Lease = 864000 }; var url = new Uri(Constants.TWITCH_WEBHOOKS_SUBSCRIPTION_URL); var response = await MakeHttpPostRequest(url, request, AccessToken); } }
bool Connect() { if (m_twitch != null) { var user = CurrentUser; if (user != null) { user.ModeratorStatusChanged -= ModStatusChanged; } m_twitch.InformChatClear -= ClearChatHandler; m_twitch.MessageReceived -= ChatMessageReceived; m_twitch.JtvMessageReceived -= JtvMessageReceived; m_twitch.ActionReceived -= ChatActionReceived; m_twitch.UserSubscribed -= SubscribeHandler; m_twitch.StatusUpdate -= StatusUpdate; m_twitch.Quit(); } string channel = m_channel.ToLower(); m_users = new TwitchUsers(channel); m_twitch = new TwitchClient(m_users); m_twitch.InformChatClear += ClearChatHandler; m_twitch.MessageReceived += ChatMessageReceived; m_twitch.JtvMessageReceived += JtvMessageReceived; m_twitch.ActionReceived += ChatActionReceived; m_twitch.UserSubscribed += SubscribeHandler; m_twitch.StatusUpdate += StatusUpdate; var currUser = CurrentUser = m_users.GetUser(m_options.User); currUser.ModeratorStatusChanged += ModStatusChanged; ModStatusChanged(currUser, currUser.IsStreamer || currUser.IsModerator); bool first = true; ConnectResult result; const int sleepTime = 5000; do { if (!NativeMethods.IsConnectedToInternet()) { WriteStatus("Not connected to the internet."); do { Thread.Sleep(sleepTime); } while (!NativeMethods.IsConnectedToInternet()); WriteStatus("Re-connected to the internet."); } if (!first) { Thread.Sleep(sleepTime); } first = false; result = m_twitch.Connect(channel, m_options.User, m_options.Pass); if (result == ConnectResult.LoginFailed) { WriteStatus("Failed to login, please change options.ini and restart the application."); return(false); } else if (result != ConnectResult.Success) { WriteStatus("Failed to connect: {0}", result == ConnectResult.NetworkFailed ? "network failed" : "failed"); } } while (result != ConnectResult.Success); WriteStatus("Connected to channel {0}.", channel); return(true); }