Bot crashes after running a servercreate command

Hey, I have some problems with one of my commands,. So it should simply create a discord server from a server template , but it's crashing with an error code, i'll provide you. It would be really nice if someone can help. Code:
const { Client, GatewayIntentBits } = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token, clientId, guildId } = require('./config.json');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});

client.once('ready', () => {
console.log('Ready!');
client.user.setActivity('CHAD IS FAT');
});

client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

if (interaction.commandName === 'ping') {
await interaction.reply(`Pong! ${Math.round(client.ws.ping)}ms.`);
} else if (interaction.commandName === 'serversetup') {
const option = interaction.options.getString('template');
let templateLink = '';

if (option === 'Community') {
templateLink = 'https://discord.new/VBRryPvhAk5N';
} else if (option === 'Advertising') {
templateLink = 'https://discord.new/cwczv577CtDe';
}

await interaction.reply(`Loading ${option} template...`);
const guild = await interaction.guild.templates.create({ sourceGuildId: null, name: null, code: templateLink });
await interaction.followUp(`Template loaded successfully! Here is the invite link to the new server: https://discord.gg/${guild.code}`);
}
});

const pingCommand = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with the current Ping!');

const serversetupCommand = new SlashCommandBuilder()
.setName('serversetup')
.setDescription('Setups the Server for you.')
.addStringOption(option =>
option.setName('template')
.setDescription('Choose your Server Topic.')
.setRequired(true)
.addChoices(
{ name: 'Community', value: 'Community' },
{ name: 'Advertising', value: 'Advertising' },
));

const commands = [
pingCommand.toJSON(),
serversetupCommand.toJSON()
];

const rest = new REST({ version: '9' }).setToken(token);

(async () => {
try {
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log('Successfully registered application commands.');
} catch (error) {
console.error(error);
}
})();

client.login(token);
const { Client, GatewayIntentBits } = require('discord.js');
const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token, clientId, guildId } = require('./config.json');

const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});

client.once('ready', () => {
console.log('Ready!');
client.user.setActivity('CHAD IS FAT');
});

client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;

if (interaction.commandName === 'ping') {
await interaction.reply(`Pong! ${Math.round(client.ws.ping)}ms.`);
} else if (interaction.commandName === 'serversetup') {
const option = interaction.options.getString('template');
let templateLink = '';

if (option === 'Community') {
templateLink = 'https://discord.new/VBRryPvhAk5N';
} else if (option === 'Advertising') {
templateLink = 'https://discord.new/cwczv577CtDe';
}

await interaction.reply(`Loading ${option} template...`);
const guild = await interaction.guild.templates.create({ sourceGuildId: null, name: null, code: templateLink });
await interaction.followUp(`Template loaded successfully! Here is the invite link to the new server: https://discord.gg/${guild.code}`);
}
});

const pingCommand = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with the current Ping!');

const serversetupCommand = new SlashCommandBuilder()
.setName('serversetup')
.setDescription('Setups the Server for you.')
.addStringOption(option =>
option.setName('template')
.setDescription('Choose your Server Topic.')
.setRequired(true)
.addChoices(
{ name: 'Community', value: 'Community' },
{ name: 'Advertising', value: 'Advertising' },
));

const commands = [
pingCommand.toJSON(),
serversetupCommand.toJSON()
];

const rest = new REST({ version: '9' }).setToken(token);

(async () => {
try {
await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);

console.log('Successfully registered application commands.');
} catch (error) {
console.error(error);
}
})();

client.login(token);
Error:
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of undefined (reading 'create')
at Client.<anonymous> (C:\Users\user\ServerMaster\bot.js:37:53)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
throw er; // Unhandled 'error' event
^

TypeError: Cannot read properties of undefined (reading 'create')
at Client.<anonymous> (C:\Users\user\ServerMaster\bot.js:37:53)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:394:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
Node.js: v18.15.0 Discord.js: See Tag
34 Replies
d.js toolkit
d.js toolkit2y ago
• What's your exact discord.js npm list discord.js and node node -v version? • Post the full error stack trace, not just the top part! • Show your code! • Explain what exactly your issue is. • Not a discord.js issue? Check out #useful-servers.
d.js docs
d.js docs2y ago
Documentation suggestion for @leteeno:method Guild#createTemplate() Creates a template for the guild.
Cgx
CgxOP2y ago
Not what I need It should create a Server from a Template not a Template of a Server
duck
duck2y ago
then you'd need to fetch the template <Guild>.fetchTemplates() fetches all templates for the given guild, the resulting collection mapped by code <Client>.fetchGuildTemplate() can be used to fetch a single template by code/url
d.js docs
d.js docs2y ago
method Guild#fetchTemplates() Fetches a collection of templates from this guild. Resolves with a collection mapping templates by their method Client#fetchGuildTemplate() Obtains a template from Discord.
Cgx
CgxOP2y ago
Wait now I'm confused.
duck
duck2y ago
<Guild>.templates doesn't exist you'd need to fetch the template you want to use, then call <GuildTemplate>.createGuild()
Cgx
CgxOP2y ago
Ok, I'll try.
Cgx
CgxOP2y ago
so I have to replace this with GuildTemplate.createGuild()?
duck
duck2y ago
not directly you still need to fetch the template first
Cgx
CgxOP2y ago
I'm to dumb for that... Still don't get it
duck
duck2y ago
which part of this are you having trouble with?
Cgx
CgxOP2y ago
I don't really know where to implent that 🥶
duck
duck2y ago
anywhere before you want to create the guild from the template
Cgx
CgxOP2y ago
so like here?
duck
duck2y ago
sure, but you should probably await it and store the result, since you need to get the template from the resulting collection fetching the templates doesn't make <Guild>.template or <Guild>.guildTemplate exist
Cgx
CgxOP2y ago
what?
duck
duck2y ago
I don't know how to explain it more simply <Guild>.template doesn't exist <Guild>.guildTemplate doesn't exist interaction.guildTempalte.templates doesn't exist they will never exist fetchTemplate resolves in a collection of templates
Cgx
CgxOP2y ago
Ok
Cgx
CgxOP2y ago
duck
duck2y ago
you have yet to define guildTemplate but you're getting closer again, fetchTemplates resolves in the collection of fetched templates
Cgx
CgxOP2y ago
hmm ok I don't know how i should define it ... I know...
duck
duck2y ago
if you know, would you like to store it in a variable? 🙂
you should probably await it and store the result, since you need to get the template from the resulting collection
Cgx
CgxOP2y ago
duck
duck2y ago
you should probably await it
Cgx
CgxOP2y ago
duck
duck2y ago
why are you calling it a second time? just call it once, await it, and store that then probably fix your syntax error when calling createGuild
Cgx
CgxOP2y ago
?
duck
duck2y ago
alright I'm gonna suggest you brush up on the basics of javascript before continuing #resources
Cgx
CgxOP2y ago
I know the error was becuase i havent declared the const variable.
duck
duck2y ago
yes but 1. defining and declaring values for variables, constant or not, is incredibly basic in js and programming in general 2. looking at your code, this isn't something you should be having trouble with unless you don't understand anything you've written so far (potentially because it was copypasted) which is why I would suggest you brush up on the basics before continuing so that you can understand what you've written and should write
Cgx
CgxOP2y ago
Ok. I forgot about that... const cannot be edited, let can ok, so i removed the constant and replaced it with "let".
duck
duck2y ago
that wasn't the issue, more so this is sidestepping the issue I'm still going to suggest you brush up on the basics before continuing
Cgx
CgxOP2y ago
doing a udemy course, yep
Want results from more Discord servers?
Add your server