简体   繁体   中英

How do I make my discord bot copy and message every user written message?

So I am new to Javascript and Discord bots. I have a stupid idea for a bot. Mostly just to annoy my friends. But the basic idea is that it will copy their message and respond with their message. I am having a problem where I cannot get the bot to respond to more than one message.

Here is my code:

const Discord = require('discord.js');
const client = new Discord.Client();

// Logs into discord with the app's token
client.login('...');

// when logged in succesfully this code will run once
client.once('ready', () => {
    console.log('Ready!');
});

if(client.on) {
    client.once('message', function (message) {
        // Send message back on the same channel
        message.channel.send(message.content);
    }); 
}

I am aware that having client.once only sends the message once but if I were to change that to client.on it would send the same message to an infinite amount. Any help would be appreciated. Thanks.

You just need to prevent the bot from responding to itself.

client.on("message", (message) => {

    // Use this if you don't want the bot to respond to itself
    if (message.author.id == client.user.id) return;

    // Use this if you don't want the bot to respond to other bots (including itself)
    if (message.author.bot) return;

    message.channel.send(message.content);

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM