#Discord bot not working

15 messages · Page 1 of 1 (latest)

lament igloo
#

I changed my IDE to VCS node.js for mu discord bot, but I still getting errors: SyntaxError: Identifier 'Discord' has already been declared
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1177:20)
at Module._compile (node:internal/modules/cjs/loader:1221:27)
at Module._extensions..js (node:internal/modules/cjs/loader:1311:10)
at Module.load (node:internal/modules/cjs/loader:1115:32)
at Module._load (node:internal/modules/cjs/loader:962:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47

#
const Discord = require('discord.js');
const ytdl = require('ytdl-core');
const { YTSearcher } = require('ytsearcher');
const config = require('./config.json');

// Create a new Discord client with intents
const client = new Discord.Client({ intents: [Discord.Intents.GUILDS, Discord.Intents.GUILD_MESSAGES, Discord.Intents.GUILD_VOICE_STATES] });

// Create a new YTSearcher instance
const searcher = new YTSearcher(config.youtubeAPIKey);

// When the client is ready, run this code
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

// When a message is received, run this code
client.on('messageCreate', async (message) => {
  // Check if the message starts with the specified prefix and is not from a bot
  if (!message.content.startsWith(config.prefix) || message.author.bot) return; ```
#
  const args = message.content.slice(config.prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();

  // Check the command
  if (command === 'play') {
    // Check if the user is in a voice channel
    if (!message.member.voice.channel) {
      return message.reply('You need to be in a voice channel to use this command.');
    }

    // Check if the bot has permission to join and speak in the voice channel
    if (!message.member.voice.channel.permissionsFor(message.guild.me).has('CONNECT') ||
        !message.member.voice.channel.permissionsFor(message.guild.me).has('SPEAK')) {
      return message.reply('I need the permissions to join and speak in your voice channel.');
    }

    // Check if a search query is provided
    if (!args.length) {
      return message.reply('You need to provide a search query.');
    }

    // Get the search results
    const result = await searcher.search(args.join(' '), { type: 'video' });

    // Get the first video from the search results
    const video = result.first;

    // Check if a video is found
    if (!video) {
      return message.reply('No video found with that search query.');
    }

    // Join the voice channel
    const connection = await message.member.voice.channel.join();

    // Play the video in the voice channel
    const stream = ytdl(video.url, { filter: 'audioonly' });
    const dispatcher = connection.play(stream);

    // Send a message to indicate the currently playing video
    message.channel.send(`Now playing: ${video.title}`);
  } else if (command === 'stop') {
    // Check if the bot is in a voice channel
    if (!message.guild.me.voice.channel) {
      return message.reply('I am not currently in a voice channel.');
    }

    // Leave the voice channel
    message.guild.me.voice.channel.leave(); ```
#
    message.channel.send('Stopped playing and left the voice channel.');
  }
});

// Log in the bot using the provided token
client.login(config.token); ```
magic ore
#

Can you use proper code block so it's easier to read?

#

```js
code
```

lament igloo
magic ore
#

From this code it looks like you're cutting out some parts

lament igloo
magic ore
#

I meant in the code block

#

But at any rate, it looks like you're leaving out some code from here

magic ore
#

Idk, more code. The error is extremely clear. You are declaring the const Discord twice.

#

You only post code where you declare it once.