From e06a6ecf85422b63d9c56892016ca89b0fc7efd0 Mon Sep 17 00:00:00 2001 From: Ske Date: Sun, 31 Jan 2021 14:44:37 +0100 Subject: [PATCH] Remove now-unused DiscordUtils functions --- PluralKit.Bot/Utils/DiscordUtils.cs | 129 +--------------------------- 1 file changed, 4 insertions(+), 125 deletions(-) diff --git a/PluralKit.Bot/Utils/DiscordUtils.cs b/PluralKit.Bot/Utils/DiscordUtils.cs index 1581f689..69bf51e6 100644 --- a/PluralKit.Bot/Utils/DiscordUtils.cs +++ b/PluralKit.Bot/Utils/DiscordUtils.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Globalization; using System.Linq; @@ -10,7 +9,6 @@ using System.Threading.Tasks; using DSharpPlus; using DSharpPlus.Entities; using DSharpPlus.EventArgs; -using DSharpPlus.Exceptions; using Myriad.Builders; using Myriad.Extensions; @@ -49,11 +47,6 @@ namespace PluralKit.Bot private static readonly FieldInfo _roleIdsField = typeof(DiscordMember).GetField("_role_ids", BindingFlags.NonPublic | BindingFlags.Instance); - - public static string NameAndMention(this DiscordUser user) - { - return $"{user.Username}#{user.Discriminator} ({user.Mention})"; - } public static string NameAndMention(this User user) { @@ -105,19 +98,13 @@ namespace PluralKit.Bot if (channel.Type == ChannelType.Private) return DM_PERMISSIONS; return Permissions.None; } - - public static bool BotHasAllPermissions(this DiscordChannel channel, Permissions permissionSet) => - (BotPermissions(channel) & permissionSet) == permissionSet; - + public static Instant SnowflakeToInstant(ulong snowflake) => Instant.FromUtc(2015, 1, 1, 0, 0, 0) + Duration.FromMilliseconds(snowflake >> 22); public static ulong InstantToSnowflake(Instant time) => (ulong) (time - Instant.FromUtc(2015, 1, 1, 0, 0, 0)).TotalMilliseconds << 22; - - public static ulong InstantToSnowflake(DateTimeOffset time) => - (ulong) (time - new DateTimeOffset(2015, 1, 1, 0, 0, 0, TimeSpan.Zero)).TotalMilliseconds << 22; - + public static async Task CreateReactionsBulk(this DiscordApiClient rest, Message msg, string[] reactions) { foreach (var reaction in reactions) @@ -131,29 +118,7 @@ namespace PluralKit.Bot // Workaround for https://github.com/DSharpPlus/DSharpPlus/issues/565 return input?.Replace("%20", "+"); } - - public static Task SendMessageFixedAsync(this DiscordChannel channel, string content = null, - DiscordEmbed embed = null, - IEnumerable mentions = null) => - // Passing an empty list blocks all mentions by default (null allows all through) - channel.SendMessageAsync(content, embed: embed, mentions: mentions ?? new IMention[0]); - - // This doesn't do anything by itself (DiscordMember.SendMessageAsync doesn't take a mentions argument) - // It's just here for consistency so we don't use the standard SendMessageAsync method >.> - public static Task SendMessageFixedAsync(this DiscordMember member, string content = null, - DiscordEmbed embed = null) => - member.SendMessageAsync(content, embed: embed); - - public static bool TryGetCachedUser(this DiscordClient client, ulong id, out DiscordUser user) - { - user = null; - - var cache = (ConcurrentDictionary) typeof(BaseDiscordClient) - .GetProperty("UserCache", BindingFlags.Instance | BindingFlags.NonPublic) - ?.GetValue(client); - return cache != null && cache.TryGetValue(id, out user); - } - + public static uint? ToDiscordColor(this string color) { if (uint.TryParse(color, NumberStyles.HexNumber, null, out var colorInt)) @@ -244,93 +209,7 @@ namespace PluralKit.Bot // So, surrounding with two backticks, then escaping all backtick pairs makes it impossible(!) to "break out" return $"``{EscapeBacktickPair(input)}``"; } - - public static Task GetUser(this DiscordRestClient client, ulong id) => - WrapDiscordCall(client.GetUserAsync(id)); - - public static Task GetUser(this DiscordClient client, ulong id) => - WrapDiscordCall(client.GetUserAsync(id)); - - public static Task GetChannel(this DiscordRestClient client, ulong id) => - WrapDiscordCall(client.GetChannelAsync(id)); - - public static Task GetChannel(this DiscordClient client, ulong id) => - WrapDiscordCall(client.GetChannelAsync(id)); - - public static Task GetGuild(this DiscordRestClient client, ulong id) => - WrapDiscordCall(client.GetGuildAsync(id)); - - public static Task GetGuild(this DiscordClient client, ulong id) => - WrapDiscordCall(client.GetGuildAsync(id)); - - public static Task GetMember(this DiscordRestClient client, ulong guild, ulong user) - { - async Task Inner() => - await (await client.GetGuildAsync(guild)).GetMemberAsync(user); - - return WrapDiscordCall(Inner()); - } - - public static Task GetMember(this DiscordClient client, ulong guild, ulong user) - { - async Task Inner() => - await (await client.GetGuildAsync(guild)).GetMemberAsync(user); - - return WrapDiscordCall(Inner()); - } - - public static Task GetMember(this DiscordGuild guild, ulong user) => - WrapDiscordCall(guild.GetMemberAsync(user)); - - public static Task GetMessage(this DiscordChannel channel, ulong id) => - WrapDiscordCall(channel.GetMessageAsync(id)); - - public static Task GetMessage(this DiscordRestClient client, ulong channel, ulong message) => - WrapDiscordCall(client.GetMessageAsync(channel, message)); - - public static DiscordGuild GetGuild(this DiscordShardedClient client, ulong id) - { - DiscordGuild guild; - foreach (DiscordClient shard in client.ShardClients.Values) - { - shard.Guilds.TryGetValue(id, out guild); - if (guild != null) return guild; - } - - return null; - } - - public static async Task GetChannel(this DiscordShardedClient client, ulong id, - ulong? guildId = null) - { - // we need to know the channel's guild ID to get the cached guild object, so we grab it from the API - if (guildId == null) - { - var channel = await WrapDiscordCall(client.ShardClients.Values.FirstOrDefault().GetChannelAsync(id)); - if (channel != null) guildId = channel.GuildId; - else return null; // we probably don't have the guild in cache if the API doesn't give it to us - } - - return client.GetGuild(guildId.Value).GetChannel(id); - } - - private static async Task WrapDiscordCall(Task t) - where T: class - { - try - { - return await t; - } - catch (NotFoundException) - { - return null; - } - catch (UnauthorizedException) - { - return null; - } - } - + public static EmbedBuilder WithSimpleLineContent(this EmbedBuilder eb, IEnumerable lines) { static int CharacterLimit(int pageNumber) =>