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);


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)


Node.js: v18.15.0
Discord.js: See Tag
Was this page helpful?