【问题标题】:SyntaxError: Cannot use import statement outside a module discord.jsSyntaxError:无法在模块 discord.js 之外使用 import 语句
【发布时间】:2022-01-25 21:08:15
【问题描述】:

我一般是编码新手,所以期待nooby行为。
我尝试制作不和谐的音乐机器人,但它无法加入语音聊天,当我输入节点时,弹出一个错误 我不知道把 import 放在哪里,我到处都试过但我不工作,ping 和 youtube 命令工作只玩不玩

这是主要代码:

const { Client, Intents, DiscordAPIError } = require('discord.js');

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

const prefix = '!';

const fs = require('fs');

const Discord = require('discord.js');
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);
}



client.once('ready', () => {
    console.log('Musicbot is online!');
});

client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;

const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();

if(command === 'ping'){
  client.commands.get('ping').execute(message, args);
} else if (command == 'youtube'){
    client.commands.get('youtube').execute(message, args);
}  else if (command == 'play'){
    client.commands.get('play').execute(message, args);
}  else if (command == 'leave'){
    client.commands.get('leave').execute(message, args);
} 


});







client.login('token');
https://stackoverflow.com/questions/ask#
这是播放命令代码:

const ytdl = require('ytdl-core');
const ytSearch = require('yt-search');

module.exports = {
    name: 'play',
    async execute(message, args){
   
        const voiceChannel = message.member.voice.channel;

        if (!voiceChannel) return message.channel.send('Musisz być na kanale głosowym by użyć tej komendy!');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('Nie masz odpowiednich uprawnień');
        if (!permissions.has('SPEAK')) return message.channel.send('Nie masz odpowiednich uprawnień');
        if (!args.length) return message.channel.send('You need to send the second argument!');

        import { joinVoiceChannel } from "@discordjs/voice";
        const connection = joinVoiceChannel(
        {
        channelId: message.member.voice.channel,
        guildId: message.guild.id,
        adapterCreator: message.guild.voiceAdapterCreator
        });
        
     
    
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);

            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
        }

        const video = await videoFinder(args.join(' '));

        if(video){
            const stream = ytdl(video.url, {filter: 'audioonly'});
            connection.play(stream, {seek: 0, volume: 1});
            on('finish', () =>{
                voiceChannel.leave();
            });

            await message.reply(`:thumbsup: Now Playing ***${video.title}***`)
        }else {
            message.channel.send('Nie znaleziono wideo'); 
        }


    }
}
请帮忙

【问题讨论】:

    标签: javascript discord discord.js bots


    【解决方案1】:

    尝试将import { joinVoiceChannel } from "@discordjs/voice"; 替换为const { joinVoiceChannel } = require("@discordjs/voice");

    出现此错误是因为您尝试在 commonjs 中使用 es modules import。但是,如果需要,您可以在 commonjs 上使用 dynamic imports。

    【讨论】:

      猜你喜欢
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2023-02-08
      • 2020-08-25
      • 2022-11-02
      • 2022-06-13
      • 2023-01-31
      • 2021-08-24
      相关资源
      最近更新 更多