javascript - 音乐机器人未连接到语音频道
问题描述
试图制作一个只播放一个链接的简单音乐机器人,但它没有连接到语音通道,因此它没有给出任何错误,所以除了输入我的所有代码之外我什么也做不了。这是我的 Main.js
const {Intents} = require('discord.js');
const Discord = require('discord.js');
const botIntents = [ Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_MEMBERS];
const client = new Discord.Client({ intents: botIntents });
const prefix = '-';
const fs = require('fs');
const teo = "<@---------->";
const aime = "<@------------>";
const pagliac = "Di di no alla vita sociale! Ogni giorno migliaia di gamer perdono i propri amici per questa Vita sociale, Non si sa cosa sia pero se donate una piccola somma di: 4 reni, tua madre e forse anche tua sorella";
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', async () =>{
console.log('Ready!');
client.user.setActivity('Tua madre', { type: 'PLAYING' });
})
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 === 'embed'){
client.commands.get('embed').execute(message, args, Discord, client);
} else if (command === 'pong'){
message.channel.send('Aime e un pagliaccio, giochiamo a the forest');
} else if (command === 'teo'){
for (var i=0; i<5; i++){
message.channel.send(teo);
}
} else if (command === 'aime'){
for (let s=0; s<5; s++){
message.channel.send(aime);
message.channel.send(pagliac);
}
} else if (command === 'clear'){
client.commands.get('clear').execute(message, args);
} else if (command === 'ping'){
message.channel.send(`Pingazzo pazzo di: ${Date.now() - message.createdTimestamp}ms. La latenza delle api ${Math.round(client.ws.ping)}ms`);
} else if (command === 'acqua'){
const siuum = new Discord.MessageEmbed()
.setColor('#0011FF')
.setTitle('Marmellata')
.setDescription('marmellata')
const sus = message.mentions.users.first();
sus.send({embeds: [siuum]});
} else if(command === 'bully'){
client.commands.get('bully').execute(message)
} else if(command === 'aimesus')
client.commands.get('aimesus').execute(message)
});
这是我的音乐机器人命令,我认为它是正确的,因为我已经在这里寻求过帮助。
const ytdl = require('ytdl-core');
module.exports ={
name:'aimesus',
description:'aaaaaaaaaaaam',
execute(message, args){
const { joinVoiceChannel, createAudioPlayer, createAudioResource, generateDependencyReport, VoiceConnectionStatus } = require('@discordjs/voice');
const url = 'https://www.youtube.com/watch?v=NevKVKbCNy4&ab_channel=NTDM'
const stream = ytdl(url, {filter: 'audioonly'});
const player = createAudioPlayer();
const resource = createAudioResource(stream);
const connection = joinVoiceChannel({
channelId: message.channelid,
guildId: message.guildid,
adapterCreator: message.guild.voiceAdapterCreator
})
connection.subscribe(player);
player.play(resource);
console.log(generateDependencyReport());
}
}
解决方案
首先,我建议您将侦听器从 更改.on('message')
为.on('messageCreate')
。前者已弃用,后者效果更好。
更重要的是,您在joinVoiceChannel()
代码中传递了错误的变量。guildid
和channelid
属性是 Snowflake 类型,而字段采用strings
。这样做:
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
})
推荐阅读
- javascript - 如何删除 Next.js 块
- python - Tkinter 使用一个代码打开多个文件
- recursion - 递归合并排序重复元素打印
- algorithm - Dijkstra 和一个关于分析的问题,它与实施有关吗?
- postgresql - 无法在 docker 中运行 pg
- sql - 将具有重复值的数据插入 Postgresql
- java - 使用菱形分配具有有界通配符的泛型类型
- java - 建立 POST 请求以保存在 mySQL 上后,我的 InputStream 与我的网站返回 java.IO.FileNotFoundException
- paypal-sandbox - PayPal 智能按钮:获取交易详情并更新到数据库
- python - 如何在实际运行值的命令之前使用字典中的值