mirror of
https://github.com/NoiTheCat/BirthdayBot.git
synced 2024-11-24 01:14:12 +00:00
Move bot uptime property from Common to Program
Additionally, update code style in Common
This commit is contained in:
parent
160152a0b4
commit
ca0dd74bae
4 changed files with 48 additions and 58 deletions
39
Common.cs
39
Common.cs
|
@ -1,26 +1,20 @@
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
|
||||||
namespace BirthdayBot
|
namespace BirthdayBot;
|
||||||
{
|
|
||||||
static class Common
|
static class Common {
|
||||||
{
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Formats a user's name to a consistent, readable format which makes use of their nickname.
|
/// Formats a user's name to a consistent, readable format which makes use of their nickname.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string FormatName(SocketGuildUser member, bool ping)
|
public static string FormatName(SocketGuildUser member, bool ping) {
|
||||||
{
|
|
||||||
if (ping) return member.Mention;
|
if (ping) return member.Mention;
|
||||||
|
|
||||||
static string escapeFormattingCharacters(string input)
|
static string escapeFormattingCharacters(string input) {
|
||||||
{
|
|
||||||
var result = new StringBuilder();
|
var result = new StringBuilder();
|
||||||
foreach (var c in input)
|
foreach (var c in input) {
|
||||||
{
|
if (c is '\\' or '_' or '~' or '*' or '@') {
|
||||||
if (c is '\\' or '_' or '~' or '*' or '@')
|
|
||||||
{
|
|
||||||
result.Append('\\');
|
result.Append('\\');
|
||||||
}
|
}
|
||||||
result.Append(c);
|
result.Append(c);
|
||||||
|
@ -29,40 +23,31 @@ namespace BirthdayBot
|
||||||
}
|
}
|
||||||
|
|
||||||
var username = escapeFormattingCharacters(member.Username);
|
var username = escapeFormattingCharacters(member.Username);
|
||||||
if (member.Nickname != null)
|
if (member.Nickname != null) {
|
||||||
{
|
|
||||||
return $"**{escapeFormattingCharacters(member.Nickname)}** ({username}#{member.Discriminator})";
|
return $"**{escapeFormattingCharacters(member.Nickname)}** ({username}#{member.Discriminator})";
|
||||||
}
|
}
|
||||||
return $"**{username}**#{member.Discriminator}";
|
return $"**{username}**#{member.Discriminator}";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly Dictionary<int, string> MonthNames = new()
|
public static Dictionary<int, string> MonthNames { get; } = new() {
|
||||||
{
|
|
||||||
{ 1, "Jan" }, { 2, "Feb" }, { 3, "Mar" }, { 4, "Apr" }, { 5, "May" }, { 6, "Jun" },
|
{ 1, "Jan" }, { 2, "Feb" }, { 3, "Mar" }, { 4, "Apr" }, { 5, "May" }, { 6, "Jun" },
|
||||||
{ 7, "Jul" }, { 8, "Aug" }, { 9, "Sep" }, { 10, "Oct" }, { 11, "Nov" }, { 12, "Dec" }
|
{ 7, "Jul" }, { 8, "Aug" }, { 9, "Sep" }, { 10, "Oct" }, { 11, "Nov" }, { 12, "Dec" }
|
||||||
};
|
};
|
||||||
|
|
||||||
public static string BotUptime => (DateTimeOffset.UtcNow - Program.BotStartTime).ToString("d' days, 'hh':'mm':'ss");
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// An alternative to <see cref="SocketGuild.HasAllMembers"/>.
|
/// An alternative to <see cref="SocketGuild.HasAllMembers"/>.
|
||||||
/// Returns true if *most* members have been downloaded.
|
/// Returns true if *most* members have been downloaded.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static bool HasMostMembersDownloaded(SocketGuild guild)
|
public static bool HasMostMembersDownloaded(SocketGuild guild) {
|
||||||
{
|
|
||||||
if (guild.HasAllMembers) return true;
|
if (guild.HasAllMembers) return true;
|
||||||
if (guild.MemberCount > 30)
|
if (guild.MemberCount > 30) {
|
||||||
{
|
|
||||||
// For guilds of size over 30, require 85% or more of the members to be known
|
// For guilds of size over 30, require 85% or more of the members to be known
|
||||||
// (26/30, 42/50, 255/300, etc)
|
// (26/30, 42/50, 255/300, etc)
|
||||||
int threshold = (int)(guild.MemberCount * 0.85);
|
int threshold = (int)(guild.MemberCount * 0.85);
|
||||||
return guild.DownloadedMemberCount >= threshold;
|
return guild.DownloadedMemberCount >= threshold;
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
// For smaller guilds, fail if two or more members are missing
|
// For smaller guilds, fail if two or more members are missing
|
||||||
return guild.MemberCount - guild.DownloadedMemberCount <= 2;
|
return guild.MemberCount - guild.DownloadedMemberCount <= 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -6,7 +6,12 @@ namespace BirthdayBot;
|
||||||
|
|
||||||
class Program {
|
class Program {
|
||||||
private static ShardManager _bot;
|
private static ShardManager _bot;
|
||||||
public static DateTimeOffset BotStartTime { get; } = DateTimeOffset.UtcNow;
|
private static readonly DateTimeOffset _botStartTime = DateTimeOffset.UtcNow;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns the amount of time the program has been running in a human-readable format.
|
||||||
|
/// </summary>
|
||||||
|
public static string BotUptime => (DateTimeOffset.UtcNow - _botStartTime).ToString("d' days, 'hh':'mm':'ss");
|
||||||
|
|
||||||
static async Task Main() {
|
static async Task Main() {
|
||||||
var cfg = new Configuration();
|
var cfg = new Configuration();
|
||||||
|
|
|
@ -105,7 +105,7 @@ class ShardManager : IDisposable {
|
||||||
Log("Warning: Not all shards terminated cleanly after 30 seconds. Continuing...");
|
Log("Warning: Not all shards terminated cleanly after 30 seconds. Continuing...");
|
||||||
}
|
}
|
||||||
|
|
||||||
Log($"Uptime: {Common.BotUptime}");
|
Log($"Uptime: {Program.BotUptime}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Log(string message) => Program.Log(nameof(ShardManager), message);
|
private void Log(string message) => Program.Log(nameof(ShardManager), message);
|
||||||
|
@ -158,7 +158,7 @@ class ShardManager : IDisposable {
|
||||||
private async Task StatusLoop() {
|
private async Task StatusLoop() {
|
||||||
try {
|
try {
|
||||||
while (!_mainCancel.IsCancellationRequested) {
|
while (!_mainCancel.IsCancellationRequested) {
|
||||||
Log($"Bot uptime: {Common.BotUptime}");
|
Log($"Bot uptime: {Program.BotUptime}");
|
||||||
|
|
||||||
// Iterate through shard list, extract data
|
// Iterate through shard list, extract data
|
||||||
var guildInfo = new Dictionary<int, GuildStatusData>();
|
var guildInfo = new Dictionary<int, GuildStatusData>();
|
||||||
|
|
|
@ -156,7 +156,7 @@ namespace BirthdayBot.UserInterface
|
||||||
//strStats.AppendLine("Server count: " + Discord.Guilds.Count.ToString());
|
//strStats.AppendLine("Server count: " + Discord.Guilds.Count.ToString());
|
||||||
// TODO restore this statistic
|
// TODO restore this statistic
|
||||||
strStats.AppendLine("Shard #" + instance.ShardId.ToString("00"));
|
strStats.AppendLine("Shard #" + instance.ShardId.ToString("00"));
|
||||||
strStats.AppendLine("Uptime: " + Common.BotUptime);
|
strStats.AppendLine("Uptime: " + Program.BotUptime);
|
||||||
|
|
||||||
// TODO fun stats
|
// TODO fun stats
|
||||||
// current birthdays, total names registered, unique time zones
|
// current birthdays, total names registered, unique time zones
|
||||||
|
|
Loading…
Reference in a new issue