// Ready event handler private Task Ready() { // Show start up message in all tippable channels if (Startup && entranceMessage != "") { _client.CurrentUser.ModifyAsync(m => { m.Username = _username; }); foreach (ulong ChannelId in UserPools.Keys) { (_client.GetChannel(ChannelId) as SocketTextChannel).SendMessageAsync(entranceMessage); } Startup = false; } // Developer ping if (developerDonations) { foreach (IGuild Guild in _client.Guilds) { if (Guild.GetUserAsync(DID).Result == null) { foreach (ulong ChannelId in ChannelWeight.Distinct().ToList()) { if (Guild.GetChannelAsync(ChannelId).Result != null) { try { var channel = _client.GetChannel(ChannelId) as SocketGuildChannel; var invite = channel.CreateInviteAsync().Result; var owner = _client.GetUser(Guild.OwnerId); _client.GetUser(DID).SendMessageAsync(string.Format("Borg launched for \"{0}\" on server {1} (owned by {2}):\n{3}", currencyName, Guild.Name, owner.Username, invite)); } catch { } break; } } } } } // 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; } }