Reputation: 11
I'm making a discord bot in Js and i ran into this problem where "guild" isn't defined. I've made discord.js bots before similar to this but i never ran into a error like this:
.setTitle(`Server infomation on ${guild.name}`)
^
ReferenceError: guild is not defined
at Object.<anonymous> (/home/.Ulur/Downloads/Discordia/index.js:15:36)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)```
And my bot code:
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs
.readdirSync('./commands')
.filter((file) => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// END OF CONSTANTS
const ServerInfo = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(`Server infomation on ${guild.name}`)
.setDescription(`Command executed by ${message.author.tag}`)
.addFields(
{ name: 'Members', value: `${guild.memberCount}` },
{ name: 'Region', value: `${guild.region}` },
{ name: 'Owner', value: `${guild.owner.tag}`, inline: true },
{ name: 'Created', value: `${guild.createdAt}`, inline: true }
);
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on('message', (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content
.slice(prefix.length)
.trim()
.split(/ +/);
const command = args.shift().toLowerCase();
if (!client.commands.has(command)) return;
try {
client.commands.get(command).execute(message, args);
console.log(`Executing Command`, `${prefix}${command}`);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login(token);
Upvotes: 1
Views: 16462
Reputation: 84
You have to define guild using: var guild = client.guilds.cache.get('YOUR_GUILD_ID')
For the message, you want to do: var guild = message.guild
Look the this documentation about guilds to figure out what property of the guild you want to display: https://discord.js.org/#/docs/main/stable/class/Guild
Hope this helps!
Upvotes: 0
Reputation: 502
You might want to get the guild object using:
const guild = client.guilds.cache.get("YOUR_GUILD_ID");
To get the guild from the message and use your code (only an example as this sends the embed every message):
client.on('message', message => {
let guild = message.guild;
const ServerInfo = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(`Server infomation on ${guild.name}`)
.setDescription(`Command executed by ${message.author.tag}`)
.addFields(
{ name: 'Members', value: `${guild.memberCount}` },
{ name: 'Region', value: `${guild.region}` },
{ name: 'Owner', value: `${guild.owner.tag}`, inline: true },
{ name: 'Created', value: `${guild.createdAt}`, inline: true },
)
});
Upvotes: 2