Reputation: 19
So, I'm creating a discord bot using node.js. I need help making server specific variables (that cant be used on other servers or could be differnet)
I searched for something and the only result was to track my variables by guild id. The problem is it uses 'message.guild.id' so that means i can't use it before checking for messages.
This is how the code to setting the prefix should look:
function setPrefix(message, arguments){
prefix[message.guild.id] = arguments
}
And this is how I wanted to set it at the start of the code:
prefix = {}
prefix[guild.id] = "@"
I tried it, but, of course, i can't get the guild id like that so it would be undefined every time i tried. What could I do? (please note im not very experienced)
Upvotes: 0
Views: 3546
Reputation: 2161
You will find the list of all the guilds your bot is a member of in <Client>.guilds
. You just need to iterate over them and set the prefix you want.
Quick snippet to set the prefix for every guild Id the bot is in to "@":
var Discord = require("discord.js");
var client = new Discord.Client();
var prefix = {};
var defaultPrefix = "@";
client.on("ready", function(){
var allGuilds = client.guilds;
//initialize all prefixes to a default prefix
allGuilds.tap(function(guild){
prefix[guild.id] = defaultPrefix;
});
});
client.login("BOT_TOKEN")
Upvotes: 2