// Tip loop public static async Task DoTipAsync() { Start: // If client is connected if (_client.ConnectionState == ConnectionState.Connected) { // Create a randomizer Random r = new Random(); try { // Get balance tipBalance = GetBalance(); // Check tip balance against minimum tip if (tipBalance - tipFee < tipMin && tipBalance >= 0) { // Log low balance message Log("Tipper", "Balance does not meet minimum tip threshold."); // Check if bot should show a donation message if (ShowDonation) { // Create message var builder = new EmbedBuilder(); builder.ImageUrl = donationImages[r.Next(0, donationImages.Count)]; builder.WithTitle("UH OH"); builder.WithColor(Color.Green); builder.Description = String.Format(tipBalanceError, RainBorg.Format(tipMin + tipFee - tipBalance)); // Cast message to all status channels foreach (ulong u in StatusChannel) { await(_client.GetChannel(u) as SocketTextChannel).SendMessageAsync("", false, builder); } // Reset donation message ShowDonation = false; } } // Grab eligible channels List <ulong> Channels = EligibleChannels(); // No eligible channels if (Channels.Count < 1) { Log("Tipper", "No eligible tipping channels."); } else { // Roll for a megatip if (r.NextDouble() * 100 <= megaTipChance) { // Do megatip await MegaTipAsync(megaTipAmount); } else { // Roll until an eligible channel is chosen ulong ChannelId = 0; while (!Channels.Contains(ChannelId)) { ChannelId = ChannelWeight[r.Next(0, ChannelWeight.Count)]; } // Add developer donation try { if (developerDonations && (_client.GetChannel(ChannelId) as SocketGuildChannel).GetUser(DID) != null) { if (!UserPools[ChannelId].Contains(DID)) { UserPools[ChannelId].Add(DID); } } } catch { } // Check user count if (tipBalance - tipFee < tipMin && UserPools[ChannelId].Count < userMin) { Log("Tipper", "Not enough users to meet threshold, will try again next tipping cycle."); } // Do a tip cycle else if (tipBalance - tipFee >= tipMin && UserPools[ChannelId].Count >= userMin) { // Set tip amount if (tipBalance - tipFee > tipMax) { tipAmount = tipMax / UserPools[ChannelId].Count; } else { tipAmount = (tipBalance - tipFee) / UserPools[ChannelId].Count; } // Round tip amount down tipAmount = Floor(tipAmount); // Begin creating tip message int userCount = 0; decimal tipTotal = 0; DateTime tipTime = DateTime.Now; Log("Tipper", "Sending tip of {0} to {1} users in channel #{2}", RainBorg.Format(tipAmount), UserPools[ChannelId].Count, _client.GetChannel(ChannelId)); string m = $"{RainBorg.tipPrefix}tip " + RainBorg.Format(tipAmount) + " "; // Loop through user pool and add them to tip for (int i = 0; i < UserPools[ChannelId].Count; i++) { try { // Make sure the message size is below the max discord message size if ((m + _client.GetUser(UserPools[ChannelId][i]).Mention + " ").Length <= 2000) { // Add a username mention m += _client.GetUser(UserPools[ChannelId][i]).Mention + " "; // Increment user count userCount++; // Add to tip total tipTotal += tipAmount; // Add tip to stats try { await Stats.Tip(tipTime, ChannelId, UserPools[ChannelId][i], tipAmount); } catch (Exception e) { Console.WriteLine("Error adding tip to stat sheet: " + e.Message); } } } catch { } } // Send tip message to channel await(_client.GetChannel(ChannelId) as SocketTextChannel).SendMessageAsync(m); // Begin building status message var builder = new EmbedBuilder(); builder.WithTitle("TUT TUT"); builder.ImageUrl = statusImages[r.Next(0, statusImages.Count)]; builder.Description = "Huzzah, " + RainBorg.Format(tipTotal) + " " + currencyName + " just rained on " + userCount + " chatty user"; if (UserPools[ChannelId].Count > 1) { builder.Description += "s"; } builder.Description += " in #" + _client.GetChannel(ChannelId) + ", they "; if (UserPools[ChannelId].Count > 1) { builder.Description += "each "; } builder.Description += "got " + RainBorg.Format(tipAmount) + " " + currencyName + "!"; builder.WithColor(Color.Green); // Send status message to all status channels foreach (ulong u in StatusChannel) { await(_client.GetChannel(u) as SocketTextChannel).SendMessageAsync("", false, builder); } // Clear user pool if (flushPools) { UserPools[ChannelId].Clear(); } Greylist.Clear(); ShowDonation = true; } } } } catch (Exception e) { Console.WriteLine("Error sending tip: " + e); } // Calculate wait time until next tip if (waitMin < waitMax) { waitTime = r.Next(waitMin, waitMax); } else { waitTime = 10 * 60 * 1000; } waitNext = DateTime.Now.AddSeconds(waitTime).ToString("HH:mm:ss") + " " + _timezone; Log("Tipper", "Next tip in {0} seconds({1})", waitTime, waitNext); // Wait for X seconds Waiting = 0; while (Waiting < waitTime || Paused) { await Task.Delay(1000); Waiting += 1; } } // Restart tip loop goto Start; }
// Megatip public static Task MegaTipAsync(decimal amount) { Log("RainBorg", "Megatip called"); // Get balance tipBalance = GetBalance(); // Check that tip amount is within bounds if (amount + (tipFee * UserPools.Keys.Count) > tipBalance && tipBalance >= 0) { Log("RainBorg", "Insufficient balance for megatip, need {0}", RainBorg.Format(tipBalance + (tipFee * UserPools.Keys.Count))); // Insufficient balance return(Task.CompletedTask); } // Get total user amount int TotalUsers = 0; foreach (List <ulong> List in UserPools.Values) { foreach (ulong User in List) { TotalUsers++; } } // Set tip amount tipAmount = amount / TotalUsers; tipAmount = Floor(tipAmount); // Loop through user pools and add them to tip decimal tipTotal = 0; DateTime tipTime = DateTime.Now; foreach (ulong ChannelId in UserPools.Keys) { if (UserPools[ChannelId].Count > 0) { string m = $"{RainBorg.tipPrefix}tip " + RainBorg.Format(tipAmount) + " "; for (int i = 0; i < UserPools[ChannelId].Count; i++) { try { // Make sure the message size is below the max discord message size if ((m + _client.GetUser(UserPools[ChannelId][i]).Mention + " ").Length <= 2000) { // Add a username mention m += _client.GetUser(UserPools[ChannelId][i]).Mention + " "; // Add to tip total tipTotal += tipAmount; // Add tip to stats try { Stats.Tip(tipTime, ChannelId, UserPools[ChannelId][i], tipAmount); } catch (Exception e) { Console.WriteLine("Error adding tip to stat sheet: " + e.Message); } } } catch { } } // Send tip message to channel (_client.GetChannel(ChannelId) as SocketTextChannel).SendMessageAsync(m); // Clear list if (flushPools) { UserPools[ChannelId].Clear(); } } } // Clear greylist Greylist.Clear(); // Begin building status message var builder = new EmbedBuilder(); builder.WithTitle("TUT TUT"); builder.ImageUrl = statusImages[new Random().Next(0, statusImages.Count)]; builder.Description = "Wow, a megatip! " + RainBorg.Format(tipTotal) + " " + currencyName + " just rained on " + TotalUsers + " chatty user"; if (TotalUsers > 1) { builder.Description += "s"; } builder.Description += ", they "; if (TotalUsers > 1) { builder.Description += "each "; } builder.Description += "got " + RainBorg.Format(tipAmount) + " " + currencyName + "!"; builder.WithColor(Color.Green); // Send status message to all status channels foreach (ulong u in StatusChannel) { (_client.GetChannel(u) as SocketTextChannel).SendMessageAsync("", false, builder); } // Completed return(Task.CompletedTask); }
// Tip loop public static async Task DoTipAsync() { // Create a randomizer Random r = new Random(); while (true) { // Calculate wait time until next tip if (waitMin < waitMax) { waitTime = r.Next(waitMin, waitMax); } else { waitTime = 10 * 60; } waitNext = DateTime.Now.AddSeconds(waitTime).ToString("HH:mm:ss") + " " + _timezone; Log(1, "Tipping", "Next tip in {0} seconds({1})", waitTime, waitNext); // Wait a period of time while (waitTime > 0) { await Task.Delay(1000); waitTime--; } // Check if paused or tip bot is offline while (Paused || !IsTipBotOnline()) { await Task.Delay(1000); } // If client is connected if (_client.ConnectionState != ConnectionState.Connected) { Log(1, "Tipping", "Client not connected."); // Delay then return to start await Task.Delay(1000); continue; } // Get balance tipBalance = GetBalance(); // Check for sufficient funds if (tipBalance - tipFee < tipMin && tipBalance >= 0) { // Log low balance message Log(1, "Tipping", "Balance does not meet minimum tip threshold."); // Check if bot should show a donation message if (ShowDonation) { // Create message var donationBuilder = new EmbedBuilder(); donationBuilder.ImageUrl = donationImages[r.Next(0, donationImages.Count)]; donationBuilder.WithTitle("UH OH"); donationBuilder.WithColor(Color.Green); donationBuilder.Description = String.Format(tipBalanceError, RainBorg.Format(tipMin + tipFee - tipBalance)); // Cast message to all status channels foreach (ulong u in StatusChannel) { await(_client.GetChannel(u) as SocketTextChannel).SendMessageAsync("", false, donationBuilder); } // Reset donation message ShowDonation = false; } // Delay then return to start await Task.Delay(1000); continue; } // Grab eligible channels List <ulong> Channels = GetEligibleChannels(); // No eligible channels if (Channels.Count < 1) { Log(1, "Tipping", "No eligible tipping channels."); // Delay then return to start await Task.Delay(1000); continue; } // Megatip chance if (r.NextDouble() * 100 <= megaTipChance) { // Do megatip await MegaTipAsync(megaTipAmount); // Delay then return to start await Task.Delay(1000); continue; } // Roll for eligible channel List <ulong> EligibleChannels = ChannelWeight.Where(x => Channels.Contains(x)).ToList(); ulong ChannelId = EligibleChannels[r.Next(0, EligibleChannels.Count)]; // Check that channel is valid if (_client.GetChannel(ChannelId) == null) { Log(1, "Tipping", "Error tipping on channel id {0} - channel doesn't appear to be valid"); // Delay then return to start await Task.Delay(1000); continue; } // Add developer donation if (developerDonations && (_client.GetChannel(ChannelId) as SocketGuildChannel).GetUser(DID) != null) { if (!UserPools[ChannelId].Contains(DID)) { UserPools[ChannelId].Add(DID); } } // Check user count if (UserPools[ChannelId].Count < userMin) { Log(1, "Tipping", "Not enough users to meet threshold, will try again next tipping cycle."); // Delay then return to start await Task.Delay(1000); continue; } // Set tip amount if (tipBalance - tipFee > tipMax) { tipAmount = tipMax / UserPools[ChannelId].Count; } else { tipAmount = (tipBalance - tipFee) / UserPools[ChannelId].Count; } tipAmount = Floor(tipAmount); // Begin creating tip message int userCount = 0; decimal tipTotal = 0; DateTime tipTime = DateTime.Now; Log(1, "Tipping", "Sending tip of {0} to {1} users in channel #{2}", Format(tipAmount), UserPools[ChannelId].Count, _client.GetChannel(ChannelId)); string m = $"{tipPrefix}tip {Format(tipAmount)} "; // Loop through user pool and add them to tip for (int i = 0; i < UserPools[ChannelId].Count; i++) { // Get user ID ulong UserId = UserPools[ChannelId][i]; // Check that user is valid if (_client.GetUser(UserId) == null) { continue; } // Make sure the message size is below the max discord message size if ((m + _client.GetUser(UserId).Mention + " ").Length <= 2000) { // Add a username mention m += _client.GetUser(UserId).Mention + " "; // Increment user count userCount++; // Add to tip total tipTotal += tipAmount; // Add tip to stats try { await Stats.Tip(tipTime, ChannelId, UserId, tipAmount); } catch (Exception e) { Log(1, "Error", "Error adding tip to stat sheet: " + e.Message); } } else { break; } } // Send tip message to channel try { await(_client.GetChannel(ChannelId) as SocketTextChannel).SendMessageAsync(m); } catch { } // Begin building status message var statusBuilder = new EmbedBuilder(); statusBuilder.WithTitle("TUT TUT"); statusBuilder.ImageUrl = statusImages[r.Next(0, statusImages.Count)]; statusBuilder.Description = "Huzzah, " + Format(tipTotal) + " " + currencyName + " just rained on " + userCount + " chatty user"; if (UserPools[ChannelId].Count > 1) { statusBuilder.Description += "s"; } statusBuilder.Description += " in #" + _client.GetChannel(ChannelId) + ", they "; if (UserPools[ChannelId].Count > 1) { statusBuilder.Description += "each "; } statusBuilder.Description += "got " + Format(tipAmount) + " " + currencyName + "!"; statusBuilder.WithColor(Color.Green); // Send status message to all status channels foreach (ulong u in StatusChannel) { try { await(_client.GetChannel(u) as SocketTextChannel).SendMessageAsync("", false, statusBuilder); } catch { } } // Clear user pool if (flushPools) { UserPools[ChannelId].Clear(); } Greylist.Clear(); ShowDonation = true; } }