public async Task StreamOffline(List <TwitchUser> userList) { foreach (TwitchUser t in userList) { //Really iterating over servers here. if (string.IsNullOrEmpty(t.DiscordUserId)) { continue; } Task <Server> getServerTask = _context.ServerList.AsQueryable().FirstAsync(s => s.ServerId == t.ServerId); RestGuild guild = (RestGuild)await _client.GetGuildAsync(ulong.Parse(t.ServerId)); if (string.IsNullOrEmpty((await getServerTask).TwitchLiveRoleId)) { continue; } Task <RestGuildUser> getUserTask = guild.GetUserAsync(ulong.Parse(t.DiscordUserId)); IRole twitchLiveRole = guild.GetRole(ulong.Parse(getServerTask.Result.TwitchLiveRoleId)); if (twitchLiveRole == null || (await getUserTask) == null) { continue; } await getUserTask.Result.RemoveRoleAsync(twitchLiveRole); } }
/// <summary> /// Tries repeatedly to get an <see cref="IGuildUser"/> even if there's a common 5XX error /// </summary> public static async Task <IGuildUser> SuperGetUser(this RestGuild guild, ulong ID) { var requestOptions = new RequestOptions() { RetryMode = RetryMode.AlwaysRetry }; Func <Task <IGuildUser> > func = async() => { return(await guild.GetUserAsync(ID, requestOptions)); }; return(await func.SuperGet()); /*for (int i = 0; i < 3; i++) { * try { * * if (user == null) { * RestGuild restGuild = await client.Rest.GetGuildAsync(guild.Id, requestOptions); * user = await restGuild.GetUserAsync(ID, requestOptions); * } * return user; * } catch (HttpException e) { //If error happens and either has failed 3 times or non 500, 503, or 530 (not logged in) error * if (i == 2 || (e.HttpCode != System.Net.HttpStatusCode.ServiceUnavailable && e.HttpCode != System.Net.HttpStatusCode.InternalServerError && (int)e.HttpCode != 530)) throw; * } * }*/ throw new Exception("SuperGetUser ran out of tries without throwing proper exception?"); }
public async Task StreamOnline(List <TwitchUser> userList, StreamStatus streamStatus) { foreach (TwitchUser t in userList) { //Really iterating over servers here. Task <Server> getServerTask = _context.ServerList.AsQueryable().FirstAsync(s => s.ServerId == t.ServerId); RestGuild guild = (RestGuild)await _client.GetGuildAsync(ulong.Parse(t.ServerId)); Task <RestGuildUser> getUserTask = null; if (!string.IsNullOrEmpty(t.DiscordUserId)) { //We don't need this yet so we'll do this later. getUserTask = guild.GetUserAsync(ulong.Parse(t.DiscordUserId)); } Server server = await getServerTask; string twitchChannelId = server.TwitchChannel; RestGuildUser user = null; if (getUserTask != null) { user = await getUserTask; } if (user != null && !string.IsNullOrEmpty(server?.TwitchLiveRoleId)) { //We need to update the role IRole twitchLiveRole = guild.GetRole(ulong.Parse(server.TwitchLiveRoleId)); await user.AddRoleAsync(twitchLiveRole); } if (!string.IsNullOrEmpty(twitchChannelId)) { //We post updates to the channel, so now we need to see if we've already posted. string streamUrl = "https://twitch.tv/" + t.ChannelName; DateTimeOffset streamStartedUTC = streamStatus.started_at; RestTextChannel channel = await guild.GetTextChannelAsync(ulong.Parse(twitchChannelId)); //Find whether or not we've selected a message ulong lastMessageId = 0; bool sendMessage = true; while (true) { IAsyncEnumerable <IReadOnlyCollection <RestMessage> > messagesUnflattened = lastMessageId == 0 ? channel.GetMessagesAsync(100) : channel.GetMessagesAsync(lastMessageId, Direction.Before, 100); List <RestMessage> messages = (await messagesUnflattened.FlattenAsync()).ToList(); var myMessages = messages.Where(m => m.Author.Id == _client.CurrentUser.Id && m.Timestamp > streamStartedUTC && m.Content.Contains(streamUrl)).ToList(); if (myMessages.Any()) { //Already sent message. We don't need to send it again sendMessage = false; break; } else { if (messages.Last().Timestamp < streamStartedUTC) { //We're past when the stream started break; } } lastMessageId = messages.Last().Id; } if (sendMessage) { //We still need to send the message string stringToSend = ""; if (user != null && String.IsNullOrEmpty(user?.Nickname)) { stringToSend += !String.IsNullOrEmpty(user.Nickname) ? user.Nickname : user.Username; } else { stringToSend += t.ChannelName; } stringToSend += " is now live at " + streamUrl; await channel.SendMessageAsync(stringToSend); } } } }