admin管理员组

文章数量:1418022

For some reason the bot is posting in the discord more than once. I am unsure how to fix this at this moment. I've exhausted all options and even restarted the code and the bot itself and I am not to sure where to go on from here.

You can find my code below:

const Discord = require('discord.js');
const fs = require('fs');

const client = new Discord.client();    
const prefix = '-';

clientmands = new Discord.Collection();
    
const mandFiles = fs.readdirSync('./mands/').filter(file => file.endsWith('.js'));

for (const file of mandFiles){
    const mand = require(`./mands/${file}`);
    
    clientmands.set(mand.name, mand);
}
    
client.once('ready', () => {
    console.log('United is online');
});
    
client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot ) return;
    
    const args = message.content.slice(prefix.length).split(/ +/);
    const mand = args.shift().toLowerCase();
    
    if(mand === 'youtube'){
        clientmands.get('youtube').execute(message, args);
    } else if (mand == 'twitter'){
        message.channel.send('');
    }
});

youtube.js

module.exports = {
    name: 'youtube',
    description: "displays youtube channel!",
    execute(message, args){
        message.channel.send('');
    }
}

For some reason the bot is posting in the discord more than once. I am unsure how to fix this at this moment. I've exhausted all options and even restarted the code and the bot itself and I am not to sure where to go on from here.

You can find my code below:

const Discord = require('discord.js');
const fs = require('fs');

const client = new Discord.client();    
const prefix = '-';

client.mands = new Discord.Collection();
    
const mandFiles = fs.readdirSync('./mands/').filter(file => file.endsWith('.js'));

for (const file of mandFiles){
    const mand = require(`./mands/${file}`);
    
    client.mands.set(mand.name, mand);
}
    
client.once('ready', () => {
    console.log('United is online');
});
    
client.on('message', message =>{
    if(!message.content.startsWith(prefix) || message.author.bot ) return;
    
    const args = message.content.slice(prefix.length).split(/ +/);
    const mand = args.shift().toLowerCase();
    
    if(mand === 'youtube'){
        client.mands.get('youtube').execute(message, args);
    } else if (mand == 'twitter'){
        message.channel.send('https://twitter./UnitedPeoplesTV');
    }
});

youtube.js

module.exports = {
    name: 'youtube',
    description: "displays youtube channel!",
    execute(message, args){
        message.channel.send('https://youtube./unitedpeoplestv?sub_confirmation=1');
    }
}
Share Improve this question edited Jan 14, 2021 at 23:16 Zsolt Meszaros 23.2k19 gold badges58 silver badges69 bronze badges asked Jan 14, 2021 at 22:31 JoeGoshJoeGosh 251 silver badge3 bronze badges 3
  • 1 Have you got multiple instances of the bot running? – Pentium1080Ti Commented Jan 14, 2021 at 22:35
  • No, there is only one bot in the discord unless I am missing something. – JoeGosh Commented Jan 14, 2021 at 22:51
  • 2 Try resetting your IDE and Terminal – Elitezen Commented Jan 14, 2021 at 23:19
Add a ment  | 

5 Answers 5

Reset to default 2

I would suggest that you kick the bot from the server, restart your puter (or stop your hosting service - making sure for example if you're using pm2 that it isnt running multiple instances), and try again. Invite the bot back again from the discord applications webpage once you have pleted that.

Make sure there aren't two processes running, Look in task manager and see if there are two Node.js processes running, and if there are then it might be double running. Try ending them and then starting the bot again.

To make sure that there aren't multiple instances, you can reset your token in the discord developer portal inside your application and replace the old token with the new one. After that, you need to restart your bot with the new token.

This will stop all other processes because discord is stopping all connections due to the token reset.

Try adding this line of code:

if (message.author.bot) return;

It check is the author of the message is a bot and returns nothing if it's True.

Found an issue - there was a duplication because of process.env file - where I also stored the bot token, and because of that function worked twice (I exported function to another file and used it there).

本文标签: javascriptMy Discord bot is sending multiple messages at once using discordjsStack Overflow