1

i get error

let executor = await this.members.fetch(executorID);
                               ^^^^^

SyntaxError: await is only valid in async function

when using the code below (use is to check if user breaks any of set filters and if so remove roles or ban user whatever they set option to)

ive tried my best to lable what parts of code does please not english isnt my first language

ive only recieved this error since trying to add a check whitelist feature - everything else works without the whitelist check code

without the code for whitelist the code works and performs as intended and the whitelist code succesfully logs ids for that guild

if(whitelisted && whitelisted.length) {
              whitelisted.forEach(x => {
              if (executorID === x.user) return;
const { Structures } = require('discord.js');
let whitelisted = db.get(`whitelist_${message.guild.id}`)
const { limits, defaultPrefix } = require('../config.js');

Structures.extend('Guild', Guild => {
    class GuildExt extends Guild {
        constructor(...args) {
            super(...args);
        }

        get prefix() {
            return this.get('prefix', defaultPrefix);
        }

        get(key, fallback) {
            return this.client.db.get(`${this.id}_${key}`) || fallback;
        }

        set(key, data) {
            return this.client.db.set(`${this.id}_${key}`, data);
        }

        delete(key) {
            return this.client.db.delete(`${this.id}_${key}`);
        }

        resolveChannel(channelID) {
            const channel = this.channels.cache.get(channelID);
            return channel;
        }

        get limits() {
            var obj = {};
            for (var k in limits) {
                obj[k] = {
                    minute: this.get(
                        `limits.${k}.minute`,
                        limits[k].per_minute
                    ),
                    hour: this.get(`limits.${k}.hour`, limits[k].per_hour)
                };
            }
            return obj;
        }

        getActions(limit = 10, filter = () => true) {
            var obj = {};
            var l = limits;
            for (var k in limits) {
                obj[k] = {
                    name: this.client.Utils.toProperCase(k),
                    actions: this.client.Utils.convertEntries(
                        [
                            ...this.get(
                                this.client.Utils.convertLimitNameToActionType(
                                    k
                                ),
                                []
                            ),
                            ...this.get(
                                `archive.${this.client.Utils.convertLimitNameToActionType(
                                    k
                                )}`,
                                []
                            )
                        ]
                            .filter(filter)
                            .slice(0, limit)
                    )
                };
            }
            return obj;
        }

        find_entry(action, filter) {
            let guild = this;
            return new Promise(resolve => {
                (async function search(iter) {
                    //console.log(`ACTION = ${action} | ITER = ${iter}`);

                    if (!guild.me) return resolve(null);

                    if (guild.me.hasPermission('VIEW_AUDIT_LOG')) {
                        let logs = await guild.fetchAuditLogs({
                            limit: 10,
                            type: action
                        });
                        let entries = logs.entries;
                        let entry = null;

                        entries = entries.filter(filter);

                        for (var e of entries)
                            if (!entry || e[0] > entry.id) entry = e[1];

                        if (entry) return resolve(entry);
                    }

                    if (++iter === 5) return resolve(null);
                    else return setTimeout(search, 200, iter);
                })(0);
            });
        }

        push_entry(entry, displayName) {
            const action = ['MEMBER_KICK', 'MEMBER_BAN_ADD'].includes(
                entry.action
            )
                ? 'MEMBER_REMOVE'
                : entry.action;
            const oneHourAgo = Date.now() - 1000 * 60 * 60;

            // Fetch Entries for a sepcific action (Last Hour)
            let entries = this.get(action, []);

            // Filter entries older than one hour to a new variable
            let olderThanOneHour = entries.filter(
                i => !(i.timestamp > oneHourAgo)
            );

            // Prepend entries older than one hour to the archive
            if (olderThanOneHour.length > 0)
                this.set(`archive.${action}`, [
                    ...olderThanOneHour,
                    ...this.get(`archive.${action}`, [])
                ]);

            // Filter entries older than one hour from old variable
            entries = entries.filter(i => i.timestamp > oneHourAgo);

            // Prepend new entry if not already found
            if (
                !entries.find(
                    i =>
                        i.target.id === entry.target.id &&
                        i.executor.id === entry.executor.id
                )
            )
                entries.unshift({
                    timestamp: entry.createdTimestamp,
                    action: entry.action,
                    target: {
                        id: entry.target.id,
                        displayName,
                        targetType: entry.targetType
                    },
                    executor: {
                        id: entry.executor.id,
                        displayName: entry.executor.tag
                    }
                });

            // Update entries newer than one hour
            return this.set(action, entries);
        }

        async check_limits(entries, executorID, configAction) {
            // Ignore if executor is the owner or is whitelisted
            if (executorID === this.ownerID) return;

         if(whitelisted && whitelisted.length) {
              whitelisted.forEach(x => {
              if (executorID === x.user) retrun;

            // Filter actions relating to executor
            const oneMinuteAgo = Date.now() - 1000 * 60;
            let executorActionsHour = entries.filter(
                i => i.executor.id === executorID
            );
            let executorActionsMinute = executorActionsHour.filter(
                i => i.timestamp > oneMinuteAgo
            );
            console.log(
                `${configAction}/${executorID}: LAST_HOUR: ${executorActionsHour.length} LAST_MINUTE: ${executorActionsMinute.length} `
            );

            let limits = this.limits;

            let limitReached = null;
            if (executorActionsHour.length >= limits[configAction].hour)
                limitReached = 'Hour';
            if (executorActionsMinute.length >= limits[configAction].minute)
                limitReached = 'Minute';

            // Check if the amount of actions is greater than or equal to the limit
            if (limitReached) {
                // Remove all of the executor's roles
                let executor = await this.members.fetch(executorID);
                executor.roles.remove(executor.roles.cache);

                // Handle managed roles
                let managed = executor.roles.cache
                    .filter(r => r.managed)
                    .array();
                for (var i = 0; i < managed.length; i++)
                    managed[i].setPermissions(0, 'Guardian Action');

                // Notify owner, executor, and logging channel
                const embed = this.client.util
                    .embed()
                    .setTitle(`Limit Reached - ${limitReached}`)
                    .setDescription(
                        this.client.Utils.convertEntries(
                            limitReached === 'Hour'
                                ? executorActionsHour
                                : executorActionsMinute
                        )
                    )
                    .setColor(0x7289da);

                await this.owner.send(
                    embed.setFooter(
                        "This message was sent to you because you're the Guild owner."
                    )
                );
                await executor.send(
                    embed.setFooter(
                        'This message was sent to you because you were the executor.'
                    )
                );

                const loggingChannel = this.resolveChannel(
                    this.get(`loggingChannelID`)
                );
                if (loggingChannel)
                    await loggingChannel.send(embed.setFooter(''));
            }
    })
    }
        }
    }
    return GuildExt;
});

i am new to JS and any help would be appreciated please dont hate if i do have bad syntax !! i am new - sorry if i dont get things the first time

Pxndaaa
  • 13
  • 4

2 Answers2

1

You forgot to make your forEach function async, just change it to:

/* ... */
whitelisted.forEach(async (x) => {
    /* ... */
    let executor = await this.members.fetch(executorID);
    /* ... */
}
/* ... */

Not part of your question but you misspelled return here

if (executorID === x.user) retrun;
IRONM00N
  • 1,105
  • 4
  • 17
0

Your line

let executor = await this.members.fetch(executorID);

is inside a non-async anonymous function in:

if (whitelisted && whitelisted.length) {
            whitelisted.forEach(x => { // <- This line
                if (executorID === x.user) return;
                    // ...

Try changing it with:

if (whitelisted && whitelisted.length) {
            whitelisted.forEach(async (x) => { // <- Use async here
                if (executorID === x.user) return;
                    // ...

Also, avoid using forEach to make asynchronous calls.

MrMythical
  • 8,908
  • 2
  • 17
  • 45
forensor
  • 81
  • 4