2021-12-15 22:07:04 +03:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Text.RegularExpressions;
|
2021-12-10 14:25:29 +03:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.Net;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
|
|
|
|
|
namespace Boyfriend;
|
2021-12-07 15:52:37 +03:00
|
|
|
|
|
|
|
|
|
public static class Utils {
|
2022-01-18 20:38:15 +03:00
|
|
|
|
private static readonly string[] Formats = {
|
|
|
|
|
"%d'd'%h'h'%m'm'%s's'", "%d'd'%h'h'%m'm'", "%d'd'%h'h'%s's'", "%d'd'%h'h'", "%d'd'%m'm'%s's'", "%d'd'%m'm'",
|
|
|
|
|
"%d'd'%s's'", "%d'd'", "%h'h'%m'm'%s's'", "%h'h'%m'm'", "%h'h'%s's'", "%h'h'", "%m'm'%s's'", "%m'm'", "%s's'",
|
2021-12-07 21:27:27 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
"%d'д'%h'ч'%m'м'%s'с'", "%d'д'%h'ч'%m'м'", "%d'д'%h'ч'%s'с'", "%d'д'%h'ч'", "%d'д'%m'м'%s'с'", "%d'д'%m'м'",
|
|
|
|
|
"%d'д'%s'с'", "%d'д'", "%h'ч'%m'м'%s'с'", "%h'ч'%m'м'", "%h'ч'%s'с'", "%h'ч'", "%m'м'%s'с'", "%m'м'", "%s'с'"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static string GetBeep(string cultureInfo, int i = -1) {
|
|
|
|
|
Messages.Culture = new CultureInfo(cultureInfo);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
var beeps = new[] {Messages.Beep1, Messages.Beep2, Messages.Beep3};
|
|
|
|
|
return beeps[i < 0 ? new Random().Next(3) : i];
|
|
|
|
|
}
|
2021-12-15 22:07:04 +03:00
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public static async Task<ITextChannel?> GetAdminLogChannel(IGuild guild) {
|
2022-01-30 11:43:15 +03:00
|
|
|
|
var adminLogChannel = await ParseChannelNullable(Boyfriend.GetGuildConfig(guild).AdminLogChannel.ToString()!);
|
2022-01-18 20:38:15 +03:00
|
|
|
|
return adminLogChannel as ITextChannel;
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Wrap(string original) {
|
2021-12-10 14:25:29 +03:00
|
|
|
|
var toReturn = original.Replace("```", "```");
|
|
|
|
|
return $"```{toReturn}{(toReturn.EndsWith("`") || toReturn.Trim().Equals("") ? " " : "")}```";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string WrapInline(string original) {
|
|
|
|
|
return $"`{original}`";
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string MentionChannel(ulong id) {
|
2021-12-10 14:25:29 +03:00
|
|
|
|
return $"<#{id}>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static ulong ParseMention(string mention) {
|
|
|
|
|
return Convert.ToUInt64(Regex.Replace(mention, "[^0-9]", ""));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
private static ulong? ParseMentionNullable(string mention) {
|
|
|
|
|
try {
|
|
|
|
|
return ParseMention(mention) == 0 ? throw new FormatException() : ParseMention(mention);
|
2022-02-02 16:14:26 +03:00
|
|
|
|
} catch (FormatException) {
|
2022-01-18 20:38:15 +03:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 14:25:29 +03:00
|
|
|
|
public static async Task<IUser> ParseUser(string mention) {
|
|
|
|
|
var user = Boyfriend.Client.GetUserAsync(ParseMention(mention));
|
|
|
|
|
return await user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static async Task<IGuildUser> ParseMember(IGuild guild, string mention) {
|
|
|
|
|
return await guild.GetUserAsync(ParseMention(mention));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
private static async Task<IChannel> ParseChannel(string mention) {
|
2021-12-15 22:07:04 +03:00
|
|
|
|
return await Boyfriend.Client.GetChannelAsync(ParseMention(mention));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public static async Task<IChannel?> ParseChannelNullable(string mention) {
|
|
|
|
|
return ParseMentionNullable(mention) == null ? null : await ParseChannel(mention);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static IRole? ParseRole(IGuild guild, string mention) {
|
2021-12-15 22:07:04 +03:00
|
|
|
|
return guild.GetRole(ParseMention(mention));
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public static IRole? ParseRoleNullable(IGuild guild, string mention) {
|
|
|
|
|
return ParseMentionNullable(mention) == null ? null : ParseRole(guild, mention);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-10 14:25:29 +03:00
|
|
|
|
public static async Task SendDirectMessage(IUser user, string toSend) {
|
|
|
|
|
try {
|
|
|
|
|
await user.SendMessageAsync(toSend);
|
|
|
|
|
} catch (HttpException e) {
|
|
|
|
|
if (e.DiscordCode != DiscordErrorCode.CannotSendMessageToUser)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public static IRole? GetMuteRole(IGuild guild) {
|
2021-12-15 22:07:04 +03:00
|
|
|
|
var role = guild.Roles.FirstOrDefault(x => x.Id == Boyfriend.GetGuildConfig(guild).MuteRole);
|
2021-12-10 14:25:29 +03:00
|
|
|
|
return role;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 20:38:15 +03:00
|
|
|
|
public static async Task SilentSendAsync(ITextChannel? channel, string text) {
|
|
|
|
|
if (channel == null) return;
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
2021-12-15 22:07:04 +03:00
|
|
|
|
try {
|
|
|
|
|
await channel.SendMessageAsync(text, false, null, null, AllowedMentions.None);
|
|
|
|
|
} catch (ArgumentException) {}
|
|
|
|
|
}
|
|
|
|
|
public static TimeSpan GetTimeSpan(string from) {
|
|
|
|
|
return TimeSpan.ParseExact(from.ToLowerInvariant(), Formats,
|
|
|
|
|
CultureInfo.InvariantCulture);
|
2021-12-07 21:27:27 +03:00
|
|
|
|
}
|
2022-01-18 20:38:15 +03:00
|
|
|
|
|
|
|
|
|
public static string JoinString(string[] args, int startIndex) {
|
|
|
|
|
return string.Join(" ", args, startIndex, args.Length - startIndex);
|
|
|
|
|
}
|
2022-02-02 16:14:26 +03:00
|
|
|
|
|
|
|
|
|
public static string GetNameAndDiscrim(IUser user) {
|
|
|
|
|
return $"{user.Username}#{user.Discriminator}";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static RequestOptions GetRequestOptions(string reason) {
|
|
|
|
|
var options = RequestOptions.Default;
|
|
|
|
|
options.AuditLogReason = reason;
|
|
|
|
|
return options;
|
|
|
|
|
}
|
2022-01-30 11:43:15 +03:00
|
|
|
|
}
|