我正在尝试在频道中存储语音时间
这是我到目前为止所拥有的东西,我被卡住了,我试图解决所有问题后不知道要放什么
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if (oldUserChannel === undefined && newUserChannel !== undefined) {
function start() {
startTime = new date()
}
}else if(newUserChannel === undefined){
function end() {
endTime = new date()
var timeDif = endTime - startTime;
}
}
});
我希望它在行会名称和用户名下的.json文件中记录语音时间,但我被困了
预先感谢
答案 0 :(得分:0)
问题在于,在您的if
语句(以及else
子句)中,您创建了一个函数,但从未调用过该函数。有2种修复方法,您可以在下面看到它们。
选项1:创建函数后调用它们
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if (oldUserChannel === undefined && newUserChannel !== undefined) {
function start() {
startTime = new date()
}
// Call the function after creating it
start();
} else if(newUserChannel === undefined) {
function end() {
endTime = new date();
var timeDif = endTime - startTime;
}
// Call the function after creating it
end();
}
});
选项2:删除函数声明,仅执行代码(我认为更好/更清洁的选项)
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if (oldUserChannel === undefined && newUserChannel !== undefined) {
startTime = new date()
} else if(newUserChannel === undefined) {
endTime = new date();
var timeDif = endTime - startTime;
}
});
请注意,该代码可能无法按照您的想法工作。假设personA加入语音通道,则将设置startTime
。如果那时personB加入语音通道,则startTime
变量将被新的new Date()
值覆盖。如果要存储每个特定用户的开始时间,则需要将其存储在与该用户相关的某个位置。说一个具有属性user
和startTime