#client is not defined

2 messages · Page 1 of 1 (latest)

ocean kindle
#
require('dotenv').config();
const { Client, GatewayIntentBits, REST, Routes, EmbedBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');

// יצירת client
const client = new Client({
  intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});

// התחברות לבוט והודעה כשהבוט מוכן
client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

// התחברות לדיסקורד
client.login(process.env.DISCORD_TOKEN)
  .then(() => console.log('Successfully logged in'))
  .catch(error => console.error('Error during client login:', error));

// רישום פקודות עם Discord API
const rest = new REST({ version: '10' }).setToken(process.env.DISCORD_TOKEN);

(async () => {
  try {
    console.log('Started refreshing application (/) commands.');

    await rest.put(Routes.applicationGuildCommands(process.env.CLIENT_ID, process.env.GUILD_ID), {
      body: [
        {
          name: 'giveaway',
          description: 'Create a giveaway',
          options: [
            {
              type: 7, // CHANNEL type
              name: 'channel',
              description: 'The channel where the giveaway will be held',
              required: true,
            },
            {
              type: 4, // INTEGER type
              name: 'duration',
              description: 'Duration of the giveaway in minutes',
              required: true,
            },
            {
              type: 3, // STRING type
              name: 'prize',
              description: 'The prize for the giveaway',
              required: true,
            },
          ],
        },
      ],
    });

    console.log('Successfully reloaded application (/) commands.');
  } catch (error) {
    console.error('Error while registering commands:', error);
  }
})();

// טיפול באינטראקציות (למשל בפקודה /giveaway)
client.on('interactionCreate', async (interaction) => {
  if (!interaction.isCommand()) return;
#

  const { commandName } = interaction;
  console.log(`Received command: ${commandName}`);

  if (commandName === 'giveaway') {
    await interaction.deferReply();

    const channel = interaction.options.getChannel('channel');
    const duration = interaction.options.getInteger('duration');
    const prize = interaction.options.getString('prize');

    if (!channel) {
      await interaction.editReply('Invalid channel specified.');
      return;
    }

    try {
      const giveawayEmbed = new EmbedBuilder()
        .setColor('#FFCC00')
        .setTitle('🎉 Giveaway! 🎉')
        .setDescription(`**Prize:** ${prize}\n**Duration:** ${duration} minutes\n**Channel:** ${channel}\n\nClick the button below to enter!`)
        .setTimestamp()
        .setFooter({ text: 'Good luck to everyone!' });

      const entryButton = new ButtonBuilder()
        .setCustomId('entry_button')
        .setLabel('🎉 Enter')
        .setStyle(ButtonStyle.Primary);

      await channel.send({ 
        embeds: [giveawayEmbed],
        components: [{ type: 1, components: [entryButton] }] 
      });

      await interaction.editReply(`Giveaway created in ${channel} for ${duration} minutes with prize: ${prize}`);
    } catch (error) {
      await interaction.editReply('Failed to send giveaway message.');
      console.error('Failed to send giveaway message:', error);
    }
  }
});

// טיפול באינטראקציות של לחצנים
client.on('interactionCreate', async (interaction) => {
  if (interaction.isButton() && interaction.customId === 'entry_button') {
    try {
      await interaction.reply({ content: 'You have entered the giveaway!', ephemeral: true });
    } catch (error) {
      console.error('Error handling button interaction:', error);
    }
  }
});