mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-21 00:35:02 +00:00
perf(arrays): Improved array looping
This commit is contained in:
parent
edd78fc9c6
commit
f9520ab434
12 changed files with 45 additions and 37 deletions
|
@ -42,9 +42,9 @@ export class QueuedEventEmitter {
|
||||||
const listeners = [...(this.listeners.get(eventName) || []), ...(this.listeners.get("*") || [])];
|
const listeners = [...(this.listeners.get(eventName) || []), ...(this.listeners.get("*") || [])];
|
||||||
|
|
||||||
let promise: Promise<any> = Promise.resolve();
|
let promise: Promise<any> = Promise.resolve();
|
||||||
listeners.forEach(listener => {
|
for (let i = 0; i < listeners.length; ++i) {
|
||||||
promise = this.queue.add(listener.bind(null, ...args));
|
promise = this.queue.add(listeners[i].bind(null, ...args));
|
||||||
});
|
}
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,11 +11,11 @@ export async function getCaseTypeAmountForUserId(
|
||||||
let typeAmount = 0;
|
let typeAmount = 0;
|
||||||
|
|
||||||
if (cases.length > 0) {
|
if (cases.length > 0) {
|
||||||
cases.forEach(singleCase => {
|
for (let i = 0; i < cases.length; ++i) {
|
||||||
if (singleCase.type === type.valueOf()) {
|
if (cases[i].type === type.valueOf()) {
|
||||||
typeAmount++;
|
typeAmount++;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return typeAmount;
|
return typeAmount;
|
||||||
|
|
|
@ -5,8 +5,8 @@ export const GuildBanRemoveAlertsEvt = locateUserEvt({
|
||||||
|
|
||||||
async listener(meta) {
|
async listener(meta) {
|
||||||
const alerts = await meta.pluginData.state.alerts.getAlertsByUserId(meta.args.user.id);
|
const alerts = await meta.pluginData.state.alerts.getAlertsByUserId(meta.args.user.id);
|
||||||
alerts.forEach(alert => {
|
for (let i = 0; i < alerts.length; ++i) {
|
||||||
meta.pluginData.state.alerts.delete(alert.id);
|
meta.pluginData.state.alerts.delete(alerts[i].id);
|
||||||
});
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -29,11 +29,12 @@ export const ChannelLeaveAlertsEvt = locateUserEvt({
|
||||||
const triggeredAlerts = await meta.pluginData.state.alerts.getAlertsByUserId(meta.args.member.id);
|
const triggeredAlerts = await meta.pluginData.state.alerts.getAlertsByUserId(meta.args.member.id);
|
||||||
const voiceChannel = meta.args.oldChannel as VoiceChannel;
|
const voiceChannel = meta.args.oldChannel as VoiceChannel;
|
||||||
|
|
||||||
triggeredAlerts.forEach(alert => {
|
for (let i = 0; i < triggeredAlerts.length; ++i) {
|
||||||
|
const alert = triggeredAlerts[i];
|
||||||
const txtChannel = meta.pluginData.client.getChannel(alert.channel_id) as TextableChannel;
|
const txtChannel = meta.pluginData.client.getChannel(alert.channel_id) as TextableChannel;
|
||||||
txtChannel.createMessage(
|
txtChannel.createMessage(
|
||||||
`🔴 <@!${alert.requestor_id}> the user <@!${alert.user_id}> disconnected out of \`${voiceChannel.name}\``,
|
`🔴 <@!${alert.requestor_id}> the user <@!${alert.user_id}> disconnected out of \`${voiceChannel.name}\``,
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,9 +4,9 @@ import { LocateUserPluginType } from "../types";
|
||||||
export async function fillActiveAlertsList(pluginData: GuildPluginData<LocateUserPluginType>) {
|
export async function fillActiveAlertsList(pluginData: GuildPluginData<LocateUserPluginType>) {
|
||||||
const allAlerts = await pluginData.state.alerts.getAllGuildAlerts();
|
const allAlerts = await pluginData.state.alerts.getAllGuildAlerts();
|
||||||
|
|
||||||
allAlerts.forEach(alert => {
|
for (let i = 0; i < allAlerts.length; ++i) {
|
||||||
if (!pluginData.state.usersWithAlerts.includes(alert.user_id)) {
|
if (!pluginData.state.usersWithAlerts.includes(allAlerts[i].user_id)) {
|
||||||
pluginData.state.usersWithAlerts.push(alert.user_id);
|
pluginData.state.usersWithAlerts.push(allAlerts[i].user_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,13 @@ export async function sendAlerts(pluginData: GuildPluginData<LocateUserPluginTyp
|
||||||
const member = await resolveMember(pluginData.client, pluginData.guild, userId);
|
const member = await resolveMember(pluginData.client, pluginData.guild, userId);
|
||||||
if (!member) return;
|
if (!member) return;
|
||||||
|
|
||||||
triggeredAlerts.forEach(alert => {
|
for (let i = 0; i < triggeredAlerts.length; ++i) {
|
||||||
|
const alert = triggeredAlerts[i];
|
||||||
const prepend = `<@!${alert.requestor_id}>, an alert requested by you has triggered!\nReminder: \`${alert.body}\`\n`;
|
const prepend = `<@!${alert.requestor_id}>, an alert requested by you has triggered!\nReminder: \`${alert.body}\`\n`;
|
||||||
const txtChannel = pluginData.client.getChannel(alert.channel_id) as TextableChannel;
|
const txtChannel = pluginData.client.getChannel(alert.channel_id) as TextableChannel;
|
||||||
sendWhere(pluginData, member, txtChannel, prepend);
|
sendWhere(pluginData, member, txtChannel, prepend);
|
||||||
if (alert.active) {
|
if (alert.active) {
|
||||||
moveMember(pluginData, alert.requestor_id, member, txtChannel);
|
moveMember(pluginData, alert.requestor_id, member, txtChannel);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,8 @@ export const MassbanCmd = modActionsCmd({
|
||||||
const banReason = formatReasonWithAttachments(banReasonReply.content, msg.attachments);
|
const banReason = formatReasonWithAttachments(banReasonReply.content, msg.attachments);
|
||||||
|
|
||||||
// Verify we can act on each of the users specified
|
// Verify we can act on each of the users specified
|
||||||
for (const userId of args.userIds) {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
|
const userId = args.userIds[i];
|
||||||
const member = pluginData.guild.members.get(userId); // TODO: Get members on demand?
|
const member = pluginData.guild.members.get(userId); // TODO: Get members on demand?
|
||||||
if (member && !canActOn(pluginData, msg.member, member)) {
|
if (member && !canActOn(pluginData, msg.member, member)) {
|
||||||
sendErrorMessage(pluginData, msg.channel, "Cannot massban one or more users: insufficient permissions");
|
sendErrorMessage(pluginData, msg.channel, "Cannot massban one or more users: insufficient permissions");
|
||||||
|
@ -52,11 +53,11 @@ export const MassbanCmd = modActionsCmd({
|
||||||
|
|
||||||
// Ignore automatic ban cases and logs for these users
|
// Ignore automatic ban cases and logs for these users
|
||||||
// We'll create our own cases below and post a single "mass banned" log instead
|
// We'll create our own cases below and post a single "mass banned" log instead
|
||||||
args.userIds.forEach(userId => {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
// Use longer timeouts since this can take a while
|
// Use longer timeouts since this can take a while
|
||||||
ignoreEvent(pluginData, IgnoredEventType.Ban, userId, 120 * 1000);
|
ignoreEvent(pluginData, IgnoredEventType.Ban, args.userIds[i], 120 * 1000);
|
||||||
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_BAN, userId, 120 * 1000);
|
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_BAN, args.userIds[i], 120 * 1000);
|
||||||
});
|
}
|
||||||
|
|
||||||
// Show a loading indicator since this can take a while
|
// Show a loading indicator since this can take a while
|
||||||
const loadingMsg = await msg.channel.createMessage("Banning...");
|
const loadingMsg = await msg.channel.createMessage("Banning...");
|
||||||
|
|
|
@ -41,11 +41,11 @@ export const MassunbanCmd = modActionsCmd({
|
||||||
|
|
||||||
// Ignore automatic unban cases and logs for these users
|
// Ignore automatic unban cases and logs for these users
|
||||||
// We'll create our own cases below and post a single "mass unbanned" log instead
|
// We'll create our own cases below and post a single "mass unbanned" log instead
|
||||||
args.userIds.forEach(userId => {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
// Use longer timeouts since this can take a while
|
// Use longer timeouts since this can take a while
|
||||||
ignoreEvent(pluginData, IgnoredEventType.Unban, userId, 120 * 1000);
|
ignoreEvent(pluginData, IgnoredEventType.Unban, args.userIds[i], 120 * 1000);
|
||||||
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_UNBAN, userId, 120 * 1000);
|
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_UNBAN, args.userIds[i], 120 * 1000);
|
||||||
});
|
}
|
||||||
|
|
||||||
// Show a loading indicator since this can take a while
|
// Show a loading indicator since this can take a while
|
||||||
const loadingMsg = await msg.channel.createMessage("Unbanning...");
|
const loadingMsg = await msg.channel.createMessage("Unbanning...");
|
||||||
|
@ -53,7 +53,8 @@ export const MassunbanCmd = modActionsCmd({
|
||||||
// Unban each user and count failed unbans (if any)
|
// Unban each user and count failed unbans (if any)
|
||||||
const failedUnbans: Array<{ userId: string; reason: UnbanFailReasons }> = [];
|
const failedUnbans: Array<{ userId: string; reason: UnbanFailReasons }> = [];
|
||||||
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
const casesPlugin = pluginData.getPlugin(CasesPlugin);
|
||||||
for (const userId of args.userIds) {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
|
const userId = args.userIds[i];
|
||||||
if (!(await isBanned(pluginData, userId))) {
|
if (!(await isBanned(pluginData, userId))) {
|
||||||
failedUnbans.push({ userId, reason: UnbanFailReasons.NOT_BANNED });
|
failedUnbans.push({ userId, reason: UnbanFailReasons.NOT_BANNED });
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -42,7 +42,8 @@ export const MassmuteCmd = modActionsCmd({
|
||||||
const muteReason = formatReasonWithAttachments(muteReasonReceived.content, msg.attachments);
|
const muteReason = formatReasonWithAttachments(muteReasonReceived.content, msg.attachments);
|
||||||
|
|
||||||
// Verify we can act upon all users
|
// Verify we can act upon all users
|
||||||
for (const userId of args.userIds) {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
|
const userId = args.userIds[i];
|
||||||
const member = pluginData.guild.members.get(userId);
|
const member = pluginData.guild.members.get(userId);
|
||||||
if (member && !canActOn(pluginData, msg.member, member)) {
|
if (member && !canActOn(pluginData, msg.member, member)) {
|
||||||
sendErrorMessage(pluginData, msg.channel, "Cannot massmute one or more users: insufficient permissions");
|
sendErrorMessage(pluginData, msg.channel, "Cannot massmute one or more users: insufficient permissions");
|
||||||
|
@ -52,10 +53,10 @@ export const MassmuteCmd = modActionsCmd({
|
||||||
|
|
||||||
// Ignore automatic mute cases and logs for these users
|
// Ignore automatic mute cases and logs for these users
|
||||||
// We'll create our own cases below and post a single "mass muted" log instead
|
// We'll create our own cases below and post a single "mass muted" log instead
|
||||||
args.userIds.forEach(userId => {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
// Use longer timeouts since this can take a while
|
// Use longer timeouts since this can take a while
|
||||||
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_MUTE, userId, 120 * 1000);
|
pluginData.state.serverLogs.ignoreLog(LogType.MEMBER_MUTE, args.userIds[i], 120 * 1000);
|
||||||
});
|
}
|
||||||
|
|
||||||
// Show loading indicator
|
// Show loading indicator
|
||||||
const loadingMsg = await msg.channel.createMessage("Muting...");
|
const loadingMsg = await msg.channel.createMessage("Muting...");
|
||||||
|
@ -64,7 +65,8 @@ export const MassmuteCmd = modActionsCmd({
|
||||||
const modId = msg.author.id;
|
const modId = msg.author.id;
|
||||||
const failedMutes: string[] = [];
|
const failedMutes: string[] = [];
|
||||||
const mutesPlugin = pluginData.getPlugin(MutesPlugin);
|
const mutesPlugin = pluginData.getPlugin(MutesPlugin);
|
||||||
for (const userId of args.userIds) {
|
for (let i = 0; i < args.userIds.length; ++i) {
|
||||||
|
const userId = args.userIds[i];
|
||||||
try {
|
try {
|
||||||
await mutesPlugin.muteUser(userId, 0, `Mass mute: ${muteReason}`, {
|
await mutesPlugin.muteUser(userId, 0, `Mass mute: ${muteReason}`, {
|
||||||
caseArgs: {
|
caseArgs: {
|
||||||
|
|
|
@ -52,10 +52,10 @@ export const MutesCmd = mutesCmd({
|
||||||
const muteRole = pluginData.config.get().mute_role;
|
const muteRole = pluginData.config.get().mute_role;
|
||||||
|
|
||||||
if (muteRole) {
|
if (muteRole) {
|
||||||
pluginData.guild.members.forEach(member => {
|
for (const member of pluginData.guild.members.values()) {
|
||||||
if (muteUserIds.has(member.id)) return;
|
if (muteUserIds.has(member.id)) return;
|
||||||
if (member.roles.includes(muteRole)) manuallyMutedMembers.push(member);
|
if (member.roles.includes(muteRole)) manuallyMutedMembers.push(member);
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
totalMutes = manuallyMutedMembers.length;
|
totalMutes = manuallyMutedMembers.length;
|
||||||
|
|
|
@ -129,7 +129,8 @@ export const CleanCmd = utilityCmd({
|
||||||
if (potentialMessagesToClean.length === 0) break;
|
if (potentialMessagesToClean.length === 0) break;
|
||||||
|
|
||||||
const filtered: SavedMessage[] = [];
|
const filtered: SavedMessage[] = [];
|
||||||
for (const message of potentialMessagesToClean) {
|
for (let i = 0; i < potentialMessagesToClean.length; ++i) {
|
||||||
|
const message = potentialMessagesToClean[i];
|
||||||
const contentString = message.data.content || "";
|
const contentString = message.data.content || "";
|
||||||
if (args.user && message.user_id !== args.user) continue;
|
if (args.user && message.user_id !== args.user) continue;
|
||||||
if (args.bots && !message.is_bot) continue;
|
if (args.bots && !message.is_bot) continue;
|
||||||
|
|
|
@ -35,7 +35,8 @@ function newTemplateVar(): ITemplateVar {
|
||||||
type ParsedTemplate = Array<string | ITemplateVar>;
|
type ParsedTemplate = Array<string | ITemplateVar>;
|
||||||
|
|
||||||
function cleanUpParseResult(arr) {
|
function cleanUpParseResult(arr) {
|
||||||
arr.forEach(item => {
|
for (let i = 0; i < arr.length; ++i) {
|
||||||
|
const item = arr[i];
|
||||||
if (typeof item === "object") {
|
if (typeof item === "object") {
|
||||||
delete item._state;
|
delete item._state;
|
||||||
delete item._parent;
|
delete item._parent;
|
||||||
|
@ -43,7 +44,7 @@ function cleanUpParseResult(arr) {
|
||||||
cleanUpParseResult(item.args);
|
cleanUpParseResult(item.args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseTemplate(str: string): ParsedTemplate {
|
export function parseTemplate(str: string): ParsedTemplate {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue