I need help with a discord counting bot. Whatever server it is in, it has the same counter number. what I mean is that they are all paired and do the same count instead of it being server by server.
Here is the main part (not all of it), I just need to learn to have the servers separate from one another:
const Discord = require('discord.js'); const client = new Discord.Client(); var counter = [{ name: "bot", nummer: 0 }]; client.on("message", message => { if (message.channel.name === "counting") { if (!isNaN(message.content)){ var counterlast = counter[counter.length - 1]; var countercheck = counterlast["nummer"] + 1; var pinger = parseInt(message.content); var lastuser = counterlast["name"]; if(countercheck === pinger && lastuser !== message.author.id){ counter.push({name: message.author.id, nummer: countercheck}); message.react('✅'); } else{ if(lastuser === message.author.id){ message.react('❌'); message.reply(`Wrong Number. The new number is **1**.`); counter.length = 0; counter.push({name: "bot", nummer: 0}); Number } else{ message.react('❌'); message.reply(` ruined it at ${countercheck}. the new number is **1**.`); counter.length = 0; counter.push({name: "bot", nummer: 0}); } } } } }); client.login('N/A');
Advertisement
Answer
I’m not sure what this counting bot is or how you store your data. However, you can get the server id from the message by accessing message.guild.id
. It means that you can check this id before you do anything to the server’s “count”.
You can use an object with the server ids as its keys like this:
const counter = { `630930214659670528`: 304, `630136153655430054`: 941, `685932343451250658`: 34123, };
The following code increases the counter of the server it’s accessed from by one:
const counter = {}; client.on('message', (message) => { if (!message.channel.name === 'counting' || isNaN(message.content)) return; // get the server id const { id } = message.guild; counter[id] = counter[id] ? counter[id] + 1 : 1; message.channel.send(`The counter is at ${counter[id]}`); });
Edit: As you’ve updated your question with your code, here’s how I’d add different counters for different servers:
const { Client } = require('discord.js'); const client = new Client(); const counters = {}; client.on('message', (message) => { if ( message.author.bot || message.channel.name !== 'counting' || isNaN(message.content) ) return; const { id } = message.guild; // if this is the first counter from this server set it up if (!counters[id]) { counters[id] = [ { name: 'bot', value: 0, }, ]; } const counter = counters[id]; const last = counter[counter.length - 1]; const increasedValue = last.value + 1; const pingerCount = parseInt(message.content); if (increasedValue === pingerCount && last.name !== message.author.id) { counter.push({ name: message.author.id, value: increasedValue }); message.react('✅'); } else { if (last.name === message.author.id) { message.react('❌'); message.reply( 'Wrong, the last user was also you. The new number is **1**.', ); counter.length = 0; counter.push({ name: 'bot', value: 0 }); } else { message.react('❌'); message.reply(`Ruined it at ${increasedValue}, the new number is **1**.`); counter.length = 0; counter.push({ name: 'bot', value: 0 }); } } });