1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00
Octobot/Boyfriend/Utils.cs
l1ttleO 868b6bcaa7
time-out failsafes and new warnings
rewrote setting values in SettingsCommand.cs
fixed a bug with message edited notification on mobile
fixed an exploit with WrapInline where you could escape the code block by simply using `
moved a few things in MuteCommand.cs
cleaned up code
updated library to 3.3.2
2022-02-21 22:08:55 +05:00

113 lines
No EOL
4.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Globalization;
using System.Text.RegularExpressions;
using Discord;
using Discord.Net;
namespace Boyfriend;
public static class Utils {
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'",
"%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);
var beeps = new[] {Messages.Beep1, Messages.Beep2, Messages.Beep3};
return beeps[i < 0 ? new Random().Next(3) : i];
}
public static async Task<ITextChannel?> GetAdminLogChannel(IGuild guild) {
var adminLogChannel = await ParseChannelNullable(Boyfriend.GetGuildConfig(guild).AdminLogChannel.ToString()!);
return adminLogChannel as ITextChannel;
}
public static string Wrap(string original) {
var toReturn = original.Replace("```", "ˋˋˋ");
return $"```{toReturn}{(toReturn.EndsWith("`") || toReturn.Trim().Equals("") ? " " : "")}```";
}
public static string WrapInline(string original) {
return $"`{original.Replace("`", "ˋ")}`";
}
public static string MentionChannel(ulong id) {
return $"<#{id}>";
}
private static ulong ParseMention(string mention) {
return Convert.ToUInt64(Regex.Replace(mention, "[^0-9]", ""));
}
private static ulong? ParseMentionNullable(string mention) {
try {
return ParseMention(mention) == 0 ? throw new FormatException() : ParseMention(mention);
} catch (FormatException) {
return null;
}
}
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));
}
private static async Task<IChannel> ParseChannel(string mention) {
return await Boyfriend.Client.GetChannelAsync(ParseMention(mention));
}
private static async Task<IChannel?> ParseChannelNullable(string mention) {
return ParseMentionNullable(mention) == null ? null : await ParseChannel(mention);
}
public static IRole? ParseRole(IGuild guild, string mention) {
return guild.GetRole(ParseMention(mention));
}
public static async Task SendDirectMessage(IUser user, string toSend) {
try {
await user.SendMessageAsync(toSend);
} catch (HttpException e) {
if (e.DiscordCode != DiscordErrorCode.CannotSendMessageToUser)
throw;
}
}
public static IRole? GetMuteRole(IGuild guild) {
var role = guild.Roles.FirstOrDefault(x => x.Id == Boyfriend.GetGuildConfig(guild).MuteRole);
return role;
}
public static async Task SilentSendAsync(ITextChannel? channel, string text) {
if (channel == null) return;
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);
}
public static string JoinString(string[] args, int startIndex) {
return string.Join(" ", args, startIndex, args.Length - startIndex);
}
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;
}
}