Home > front end >  Discord.JS v13 Search a List for a specific key word
Discord.JS v13 Search a List for a specific key word

Time:07-03

I wanted to write a command to check if a specific user is part of a ban list in my server. Unfortunately it always does the stuff in the else loop. I hope you can help.

const Discord = require('discord.js');
const ms = require('ms');
const { Permissions } = require("discord.js");
const { MessageEmbed } = require("discord.js");
//const config = require("./config.json");


module.exports = {
    name: 'check',
    category: "Moderation",
    description: 'Checkt ob ein User gebannt ist',

    run: async(client,message,args,guild) => {
        let arg = message.content.split(" ")
        let banlist = client.channels.cache.get('CHANNELID')
        let check = arg[1]

        message.delete()

        if (!arg[1]) {
            return message.channel.send({content :"Please specify a user to check!"}).then((message) => setTimeout(() => message.delete(), 10000)).catch(err => {
                console.error(err);
            });
        }

        banlist.messages.fetch({limit : 30}).then(messages => {
            let positive = messages.filter(msgs => msgs.content.includes(check))
            if(!positive) {
                message.channel.send({content :"This user is not banned!"}).then((message) => setTimeout(() => message.delete(), 10000)).catch(err => {
                    console.error(err); 
                });
            }
            else {
                message.channel.send({content :`This user is banned.`}).then((message) => setTimeout(() => message.delete(), 10000)).catch(err => {
                    console.error(err); 
                });
            }
        })


        }
};

I know there are methods to check the servers banned user list but not everyone on the list in the channel is banned on discord. So I can't do that. To sum that up. I tried to do a command, which checks a channel for a specific key word and returns differing statements, whether he found that key word or not.

CodePudding user response:

filter() returns an empty array if it finds nothing. You can use:

if(positive.length === 0) {
// ...
}
else {
// ...
}

CodePudding user response:

Since you're looking for a boolean, use Array.some()

const positive = messages.some(msg => msg.content.includes(check));

.filter() is intended for returning a new array

  • Related