Finish preliminary rework, ready to test

This commit is contained in:
Dark 2021-06-02 04:07:50 +02:00
parent 57893e7f76
commit d0a1beb809
No known key found for this signature in database
GPG key ID: 2CD6ACB6B0A87B8A
177 changed files with 854 additions and 707 deletions

View file

@ -1,7 +1,6 @@
import { utilityCmd } from "../types";
import { EmbedWith, multiSorter, resolveMember, sorter } from "../../../utils";
import { getCurrentUptime } from "../../../uptime";
import humanizeDuration from "humanize-duration";
import LCL from "last-commit-log";
@ -9,6 +8,7 @@ import path from "path";
import moment from "moment-timezone";
import { rootDir } from "../../../paths";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { GuildChannel, MessageOptions } from "discord.js";
export const AboutCmd = utilityCmd({
trigger: "about",
@ -41,7 +41,7 @@ export const AboutCmd = utilityCmd({
version = "?";
}
const shard = pluginData.client.shards.get(pluginData.client.guildShardMap[pluginData.guild.id])!;
//const shard = pluginData.client.shards.get(pluginData.client.guildShardMap[pluginData.guild.id])!; FIXME Sharding stuff
const lastReload = humanizeDuration(Date.now() - pluginData.state.lastReload, {
largest: 2,
@ -53,7 +53,7 @@ export const AboutCmd = utilityCmd({
["Last reload", `${lastReload} ago`],
["Last update", lastUpdate],
["Version", version],
["API latency", `${shard.latency}ms`],
// ["API latency", `${shard.latency}ms`],
["Server timezone", timeAndDate.getGuildTz()],
];
@ -65,7 +65,7 @@ export const AboutCmd = utilityCmd({
);
loadedPlugins.sort();
const aboutContent: MessageContent & { embed: EmbedWith<"title" | "fields"> } = {
const aboutContent: MessageOptions & { embed: EmbedWith<"title" | "fields"> } = {
embed: {
title: `About ${pluginData.client.user!.username}`,
fields: [
@ -102,7 +102,7 @@ export const AboutCmd = utilityCmd({
// For the embed color, find the highest colored role the bot has - this is their color on the server as well
const botMember = await resolveMember(pluginData.client, pluginData.guild, pluginData.client.user!.id);
let botRoles = botMember?.roles.map(r => (msg.channel as GuildChannel).guild.roles.get(r)!) || [];
let botRoles = botMember?.roles.cache.map(r => (msg.channel as GuildChannel).guild.roles.cache.get(r.id)!) || [];
botRoles = botRoles.filter(r => !!r); // Drop any unknown roles
botRoles = botRoles.filter(r => r.color); // Filter to those with a color
botRoles.sort(sorter("position", "DESC")); // Sort by position (highest first)
@ -111,10 +111,10 @@ export const AboutCmd = utilityCmd({
}
// Use the bot avatar as the embed image
if (pluginData.client.user!avatarURL) {
aboutContent.embed.thumbnail = { url: pluginData.client.user!.avatarURL };
if (pluginData.client.user!.avatarURL()) {
aboutContent.embed.thumbnail = { url: pluginData.client.user!.avatarURL()! };
}
msg.channel.createMessage(aboutContent);
msg.channel.send(aboutContent);
},
});

View file

@ -2,6 +2,7 @@ import { utilityCmd } from "../types";
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { UnknownUser } from "../../../utils";
import { sendErrorMessage } from "../../../pluginUtils";
import { MessageEmbedOptions } from "discord.js";
export const AvatarCmd = utilityCmd({
trigger: ["avatar", "av"],
@ -15,15 +16,16 @@ export const AvatarCmd = utilityCmd({
async run({ message: msg, args, pluginData }) {
const user = args.user || msg.author;
if (!(user instanceof UnknownUser)) {
let extension = user.avatarURL.slice(user.avatarURL.lastIndexOf("."), user.avatarURL.lastIndexOf("?"));
const avatar = user.avatarURL() || user.defaultAvatarURL;
let extension = avatar.slice(avatar.lastIndexOf("."), avatar.lastIndexOf("?"));
// Some pngs can have the .jpg extention for some reason, so we always use .png for static images
extension = extension === ".gif" ? extension : ".png";
const avatarUrl = user.avatarURL.slice(0, user.avatarURL.lastIndexOf("."));
const embed: EmbedOptions = {
const avatarUrl = avatar.slice(0, avatar.lastIndexOf("."));
const embed: MessageEmbedOptions = {
image: { url: avatarUrl + `${extension}?size=2048` },
};
embed.title = `Avatar of ${user.username}#${user.discriminator}:`;
msg.channel.createMessage({ embed });
msg.channel.send({ embed });
} else {
sendErrorMessage(pluginData, msg.channel, "Invalid user ID");
}

View file

@ -20,6 +20,6 @@ export const ChannelInfoCmd = utilityCmd({
return;
}
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -9,6 +9,7 @@ import { SavedMessage } from "../../../data/entities/SavedMessage";
import { LogType } from "../../../data/LogType";
import { allowTimeout } from "../../../RegExpRunner";
import { ModActionsPlugin } from "../../../plugins/ModActions/ModActionsPlugin";
import { TextChannel, User, Message } from "discord.js";
const MAX_CLEAN_COUNT = 150;
const MAX_CLEAN_TIME = 1 * DAYS;
@ -32,7 +33,7 @@ async function cleanMessages(
pluginData.state.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, idsToDelete[0]);
// Actually delete the messages
await pluginData.client.deleteMessages(channel.id, idsToDelete);
(pluginData.guild.channels.cache.get(channel.id) as TextChannel).bulkDelete(idsToDelete);
await pluginData.state.savedMessages.markBulkAsDeleted(idsToDelete);
// Create an archive
@ -106,18 +107,18 @@ export const CleanCmd = utilityCmd({
}
}
const cleaningMessage = msg.channel.createMessage("Cleaning...");
const cleaningMessage = msg.channel.send("Cleaning...");
const messagesToClean: SavedMessage[] = [];
let beforeId = msg.id;
const timeCutoff = msg.timestamp - MAX_CLEAN_TIME;
const timeCutoff = msg.createdTimestamp - MAX_CLEAN_TIME;
const upToMsgId = args["to-id"];
let foundId = false;
const deletePins = args["delete-pins"] != null ? args["delete-pins"] : false;
let pins: Message[] = [];
if (!deletePins) {
pins = await msg.channel.getPins();
pins = (await msg.channel.messages.fetchPinned()).array();
}
while (messagesToClean.length < args.count) {

View file

@ -4,6 +4,7 @@ import { messageLink } from "../../../utils";
import { sendErrorMessage } from "../../../pluginUtils";
import { canReadChannel } from "../../../utils/canReadChannel";
import { TextChannel } from "discord.js";
export const ContextCmd = utilityCmd({
trigger: "context",
@ -35,12 +36,17 @@ export const ContextCmd = utilityCmd({
return;
}
const previousMessage = (await pluginData.client.getMessages(channel.id, 1, messageId))[0];
const previousMessage = (
await (pluginData.guild.channels.cache.get(channel.id) as TextChannel).messages.fetch({
limit: 1,
before: messageId,
})
)[0];
if (!previousMessage) {
sendErrorMessage(pluginData, msg.channel, "Message context not found");
return;
}
msg.channel.createMessage(messageLink(pluginData.guild.id, previousMessage.channel.id, previousMessage.id));
msg.channel.send(messageLink(pluginData.guild.id, previousMessage.channel.id, previousMessage.id));
},
});

View file

@ -27,6 +27,6 @@ export const EmojiInfoCmd = utilityCmd({
return;
}
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -71,7 +71,7 @@ export const HelpCmd = utilityCmd({
});
if (totalResults === 0) {
msg.channel.createMessage("No matching commands found!");
msg.channel.send("No matching commands found!");
return;
}

View file

@ -42,7 +42,7 @@ export const InfoCmd = utilityCmd({
if (channel) {
const embed = await getChannelInfoEmbed(pluginData, channelId!, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -50,11 +50,11 @@ export const InfoCmd = utilityCmd({
// 2. Server
if (userCfg.can_server) {
const guild = pluginData.client.guilds.get(value);
const guild = pluginData.client.guilds.fetch(value);
if (guild) {
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -66,7 +66,7 @@ export const InfoCmd = utilityCmd({
if (user && userCfg.can_userinfo) {
const embed = await getUserInfoEmbed(pluginData, user.id, Boolean(args.compact), message.author.id);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -84,7 +84,7 @@ export const InfoCmd = utilityCmd({
message.author.id,
);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -99,7 +99,7 @@ export const InfoCmd = utilityCmd({
if (invite) {
const embed = await getInviteInfoEmbed(pluginData, inviteCode);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -112,7 +112,7 @@ export const InfoCmd = utilityCmd({
if (serverPreview) {
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -124,7 +124,7 @@ export const InfoCmd = utilityCmd({
const role = roleId && pluginData.guild.roles.cache.get(roleId);
if (role) {
const embed = await getRoleInfoEmbed(pluginData, role, message.author.id);
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -135,7 +135,7 @@ export const InfoCmd = utilityCmd({
if (emojiIdMatch?.[2]) {
const embed = await getEmojiInfoEmbed(pluginData, emojiIdMatch[2]);
if (embed) {
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}
}
@ -144,7 +144,7 @@ export const InfoCmd = utilityCmd({
// 9. Arbitrary ID
if (isValidSnowflake(value) && userCfg.can_snowflake) {
const embed = await getSnowflakeInfoEmbed(pluginData, value, true, message.author.id);
message.channel.createMessage({ embed });
message.channel.send({ embed });
return;
}

View file

@ -22,6 +22,6 @@ export const InviteInfoCmd = utilityCmd({
return;
}
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -83,6 +83,6 @@ export const JumboCmd = utilityCmd({
};
}
msg.channel.createMessage("", file);
msg.channel.send("", file);
},
});

View file

@ -17,6 +17,8 @@ export const LevelCmd = utilityCmd({
run({ message, args, pluginData }) {
const member = args.member || message.member;
const level = getMemberLevel(pluginData, member);
message.channel.createMessage(`The permission level of ${member.username}#${member.discriminator} is **${level}**`);
message.channel.send(
`The permission level of ${member.user.username}#${member.user.discriminator} is **${level}**`,
);
},
});

View file

@ -31,6 +31,6 @@ export const MessageInfoCmd = utilityCmd({
return;
}
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -16,24 +16,24 @@ export const NicknameCmd = utilityCmd({
async run({ message: msg, args, pluginData }) {
if (msg.member.id !== args.member.id && !canActOn(pluginData, msg.member, args.member)) {
msg.channel.createMessage(errorMessage("Cannot change nickname: insufficient permissions"));
msg.channel.send(errorMessage("Cannot change nickname: insufficient permissions"));
return;
}
const nicknameLength = [...args.nickname].length;
if (nicknameLength < 2 || nicknameLength > 32) {
msg.channel.createMessage(errorMessage("Nickname must be between 2 and 32 characters long"));
msg.channel.send(errorMessage("Nickname must be between 2 and 32 characters long"));
return;
}
const oldNickname = args.member.nick || "<none>";
const oldNickname = args.member.nickname || "<none>";
try {
await args.member.edit({
nick: args.nickname,
});
} catch {
msg.channel.createMessage(errorMessage("Failed to change nickname"));
msg.channel.send(errorMessage("Failed to change nickname"));
return;
}

View file

@ -15,7 +15,7 @@ export const NicknameResetCmd = utilityCmd({
async run({ message: msg, args, pluginData }) {
if (msg.member.id !== args.member.id && !canActOn(pluginData, msg.member, args.member)) {
msg.channel.createMessage(errorMessage("Cannot reset nickname: insufficient permissions"));
msg.channel.send(errorMessage("Cannot reset nickname: insufficient permissions"));
return;
}
@ -24,7 +24,7 @@ export const NicknameResetCmd = utilityCmd({
nick: "",
});
} catch {
msg.channel.createMessage(errorMessage("Failed to reset nickname"));
msg.channel.send(errorMessage("Failed to reset nickname"));
return;
}

View file

@ -1,5 +1,6 @@
import { utilityCmd } from "../types";
import { noop, trimLines } from "../../../utils";
import { Message } from "discord.js";
const { performance } = require("perf_hooks");
@ -15,12 +16,12 @@ export const PingCmd = utilityCmd({
for (let i = 0; i < 4; i++) {
const start = performance.now();
const message = await msg.channel.createMessage(`Calculating ping... ${i + 1}`);
const message = await msg.channel.send(`Calculating ping... ${i + 1}`);
times.push(performance.now() - start);
messages.push(message);
if (msgToMsgDelay === undefined) {
msgToMsgDelay = message.timestamp - msg.timestamp;
msgToMsgDelay = message.createdTimestamp - msg.createdTimestamp;
}
}
@ -28,25 +29,19 @@ export const PingCmd = utilityCmd({
const lowest = Math.round(Math.min(...times));
const mean = Math.round(times.reduce((total, ms) => total + ms, 0) / times.length);
const shard = pluginData.client.shards.get(pluginData.client.guildShardMap[pluginData.guild.id])!;
// const shard = pluginData.client.shards.get(pluginData.client.guildShardMap[pluginData.guild.id])!; FIXME sharding stuff
msg.channel.createMessage(
msg.channel.send(
trimLines(`
**Ping:**
Lowest: **${lowest}ms**
Highest: **${highest}ms**
Mean: **${mean}ms**
Time between ping command and first reply: **${msgToMsgDelay!}ms**
Shard latency: **${shard.latency}ms**
`),
`), // Omitted line: Shard latency: **${shard.latency}ms**
);
// Clean up test messages
pluginData.client
.deleteMessages(
messages[0].channel.id,
messages.map(m => m.id),
)
.catch(noop);
msg.channel.bulkDelete(messages).catch(noop);
},
});

View file

@ -1,6 +1,7 @@
import { utilityCmd } from "../types";
import { activeReloads } from "../guildReloads";
import { TextChannel } from "discord.js";
export const ReloadGuildCmd = utilityCmd({
trigger: "reload_guild",
@ -11,7 +12,7 @@ export const ReloadGuildCmd = utilityCmd({
if (activeReloads.has(pluginData.guild.id)) return;
activeReloads.set(pluginData.guild.id, msg.channel as TextChannel);
msg.channel.createMessage("Reloading...");
msg.channel.send("Reloading...");
pluginData.getKnubInstance().reloadGuild(pluginData.guild.id);
},
});

View file

@ -15,6 +15,6 @@ export const RoleInfoCmd = utilityCmd({
async run({ message, args, pluginData }) {
const embed = await getRoleInfoEmbed(pluginData, args.role, message.author.id);
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -4,6 +4,7 @@ import { commandTypeHelpers as ct } from "../../../commandTypes";
import { chunkArray, sorter, trimLines } from "../../../utils";
import { refreshMembersIfNeeded } from "../refreshMembers";
import { sendErrorMessage } from "../../../pluginUtils";
import { Role, TextChannel } from "discord.js";
export const RolesCmd = utilityCmd({
trigger: "roles",
@ -21,7 +22,9 @@ export const RolesCmd = utilityCmd({
async run({ message: msg, args, pluginData }) {
const { guild } = pluginData;
let roles: Array<{ _memberCount?: number } & Role> = Array.from((msg.channel as TextChannel).guild.roles.values());
let roles: Array<{ _memberCount?: number } & Role> = Array.from(
(msg.channel as TextChannel).guild.roles.cache.values(),
);
let sort = args.sort;
if (args.search) {
@ -33,8 +36,8 @@ export const RolesCmd = utilityCmd({
await refreshMembersIfNeeded(guild);
// If the user requested role member counts as well, calculate them and sort the roles by their member count
const roleCounts: Map<string, number> = Array.from(guild.members.values()).reduce((map, member) => {
for (const roleId of member.roles) {
const roleCounts: Map<string, number> = Array.from(guild.members.cache.values()).reduce((map, member) => {
for (const roleId of member.roles.cache) {
if (!map.has(roleId)) map.set(roleId, 0);
map.set(roleId, map.get(roleId) + 1);
}
@ -97,14 +100,14 @@ export const RolesCmd = utilityCmd({
});
if (i === 0) {
msg.channel.createMessage(
msg.channel.send(
trimLines(`
${args.search ? "Total roles found" : "Total roles"}: ${roles.length}
\`\`\`py\n${roleLines.join("\n")}\`\`\`
`),
);
} else {
msg.channel.createMessage("```py\n" + roleLines.join("\n") + "```");
msg.channel.send("```py\n" + roleLines.join("\n") + "```");
}
}
},

View file

@ -21,6 +21,6 @@ export const ServerInfoCmd = utilityCmd({
return;
}
message.channel.createMessage({ embed: serverInfoEmbed });
message.channel.send({ embed: serverInfoEmbed });
},
});

View file

@ -16,6 +16,6 @@ export const SnowflakeInfoCmd = utilityCmd({
async run({ message, args, pluginData }) {
const embed = await getSnowflakeInfoEmbed(pluginData, args.id, false, message.author.id);
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -22,9 +22,7 @@ export const SourceCmd = utilityCmd({
return;
}
const message = await pluginData.client
.getMessage(args.message.channel.id, args.message.messageId)
.catch(() => null);
const message = await args.message.channel.messages.fetch(args.message.messageId).catch(() => null);
if (!message) {
sendErrorMessage(pluginData, cmdMessage.channel, "Unknown message");
return;
@ -44,6 +42,6 @@ export const SourceCmd = utilityCmd({
const archiveId = await pluginData.state.archives.create(source, moment.utc().add(1, "hour"));
const baseUrl = getBaseUrl(pluginData);
const url = pluginData.state.archives.getUrl(baseUrl, archiveId);
cmdMessage.channel.createMessage(`Message source: ${url}`);
cmdMessage.channel.send(`Message source: ${url}`);
},
});

View file

@ -23,6 +23,6 @@ export const UserInfoCmd = utilityCmd({
return;
}
message.channel.createMessage({ embed });
message.channel.send({ embed });
},
});

View file

@ -10,7 +10,7 @@ import {
import { canActOn, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
import { LogType } from "../../../data/LogType";
import { resolveChannel } from "knub/dist/helpers";
import { VoiceChannel } from "discord.js";
export const VcdisconnectCmd = utilityCmd({
trigger: ["vcdisconnect", "vcdisc", "vcdc", "vckick", "vck"],
@ -28,16 +28,14 @@ export const VcdisconnectCmd = utilityCmd({
return;
}
if (!args.member.voiceState || !args.member.voiceState.channelID) {
if (!args.member.voice || !args.member.voice.channelID) {
sendErrorMessage(pluginData, msg.channel, "Member is not in a voice channel");
return;
}
const channel = (await resolveChannel(pluginData.guild, args.member.voiceState.channelID)) as VoiceChannel;
const channel = pluginData.guild.channels.cache.get(args.member.voice.channelID) as VoiceChannel;
try {
await args.member.edit({
channelID: null,
});
await args.member.voice.kick();
} catch {
sendErrorMessage(pluginData, msg.channel, "Failed to disconnect member");
return;

View file

@ -11,6 +11,7 @@ import {
import { canActOn, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
import { LogType } from "../../../data/LogType";
import { VoiceChannel } from "discord.js";
export const VcmoveCmd = utilityCmd({
trigger: "vcmove",
@ -47,7 +48,7 @@ export const VcmoveCmd = utilityCmd({
channel = potentialChannel;
} else {
// Search string -> find closest matching voice channel name
const voiceChannels = pluginData.guild.channels.filter(theChannel => {
const voiceChannels = pluginData.guild.channels.cache.array().filter(theChannel => {
return theChannel instanceof VoiceChannel;
}) as VoiceChannel[];
const closestMatch = simpleClosestStringMatch(args.channel, voiceChannels, ch => ch.name);
@ -59,21 +60,21 @@ export const VcmoveCmd = utilityCmd({
channel = closestMatch;
}
if (!args.member.voiceState || !args.member.voiceState.channelID) {
if (!args.member.voice || !args.member.voice.channelID) {
sendErrorMessage(pluginData, msg.channel, "Member is not in a voice channel");
return;
}
if (args.member.voiceState.channelID === channel.id) {
if (args.member.voice.channelID === channel.id) {
sendErrorMessage(pluginData, msg.channel, "Member is already on that channel!");
return;
}
const oldVoiceChannel = pluginData.guild.channels.cache.get(args.member.voiceState.channelID);
const oldVoiceChannel = pluginData.guild.channels.cache.get(args.member.voice.channelID);
try {
await args.member.edit({
channelID: channel.id,
channel: channel.id,
});
} catch {
sendErrorMessage(pluginData, msg.channel, "Failed to move member");
@ -130,7 +131,7 @@ export const VcmoveAllCmd = utilityCmd({
channel = potentialChannel;
} else {
// Search string -> find closest matching voice channel name
const voiceChannels = pluginData.guild.channels.filter(theChannel => {
const voiceChannels = pluginData.guild.channels.cache.array().filter(theChannel => {
return theChannel instanceof VoiceChannel;
}) as VoiceChannel[];
const closestMatch = simpleClosestStringMatch(args.channel, voiceChannels, ch => ch.name);
@ -142,7 +143,7 @@ export const VcmoveAllCmd = utilityCmd({
channel = closestMatch;
}
if (args.oldChannel.voiceMembers.size === 0) {
if (args.oldChannel.members.size === 0) {
sendErrorMessage(pluginData, msg.channel, "Voice channel is empty");
return;
}
@ -154,9 +155,9 @@ export const VcmoveAllCmd = utilityCmd({
// Cant leave null, otherwise we get an assignment error in the catch
let currMember = msg.member;
const moveAmt = args.oldChannel.voiceMembers.size;
const moveAmt = args.oldChannel.members.size;
let errAmt = 0;
for (const memberWithId of args.oldChannel.voiceMembers) {
for (const memberWithId of args.oldChannel.members) {
currMember = memberWithId[1];
// Check for permissions but allow self-moves
@ -164,7 +165,7 @@ export const VcmoveAllCmd = utilityCmd({
sendErrorMessage(
pluginData,
msg.channel,
`Failed to move ${currMember.username}#${currMember.discriminator} (${currMember.id}): You cannot act on this member`,
`Failed to move ${currMember.user.username}#${currMember.user.discriminator} (${currMember.id}): You cannot act on this member`,
);
errAmt++;
continue;
@ -172,7 +173,7 @@ export const VcmoveAllCmd = utilityCmd({
try {
currMember.edit({
channelID: channel.id,
channel: channel.id,
});
} catch {
if (msg.member.id === currMember.id) {
@ -182,7 +183,7 @@ export const VcmoveAllCmd = utilityCmd({
sendErrorMessage(
pluginData,
msg.channel,
`Failed to move ${currMember.username}#${currMember.discriminator} (${currMember.id})`,
`Failed to move ${currMember.user.username}#${currMember.user.discriminator} (${currMember.id})`,
);
errAmt++;
continue;

View file

@ -5,6 +5,8 @@ import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { EmbedWith, formatNumber, preEmbedPadding, trimLines } from "../../../utils";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { MessageEmbedOptions, Constants, VoiceChannel, StageChannel } from "discord.js";
import { ChannelTypeStrings } from "src/types";
const TEXT_CHANNEL_ICON =
"https://cdn.discordapp.com/attachments/740650744830623756/740656843545772062/text-channel.png";
@ -19,7 +21,7 @@ export async function getChannelInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
channelId: string,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
): Promise<MessageEmbedOptions | null> {
const channel = pluginData.guild.channels.cache.get(channelId);
if (!channel) {
return null;
@ -30,22 +32,22 @@ export async function getChannelInfoEmbed(
};
let icon = TEXT_CHANNEL_ICON;
if (channel.type === Constants.ChannelTypes.GUILD_VOICE) {
if (channel.type === ChannelTypeStrings.VOICE) {
icon = VOICE_CHANNEL_ICON;
} else if (channel.type === Constants.ChannelTypes.GUILD_NEWS) {
} else if (channel.type === ChannelTypeStrings.NEWS) {
icon = ANNOUNCEMENT_CHANNEL_ICON;
} else if (channel.type === Constants.ChannelTypes.GUILD_STAGE) {
} else if (channel.type === ChannelTypeStrings.STAGE) {
icon = STAGE_CHANNEL_ICON;
}
const channelType =
{
[Constants.ChannelTypes.GUILD_TEXT]: "Text channel",
[Constants.ChannelTypes.GUILD_VOICE]: "Voice channel",
[Constants.ChannelTypes.GUILD_CATEGORY]: "Category",
[Constants.ChannelTypes.GUILD_NEWS]: "Announcement channel",
[Constants.ChannelTypes.GUILD_STORE]: "Store channel",
[Constants.ChannelTypes.GUILD_STAGE]: "Stage channel",
[ChannelTypeStrings.TEXT]: "Text channel",
[ChannelTypeStrings.VOICE]: "Voice channel",
[ChannelTypeStrings.CATEGORY]: "Category",
[ChannelTypeStrings.NEWS]: "Announcement channel",
[ChannelTypeStrings.STORE]: "Store channel",
[ChannelTypeStrings.STAGE]: "Stage channel",
}[channel.type] || "Channel";
embed.author = {
@ -55,9 +57,9 @@ export async function getChannelInfoEmbed(
let channelName = `#${channel.name}`;
if (
channel.type === Constants.ChannelTypes.GUILD_VOICE ||
channel.type === Constants.ChannelTypes.GUILD_CATEGORY ||
channel.type === Constants.ChannelTypes.GUILD_STAGE
channel.type === ChannelTypeStrings.VOICE ||
channel.type === ChannelTypeStrings.CATEGORY ||
channel.type === ChannelTypeStrings.STAGE
) {
channelName = channel.name;
}
@ -68,12 +70,12 @@ export async function getChannelInfoEmbed(
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const channelAge = humanizeDuration(Date.now() - channel.createdAt, {
const channelAge = humanizeDuration(Date.now() - channel.createdTimestamp, {
largest: 2,
round: true,
});
const showMention = channel.type !== Constants.ChannelTypes.GUILD_CATEGORY;
const showMention = channel.type !== ChannelTypeStrings.CATEGORY;
embed.fields.push({
name: preEmbedPadding + "Channel information",
@ -86,11 +88,11 @@ export async function getChannelInfoEmbed(
`),
});
if (channel.type === Constants.ChannelTypes.GUILD_VOICE || channel.type === Constants.ChannelTypes.GUILD_STAGE) {
const voiceMembers = Array.from(channel.voiceMembers.values());
const muted = voiceMembers.filter(vm => vm.voiceState.mute || vm.voiceState.selfMute);
const deafened = voiceMembers.filter(vm => vm.voiceState.deaf || vm.voiceState.selfDeaf);
const voiceOrStage = channel.type === Constants.ChannelTypes.GUILD_VOICE ? "Voice" : "Stage";
if (channel.type === ChannelTypeStrings.VOICE || channel.type === ChannelTypeStrings.STAGE) {
const voiceMembers = Array.from((channel as VoiceChannel | StageChannel).members.values());
const muted = voiceMembers.filter(vm => vm.voice.mute || vm.voice.selfMute);
const deafened = voiceMembers.filter(vm => vm.voice.deaf || vm.voice.selfDeaf);
const voiceOrStage = channel.type === ChannelTypeStrings.VOICE ? "Voice" : "Stage";
embed.fields.push({
name: preEmbedPadding + `${voiceOrStage} information`,
@ -102,21 +104,20 @@ export async function getChannelInfoEmbed(
});
}
if (channel.type === Constants.ChannelTypes.GUILD_CATEGORY) {
const textChannels = pluginData.guild.channels.filter(
ch => ch.parentID === channel.id && ch.type !== Constants.ChannelTypes.GUILD_VOICE,
if (channel.type === ChannelTypeStrings.CATEGORY) {
const textChannels = pluginData.guild.channels.cache.filter(
ch => ch.parentID === channel.id && ch.type !== ChannelTypeStrings.VOICE,
);
const voiceChannels = pluginData.guild.channels.filter(
const voiceChannels = pluginData.guild.channels.cache.filter(
ch =>
ch.parentID === channel.id &&
(ch.type === Constants.ChannelTypes.GUILD_VOICE || ch.type === Constants.ChannelTypes.GUILD_STAGE),
ch.parentID === channel.id && (ch.type === ChannelTypeStrings.VOICE || ch.type === ChannelTypeStrings.STAGE),
);
embed.fields.push({
name: preEmbedPadding + "Category information",
value: trimLines(`
Text channels: **${textChannels.length}**
Voice channels: **${voiceChannels.length}**
Text channels: **${textChannels.size}**
Voice channels: **${voiceChannels.size}**
`),
});
}

View file

@ -1,12 +1,13 @@
import { GuildPluginData } from "knub";
import { UtilityPluginType } from "../types";
import { trimLines, preEmbedPadding, EmbedWith } from "../../../utils";
import { MessageEmbedOptions } from "discord.js";
export async function getEmojiInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
emojiId: string,
): Promise<EmbedOptions | null> {
const emoji = pluginData.guild.emojis.find(e => e.id === emojiId);
): Promise<MessageEmbedOptions | null> {
const emoji = pluginData.guild.emojis.cache.find(e => e.id === emojiId);
if (!emoji) {
return null;
}

View file

@ -1,8 +1,9 @@
import { Client, GuildPreview } from "discord.js";
import { memoize, MINUTES } from "../../../utils";
/**
* Memoized getGuildPreview
*/
export function getGuildPreview(client: Client, guildId: string): Promise<GuildPreview | null> {
return memoize(() => client.getGuildPreview(guildId).catch(() => null), `getGuildPreview_${guildId}`, 10 * MINUTES);
return memoize(() => client.fetchGuildPreview(guildId).catch(() => null), `getGuildPreview_${guildId}`, 10 * MINUTES);
}

View file

@ -9,6 +9,7 @@ import {
EmbedWith,
emptyEmbedValue,
formatNumber,
GroupDMInvite,
inviteHasCounts,
isGroupDMInvite,
isGuildInvite,
@ -16,12 +17,14 @@ import {
resolveInvite,
trimLines,
} from "../../../utils";
import { MessageEmbedOptions, Constants, Invite } from "discord.js";
import { ChannelTypeStrings } from "src/types";
export async function getInviteInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
inviteCode: string,
): Promise<EmbedOptions | null> {
const invite = await resolveInvite(pluginData.client, inviteCode, true);
): Promise<MessageEmbedOptions | null> {
let invite = await resolveInvite(pluginData.client, inviteCode, true);
if (!invite) {
return null;
}
@ -67,9 +70,7 @@ export async function getInviteInfoEmbed(
});
const channelName =
invite.channel.type === Constants.ChannelTypes.GUILD_VOICE
? `🔉 ${invite.channel.name}`
: `#${invite.channel.name}`;
invite.channel.type === ChannelTypeStrings.VOICE ? `🔉 ${invite.channel.name}` : `#${invite.channel.name}`;
const channelCreatedAtTimestamp = snowflakeToTimestamp(invite.channel.id);
const channelCreatedAt = moment.utc(channelCreatedAtTimestamp, "x");
@ -84,7 +85,7 @@ export async function getInviteInfoEmbed(
Created: **${channelAge} ago**
`);
if (invite.channel.type !== Constants.ChannelTypes.GUILD_VOICE) {
if (invite.channel.type !== ChannelTypeStrings.VOICE) {
channelInfo += `\nMention: <#${invite.channel.id}>`;
}
@ -113,16 +114,17 @@ export async function getInviteInfoEmbed(
fields: [],
};
invite = invite as GroupDMInvite;
embed.author = {
name: invite.channel.name ? `Group DM invite: ${invite.channel.name}` : `Group DM invite`,
url: `https://discord.gg/${invite.code}`,
};
}; // FIXME pending invite re-think
if (invite.channel.icon) {
/*if (invite.channel.icon) {
embed.author.icon_url = `https://cdn.discordapp.com/channel-icons/${invite.channel.id}/${invite.channel.icon}.png?size=256`;
}
const channelCreatedAtTimestamp = snowflakeToTimestamp(invite.channel.id);
}*/ const channelCreatedAtTimestamp = snowflakeToTimestamp(
invite.channel.id,
);
const channelCreatedAt = moment.utc(channelCreatedAtTimestamp, "x");
const channelAge = humanizeDuration(Date.now() - channelCreatedAtTimestamp, {
largest: 2,

View file

@ -6,6 +6,8 @@ import humanizeDuration from "humanize-duration";
import { chunkMessageLines, EmbedWith, messageLink, preEmbedPadding, trimEmptyLines, trimLines } from "../../../utils";
import { getDefaultPrefix } from "knub/dist/commands/commandUtils";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { MessageEmbedOptions, Constants, TextChannel } from "discord.js";
import { MessageTypeStrings } from "src/types";
const MESSAGE_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/740685652152025088/message.png";
@ -14,8 +16,10 @@ export async function getMessageInfoEmbed(
channelId: string,
messageId: string,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
const message = await pluginData.client.getMessage(channelId, messageId).catch(() => null);
): Promise<MessageEmbedOptions | null> {
const message = await (pluginData.guild.channels.resolve(channelId) as TextChannel).messages
.fetch(messageId)
.catch(() => null);
if (!message) {
return null;
}
@ -36,12 +40,12 @@ export async function getMessageInfoEmbed(
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const messageAge = humanizeDuration(Date.now() - message.createdAt, {
const messageAge = humanizeDuration(Date.now() - message.createdTimestamp, {
largest: 2,
round: true,
});
const editedAt = message.editedTimestamp && moment.utc(message.editedTimestamp, "x");
const editedAt = message.editedTimestamp ? moment.utc(message.editedTimestamp!, "x") : undefined;
const tzEditedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, editedAt)
: timeAndDate.inGuildTz(editedAt);
@ -55,16 +59,16 @@ export async function getMessageInfoEmbed(
const type =
{
[Constants.MessageTypes.DEFAULT]: "Regular message",
[Constants.MessageTypes.CHANNEL_PINNED_MESSAGE]: "System message",
[Constants.MessageTypes.GUILD_MEMBER_JOIN]: "System message",
[Constants.MessageTypes.USER_PREMIUM_GUILD_SUBSCRIPTION]: "System message",
[Constants.MessageTypes.USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1]: "System message",
[Constants.MessageTypes.USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2]: "System message",
[Constants.MessageTypes.USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3]: "System message",
[Constants.MessageTypes.CHANNEL_FOLLOW_ADD]: "System message",
[Constants.MessageTypes.GUILD_DISCOVERY_DISQUALIFIED]: "System message",
[Constants.MessageTypes.GUILD_DISCOVERY_REQUALIFIED]: "System message",
[MessageTypeStrings.DEFAULT]: "Regular message",
[MessageTypeStrings.PINS_ADD]: "System message",
[MessageTypeStrings.GUILD_MEMBER_JOIN]: "System message",
[MessageTypeStrings.USER_PREMIUM_GUILD_SUBSCRIPTION]: "System message",
[MessageTypeStrings.USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_1]: "System message",
[MessageTypeStrings.USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_2]: "System message",
[MessageTypeStrings.USER_PREMIUM_GUILD_SUBSCRIPTION_TIER_3]: "System message",
[MessageTypeStrings.CHANNEL_FOLLOW_ADD]: "System message",
[MessageTypeStrings.GUILD_DISCOVERY_DISQUALIFIED]: "System message",
[MessageTypeStrings.GUILD_DISCOVERY_REQUALIFIED]: "System message",
}[message.type] || "Unknown";
embed.fields.push({
@ -87,12 +91,12 @@ export async function getMessageInfoEmbed(
? await timeAndDate.inMemberTz(requestMemberId, authorCreatedAt)
: timeAndDate.inGuildTz(authorCreatedAt);
const prettyAuthorCreatedAt = tzAuthorCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const authorAccountAge = humanizeDuration(Date.now() - message.author.createdAt, {
const authorAccountAge = humanizeDuration(Date.now() - message.author.createdTimestamp, {
largest: 2,
round: true,
});
const authorJoinedAt = message.member && moment.utc(message.member.joinedAt, "x");
const authorJoinedAt = message.member && moment.utc(message.member.joinedTimestamp!, "x");
const tzAuthorJoinedAt = authorJoinedAt
? requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, authorJoinedAt)
@ -101,7 +105,7 @@ export async function getMessageInfoEmbed(
const prettyAuthorJoinedAt = tzAuthorJoinedAt?.format(timeAndDate.getDateFormat("pretty_datetime"));
const authorServerAge =
message.member &&
humanizeDuration(Date.now() - message.member.joinedAt, {
humanizeDuration(Date.now() - message.member.joinedTimestamp!, {
largest: 2,
round: true,
});
@ -126,7 +130,7 @@ export async function getMessageInfoEmbed(
});
}
if (message.attachments.length) {
if (message.attachments.size) {
embed.fields.push({
name: preEmbedPadding + "Attachments",
value: message.attachments[0].url,

View file

@ -4,6 +4,7 @@ import { trimLines, preEmbedPadding, EmbedWith } from "../../../utils";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { Role, MessageEmbedOptions } from "discord.js";
const MENTION_ICON = "https://cdn.discordapp.com/attachments/705009450855039042/839284872152481792/mention.png";
@ -11,7 +12,7 @@ export async function getRoleInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
role: Role,
requestMemberId?: string,
): Promise<EmbedOptions> {
): Promise<MessageEmbedOptions> {
const embed: EmbedWith<"fields"> = {
fields: [],
};
@ -29,12 +30,12 @@ export async function getRoleInfoEmbed(
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const roleAge = humanizeDuration(Date.now() - role.createdAt, {
const roleAge = humanizeDuration(Date.now() - role.createdTimestamp, {
largest: 2,
round: true,
});
const rolePerms = Object.keys(role.permissions.json).map(p =>
const rolePerms = Object.keys(role.permissions.toJSON()).map(p =>
p
// Voice channel related permission names start with 'voice'
.replace(/^voice/i, "")
@ -44,7 +45,7 @@ export async function getRoleInfoEmbed(
);
// -1 because of the @everyone role
const totalGuildRoles = pluginData.guild.roles.size - 1;
const totalGuildRoles = pluginData.guild.roles.cache.size - 1;
embed.fields.push({
name: preEmbedPadding + "Role information",

View file

@ -17,16 +17,17 @@ import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { getGuildPreview } from "./getGuildPreview";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { MessageEmbedOptions, CategoryChannel, TextChannel, VoiceChannel } from "discord.js";
export async function getServerInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
serverId: string,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
): Promise<MessageEmbedOptions | null> {
const thisServer = serverId === pluginData.guild.id ? pluginData.guild : null;
const [restGuild, guildPreview] = await Promise.all([
thisServer
? memoize(() => pluginData.client.getRESTGuild(serverId), `getRESTGuild_${serverId}`, 10 * MINUTES)
? memoize(() => pluginData.client.guilds.fetch(serverId), `getRESTGuild_${serverId}`, 10 * MINUTES)
: null,
getGuildPreview(pluginData.client, serverId),
]);
@ -46,12 +47,12 @@ export async function getServerInfoEmbed(
embed.author = {
name: `Server: ${(guildPreview || restGuild)!.name}`,
icon_url: (guildPreview || restGuild)!.iconURL ?? undefined,
iconURL: (guildPreview || restGuild)!.iconURL() ?? undefined,
};
// BASIC INFORMATION
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAt = moment.utc((guildPreview || restGuild)!.createdAt, "x");
const createdAt = moment.utc((guildPreview || restGuild)!.id, "x"); // FIXME ID -> Timestamp
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
@ -86,7 +87,7 @@ export async function getServerInfoEmbed(
const bannerUrl = restGuild?.bannerURL ? `[Link](${restGuild.bannerURL})` : "None";
const splashUrl =
(restGuild || guildPreview)!.splashURL != null
? `[Link](${(restGuild || guildPreview)!.splashURL?.replace("size=128", "size=2048")})`
? `[Link](${(restGuild || guildPreview)!.splashURL()?.replace("size=128", "size=2048")})`
: "None";
embed.fields.push(
@ -113,33 +114,33 @@ export async function getServerInfoEmbed(
restGuild?.approximateMemberCount ||
restGuild?.memberCount ||
thisServer?.memberCount ||
thisServer?.members.size ||
thisServer?.members.cache.size ||
0;
let onlineMemberCount = (guildPreview?.approximatePresenceCount || restGuild?.approximatePresenceCount)!;
if (onlineMemberCount == null && restGuild?.vanityURL) {
if (onlineMemberCount == null && restGuild?.vanityURLCode) {
// For servers with a vanity URL, we can also use the numbers from the invite for online count
const invite = await resolveInvite(pluginData.client, restGuild.vanityURL!, true);
const invite = await resolveInvite(pluginData.client, restGuild.vanityURLCode!, true);
if (invite && inviteHasCounts(invite)) {
onlineMemberCount = invite.presenceCount;
}
}
if (!onlineMemberCount && thisServer) {
onlineMemberCount = thisServer.members.filter(m => m.status !== "offline").length; // Extremely inaccurate fallback
onlineMemberCount = thisServer.members.cache.filter(m => m.presence.status !== "offline").size; // Extremely inaccurate fallback
}
const offlineMemberCount = totalMembers - onlineMemberCount;
let memberCountTotalLines = `Total: **${formatNumber(totalMembers)}**`;
if (restGuild?.maxMembers) {
memberCountTotalLines += `\nMax: **${formatNumber(restGuild.maxMembers)}**`;
if (restGuild?.maximumMembers) {
memberCountTotalLines += `\nMax: **${formatNumber(restGuild.maximumMembers)}**`;
}
let memberCountOnlineLines = `Online: **${formatNumber(onlineMemberCount)}**`;
if (restGuild?.maxPresences) {
memberCountOnlineLines += `\nMax online: **${formatNumber(restGuild.maxPresences)}**`;
if (restGuild?.maximumPresences) {
memberCountOnlineLines += `\nMax online: **${formatNumber(restGuild.maximumPresences)}**`;
}
embed.fields.push({
@ -154,19 +155,19 @@ export async function getServerInfoEmbed(
// CHANNEL COUNTS
if (thisServer) {
const totalChannels = thisServer.channels.size;
const categories = thisServer.channels.filter(channel => channel instanceof CategoryChannel);
const textChannels = thisServer.channels.filter(channel => channel instanceof TextChannel);
const voiceChannels = thisServer.channels.filter(channel => channel instanceof VoiceChannel);
const totalChannels = thisServer.channels.cache.size;
const categories = thisServer.channels.cache.filter(channel => channel instanceof CategoryChannel);
const textChannels = thisServer.channels.cache.filter(channel => channel instanceof TextChannel);
const voiceChannels = thisServer.channels.cache.filter(channel => channel instanceof VoiceChannel);
embed.fields.push({
name: preEmbedPadding + "Channels",
inline: true,
value: trimLines(`
Total: **${totalChannels}** / 500
Categories: **${categories.length}**
Text: **${textChannels.length}**
Voice: **${voiceChannels.length}**
Categories: **${categories.size}**
Text: **${textChannels.size}**
Voice: **${voiceChannels.size}**
`),
});
}
@ -175,7 +176,7 @@ export async function getServerInfoEmbed(
const otherStats: string[] = [];
if (thisServer) {
otherStats.push(`Roles: **${thisServer.roles.size}** / 250`);
otherStats.push(`Roles: **${thisServer.roles.cache.size}** / 250`);
}
if (restGuild) {
@ -186,9 +187,9 @@ export async function getServerInfoEmbed(
2: 150,
3: 250,
}[restGuild.premiumTier] || 50;
otherStats.push(`Emojis: **${restGuild.emojis.length}** / ${maxEmojis * 2}`);
otherStats.push(`Emojis: **${restGuild.emojis.cache.size}** / ${maxEmojis * 2}`);
} else {
otherStats.push(`Emojis: **${guildPreview!.emojis.length}**`);
otherStats.push(`Emojis: **${guildPreview!.emojis.size}**`);
}
if (thisServer) {

View file

@ -14,6 +14,7 @@ import { CaseTypes } from "../../../data/CaseTypes";
import humanizeDuration from "humanize-duration";
import { snowflakeToTimestamp } from "../../../utils/snowflakeToTimestamp";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { MessageEmbedOptions } from "discord.js";
const SNOWFLAKE_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/742020790471491668/snowflake.png";
@ -22,7 +23,7 @@ export async function getSnowflakeInfoEmbed(
snowflake: string,
showUnknownWarning = false,
requestMemberId?: string,
): Promise<EmbedOptions> {
): Promise<MessageEmbedOptions> {
const embed: EmbedWith<"fields"> = {
fields: [],
};

View file

@ -15,13 +15,14 @@ import moment from "moment-timezone";
import { CaseTypes } from "../../../data/CaseTypes";
import humanizeDuration from "humanize-duration";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
import { MessageEmbedOptions, Role } from "discord.js";
export async function getUserInfoEmbed(
pluginData: GuildPluginData<UtilityPluginType>,
userId: string,
compact = false,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
): Promise<MessageEmbedOptions | null> {
const user = await resolveUser(pluginData.client, userId);
if (!user || user instanceof UnknownUser) {
return null;
@ -39,7 +40,7 @@ export async function getUserInfoEmbed(
name: `User: ${user.username}#${user.discriminator}`,
};
const avatarURL = user.avatarURL || user.defaultAvatarURL;
const avatarURL = user.avatarURL() || user.defaultAvatarURL;
embed.author.icon_url = avatarURL;
const createdAt = moment.utc(user.createdAt, "x");
@ -47,7 +48,7 @@ export async function getUserInfoEmbed(
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const accountAge = humanizeDuration(moment.utc().valueOf() - user.createdAt, {
const accountAge = humanizeDuration(moment.utc().valueOf() - user.createdTimestamp, {
largest: 2,
round: true,
});
@ -61,12 +62,12 @@ export async function getUserInfoEmbed(
`),
});
if (member) {
const joinedAt = moment.utc(member.joinedAt, "x");
const joinedAt = moment.utc(member.joinedTimestamp!, "x");
const tzJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
: timeAndDate.inGuildTz(joinedAt);
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedAt, {
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedTimestamp!, {
largest: 2,
round: true,
});
@ -92,16 +93,18 @@ export async function getUserInfoEmbed(
});
if (member) {
const joinedAt = moment.utc(member.joinedAt, "x");
const joinedAt = moment.utc(member.joinedTimestamp!, "x");
const tzJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
: timeAndDate.inGuildTz(joinedAt);
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedAt, {
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedTimestamp!, {
largest: 2,
round: true,
});
const roles = member.roles.map(id => pluginData.guild.roles.cache.get(id)).filter(r => r != null) as Role[];
const roles = member.roles.cache
.map(role => pluginData.guild.roles.cache.get(role.id))
.filter(r => r != null) as Role[];
roles.sort(sorter("position", "DESC"));
embed.fields.push({
@ -112,16 +115,14 @@ export async function getUserInfoEmbed(
`),
});
const voiceChannel = member.voiceState.channelID
? pluginData.guild.channels.cache.get(member.voiceState.channelID)
: null;
if (voiceChannel || member.voiceState.mute || member.voiceState.deaf) {
const voiceChannel = member.voice.channelID ? pluginData.guild.channels.cache.get(member.voice.channelID) : null;
if (voiceChannel || member.voice.mute || member.voice.deaf) {
embed.fields.push({
name: preEmbedPadding + "Voice information",
value: trimLines(`
${voiceChannel ? `Current voice channel: **${voiceChannel ? voiceChannel.name : "None"}**` : ""}
${member.voiceState.mute ? "Server voice muted: **Yes**" : ""}
${member.voiceState.deaf ? "Server voice deafened: **Yes**" : ""}
${member.voice.mute ? "Server voice muted: **Yes**" : ""}
${member.voice.deaf ? "Server voice deafened: **Yes**" : ""}
`),
});
}

View file

@ -1 +1,3 @@
import { TextChannel } from "discord.js";
export const activeReloads: Map<string, TextChannel> = new Map();

View file

@ -1,3 +1,4 @@
import { Guild } from "discord.js";
import { HOURS, noop } from "../../utils";
const MEMBER_REFRESH_FREQUENCY = 1 * HOURS; // How often to do a full member refresh when using commands that need it
@ -9,7 +10,7 @@ export async function refreshMembersIfNeeded(guild: Guild) {
return lastRefresh.promise;
}
const loadPromise = guild.fetchAllMembers().then(noop);
const loadPromise = guild.members.fetch().then(noop);
memberRefreshLog.set(guild.id, {
time: Date.now(),
promise: loadPromise,

View file

@ -14,6 +14,7 @@ import { inputPatternToRegExp, InvalidRegexError } from "../../validatorUtils";
import { asyncFilter } from "../../utils/async";
import Timeout = NodeJS.Timeout;
import { hasDiscordPermissions } from "../../utils/hasDiscordPermissions";
import { Message, User, Constants, TextChannel, GuildMember, Permissions } from "discord.js";
const SEARCH_RESULTS_PER_PAGE = 15;
const SEARCH_ID_RESULTS_PER_PAGE = 50;
@ -90,7 +91,7 @@ export async function displaySearch(
if (originalSearchMsg) {
searchMsgPromise = originalSearchMsg.edit("Searching...");
} else {
searchMsgPromise = msg.channel.createMessage("Searching...");
searchMsgPromise = msg.channel.send("Searching...");
searchMsgPromise.then(m => (originalSearchMsg = m));
}
@ -106,12 +107,12 @@ export async function displaySearch(
}
} catch (e) {
if (e instanceof SearchError) {
sendErrorMessage(pluginData, msg.channel, e.message);
sendErrorMessage(pluginData, msg.channel as TextChannel, e.message);
return;
}
if (e instanceof InvalidRegexError) {
sendErrorMessage(pluginData, msg.channel, e.message);
sendErrorMessage(pluginData, msg.channel as TextChannel, e.message);
return;
}
@ -119,7 +120,7 @@ export async function displaySearch(
}
if (searchResult.totalResults === 0) {
sendErrorMessage(pluginData, msg.channel, "No results found");
sendErrorMessage(pluginData, msg.channel as TextChannel, "No results found");
return;
}
@ -149,7 +150,7 @@ export async function displaySearch(
const embed = await getUserInfoEmbed(pluginData, searchResult.results[0].id, false);
if (embed) {
searchMsg.edit("Only one result:");
msg.channel.createMessage({ embed });
msg.channel.send({ embed });
return;
}
}
@ -160,30 +161,32 @@ export async function displaySearch(
if (searchResult.totalResults > perPage) {
if (!hasReactions) {
hasReactions = true;
searchMsg.addReaction("⬅");
searchMsg.addReaction("➡");
searchMsg.addReaction("🔄");
searchMsg.react("⬅");
searchMsg.react("➡");
searchMsg.react("🔄");
const listenerFn = pluginData.events.on("messageReactionAdd", ({ args: { message: rMsg, emoji, member } }) => {
const listenerFn = pluginData.events.on("messageReactionAdd", async ({ args: { reaction, user } }) => {
const rMsg = reaction.message;
const member = await pluginData.guild.members.fetch(user.id);
if (rMsg.id !== searchMsg.id) return;
if (member.id !== msg.author.id) return;
if (!["⬅", "➡", "🔄"].includes(emoji.name)) return;
if (member.user.id !== msg.author.id) return;
if (!["⬅", "➡", "🔄"].includes(reaction.emoji.name!)) return;
if (emoji.name === "⬅" && currentPage > 1) {
if (reaction.emoji.name === "⬅" && currentPage > 1) {
loadSearchPage(currentPage - 1);
} else if (emoji.name === "➡" && currentPage < searchResult.lastPage) {
} else if (reaction.emoji.name === "➡" && currentPage < searchResult.lastPage) {
loadSearchPage(currentPage + 1);
} else if (emoji.name === "🔄") {
} else if (reaction.emoji.name === "🔄") {
loadSearchPage(currentPage);
}
if (isFullMessage(rMsg)) {
rMsg.removeReaction(emoji.name, member.id);
reaction.remove();
}
});
clearReactionsFn = async () => {
searchMsg.removeReactions().catch(noop);
searchMsg.reactions.removeAll().catch(noop);
pluginData.events.off("messageReactionAdd", listenerFn);
};
}
@ -229,12 +232,12 @@ export async function archiveSearch(
}
} catch (e) {
if (e instanceof SearchError) {
sendErrorMessage(pluginData, msg.channel, e.message);
sendErrorMessage(pluginData, msg.channel as TextChannel, e.message);
return;
}
if (e instanceof InvalidRegexError) {
sendErrorMessage(pluginData, msg.channel, e.message);
sendErrorMessage(pluginData, msg.channel as TextChannel, e.message);
return;
}
@ -242,7 +245,7 @@ export async function archiveSearch(
}
if (results.totalResults === 0) {
sendErrorMessage(pluginData, msg.channel, "No results found");
sendErrorMessage(pluginData, msg.channel as TextChannel, "No results found");
return;
}
@ -260,7 +263,7 @@ export async function archiveSearch(
const baseUrl = getBaseUrl(pluginData);
const url = await pluginData.state.archives.getUrl(baseUrl, archiveId);
await msg.channel.createMessage(`Exported search results: ${url}`);
await msg.channel.send(`Exported search results: ${url}`);
}
async function performMemberSearch(
@ -268,16 +271,16 @@ async function performMemberSearch(
args: MemberSearchParams,
page = 1,
perPage = SEARCH_RESULTS_PER_PAGE,
): Promise<{ results: Member[]; totalResults: number; page: number; lastPage: number; from: number; to: number }> {
): Promise<{ results: GuildMember[]; totalResults: number; page: number; lastPage: number; from: number; to: number }> {
await refreshMembersIfNeeded(pluginData.guild);
let matchingMembers = Array.from(pluginData.guild.members.values());
let matchingMembers = Array.from(pluginData.guild.members.cache.values());
if (args.role) {
const roleIds = args.role.split(",");
matchingMembers = matchingMembers.filter(member => {
for (const role of roleIds) {
if (!member.roles.includes(role)) return false;
if (!member.roles.cache.has(role)) return false;
}
return true;
@ -285,11 +288,11 @@ async function performMemberSearch(
}
if (args.voice) {
matchingMembers = matchingMembers.filter(m => m.voiceState.channelID != null);
matchingMembers = matchingMembers.filter(m => m.voice.channelID != null);
}
if (args.bot) {
matchingMembers = matchingMembers.filter(m => m.bot);
matchingMembers = matchingMembers.filter(m => m.user.bot);
}
if (args.query) {
@ -306,6 +309,7 @@ async function performMemberSearch(
const execRegExp = getOptimizedRegExpRunner(pluginData, isSafeRegex);
/** FIXME if we ever get the intent for this again
if (args["status-search"]) {
matchingMembers = await asyncFilter(matchingMembers, async member => {
if (member.game) {
@ -344,17 +348,18 @@ async function performMemberSearch(
return false;
});
} else {
matchingMembers = await asyncFilter(matchingMembers, async member => {
if (member.nick && (await execRegExp(queryRegex, member.nick).catch(allowTimeout))) {
return true;
}
*/
matchingMembers = await asyncFilter(matchingMembers, async member => {
if (member.nickname && (await execRegExp(queryRegex, member.nickname).catch(allowTimeout))) {
return true;
}
const fullUsername = `${member.user.username}#${member.user.discriminator}`;
if (await execRegExp(queryRegex, fullUsername).catch(allowTimeout)) return true;
const fullUsername = `${member.user.username}#${member.user.discriminator}`;
if (await execRegExp(queryRegex, fullUsername).catch(allowTimeout)) return true;
return false;
});
}
return false;
});
// } FIXME in conjunction with above comment
}
const [, sortDir, sortBy] = (args.sort && args.sort.match(/^(-?)(.*)$/)) ?? [null, "ASC", "name"];
@ -396,11 +401,11 @@ async function performBanSearch(
perPage = SEARCH_RESULTS_PER_PAGE,
): Promise<{ results: User[]; totalResults: number; page: number; lastPage: number; from: number; to: number }> {
const member = pluginData.guild.members.cache.get(pluginData.client.user!.id);
if (member && !hasDiscordPermissions(member.permissions, Constants.Permissions.banMembers)) {
if (member && !hasDiscordPermissions(member.permissions, Permissions.FLAGS.BAN_MEMBERS)) {
throw new SearchError(`Unable to search bans: missing "Ban Members" permission`);
}
let matchingBans = (await pluginData.guild.getBans()).map(x => x.user);
let matchingBans = (await pluginData.guild.bans.fetch({ cache: false })).map(x => x.user);
if (args.query) {
let isSafeRegex = true;
@ -454,14 +459,14 @@ async function performBanSearch(
};
}
function formatSearchResultList(members: Array<Member | User>): string {
function formatSearchResultList(members: Array<GuildMember | User>): string {
const longestId = members.reduce((longest, member) => Math.max(longest, member.id.length), 0);
const lines = members.map(member => {
const paddedId = member.id.padEnd(longestId, " ");
let line;
if (member instanceof Member) {
if (member instanceof GuildMember) {
line = `${paddedId} ${member.user.username}#${member.user.discriminator}`;
if (member.nick) line += ` (${member.nick})`;
if (member.nickname) line += ` (${member.nickname})`;
} else {
line = `${paddedId} ${member.username}#${member.discriminator}`;
}
@ -470,6 +475,6 @@ function formatSearchResultList(members: Array<Member | User>): string {
return lines.join("\n");
}
function formatSearchResultIdList(members: Array<Member | User>): string {
function formatSearchResultIdList(members: Array<GuildMember | User>): string {
return members.map(m => m.id).join(" ");
}