【发布时间】:2021-09-28 14:49:55
【问题描述】:
我目前正在尝试在 Discord 机器人中实现语音识别,但我一直遇到错误。我也是 Discord.JS 和整体编程的新手。
我从一个解释普通消息而不是语音的事件文件中复制了以下一些代码,它在那里工作正常。
以下行抛出“UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'startsWith' of undefined”:if (!msg.content.startsWith(prefix) || msg.author.bot) return;
我的整个文件:
const fs = require('fs');
module.exports = {
name: "join",
description: "Joins the VC that user is in",
async execute(client, message, args, Discord) {
const voiceChannel = message.member.voice.channel;
const connection = await voiceChannel.join();
if (!voiceChannel) return message.channel.send('Join a VC retard');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send("You don't have the correct permissions");
if (!permissions.has('SPEAK')) return message.channel.send("You don't have the correct permissions");
connection.client.on('speech', msg => {
console.log(msg.content);
const prefix = 'yo bot ' || 'Aries ' || 'Ares ' || 'yobot ';
if (!msg.content.startsWith(prefix) || msg.author.bot) return;
const args = msg.content.slice(prefix.length).split(/ +/);
const cmd = args.shift().toLowerCase();
const command = client.commands.get(cmd);
if (command) command.execute(client, msg, args, Discord);
})
}
}
【问题讨论】:
-
你能告诉我们
connection.client.on('speech', msg =>函数中的msg是什么吗? -
当我将 msg.content 记录到控制台时,它会打印出我所说的内容,但一秒钟后会打印出错误。
-
是的,我明白了,但这就是为什么我要求
msg而不是msg.content来查看msg是否像其他人所说的那样在日志中具有content属性。
标签: javascript discord typeerror