Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

javascript - how to use two parameters in one function in discord js?

how to use two parameters in one function for example I want to use message and member for this function I got this problem a lot I used to make another function but because const is closed scope function I couldn't do that.

client.on('message', giveGif);

async function giveGif(msg, member) {
 
    let space = msg.content.split(" ");
        if(space[0] === prefix + "gif") {
            let keywords = "find gifs";
            if(space.length > 1){
                keywords = space.slice(1, space.length).join(" ");
            }
            let url = `https://api.tenor.com/v1/search?q=${keywords}&key=${process.env.TENOR_API_KEY}&contentfilter=high`;
            let response = await fetch(url);
            let json = await response.json();
            const index = Math.floor(Math.random() * json.results.length);
            let result = json.results[index]
                ? json.results[index].url
                : "No gif was found with your search....";
                const embed = new Discord.MessageEmbed();
                embed.setTitle(`Gif bot`);
                embed.setDescription(`<@${member.id}> asked for ${keywords}`)
               // embed.addField(`<@author> asked for ${keywords}`, result);
                embed.setImage(result);
                embed.setColor('RANDOM');
                embed.setFooter('this bot is provided by by me');
                embed.setTimestamp();           
            msg.guild.channels.cache.find(i => i.name === 'gifs').send(embed);
    }
}
question from:https://stackoverflow.com/questions/65846841/how-to-use-two-parameters-in-one-function-in-discord-js

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

msg holds the parameter member already, as mentioned above by @Y. Gherbi.

To access member, simply do message.member

e.g.

//get members that have admin role
const memberRoles = message.member.roles.has(role => role.name === 'ADMINISTRATOR')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...