Skip to content

Commit

Permalink
add time Duration checking for event happen
Browse files Browse the repository at this point in the history
  • Loading branch information
JangAyeon committed Apr 16, 2024
1 parent ed8b0b1 commit 4d3bda9
Show file tree
Hide file tree
Showing 9 changed files with 319 additions and 127 deletions.
130 changes: 9 additions & 121 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

const { Client, GatewayIntentBits, REST, Routes } = require("discord.js");
const { voiceStateUpdate } = require("./events/voiceStateUpdate");

const client = new Client({
intents: [
Expand All @@ -11,139 +13,25 @@ const client = new Client({



const {
initUserState,
// endDuration,
userState,
// saveDuration,
} = require("./duration");


client.once("ready", () => {
console.log("Bot is ready!");
});


function isChannelToTrack(channelId){
// YOUR_VOICE_CHANNEL_ID
const channelToTrack = process.env.VOICE_CHANNEL;
if(channelToTrack===channelId){
return true
}
else{
return false
}

}



function checkJoinLeft(user, oldState, newState){

if (user && newState.channelId === channelToTrack) {
// 이벤트 발생이 최초로 입장한 유저의 경우
if (userDurations.has(user.id) === false) {
// User joined the specified voice channel
console.log(
`${user.user.tag} joined ${newState.channel?.name}: ${new Date()}`
);
startDuration(user.id);
}
} else if (
user &&
oldState.channelId === channelToTrack &&
newState.channel === null
) {
// User left the specified voice channel

const {
id: userUId,
tag: userTag,
globalName: userGlobalName,
} = user.user;
console.log(
`${userGlobalName} left ${oldState.channel?.name}: ${new Date()}`
);

if (userDurations.has(userUId)) {
const userDurationInfo = userDurations.get(userUId);
endDuration(userDurationInfo);
const { startTime, endTime, totalDuration } = userDurationInfo;
console.log(
`${userUId} left ${userGlobalName}: ${totalDuration} need to be saved on DB`
);

/*saveDuration(
userUId,
userGlobalName,
userTag,
startTime,
endTime,
totalDuration
);*/

// Remove the user from the tracking map
userDurations.delete(user.id);
}
}

}

client.on("voiceStateUpdate", (oldState, newState) => {
const user = newState.member.user;



if (user && oldState && newState) {
const userInfo = {
id: user.id,
name: user.username,
globalName: user.globalName,
};
const newChannel = newState.channelId;
const oldChannel = oldState.channelId;
const newState_ = {
selfDeaf: newState.selfDeaf,
selfMute: newState.selfMute,
selfVideo: newState.selfVideo,
streaming: newState.streaming,
};
const oldState_ = {
selfDeaf: oldState.selfDeaf,
selfMute: oldState.selfMute,
selfVideo: oldState.selfVideo,
streaming: oldState.streaming,
};
// 추적하는 채널 내에서 이벤트 발생한 경우
if (isChannelToTrack(newChannel)) {
// 처음 입장한 경우
if (!userState.has(userInfo.id)) {
initUserState(userInfo, newState_);
} else {
// 기타 마이크, 화면 공유, 이어폰 해제, 소리 끄기 이벤트 발생 여부 확인
}
}
else{
if(isChannelToTrack(oldChannel) && userState.has(user.id)){
console.log("퇴장 처리~~~")
userState.delete(userInfo.id);
}
else{
console.log("그냥 다른 채널에서 발생한 일")
}
}
}



// checkJoinLeft(user, oldState, newState);



});


client.on("voiceStateUpdate", voiceStateUpdate);


client.login(process.env.DISCORD_TOKEN).catch((error) => {
console.log(error);
process.exit(1);
});



module.exports = client;
186 changes: 186 additions & 0 deletions events/voiceStateUpdate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
const { initUserState, userState } = require("../model/userState");

const {
isEventHappened,
setUserInfo,
setChannelInfo,
setEventState,
getTime,
getDuration,
} = require("../utils");


function voiceStateUpdate(oldState, newState){
const {
member: { user },
} = newState;
if (!user || !oldState || !newState) return;

const userInfo = setUserInfo(user);

const channelInfo = setChannelInfo(
oldState.channelId,
newState.channelId
);
const newState_ = setEventState(newState);
const oldState_ = setEventState(oldState);

/*
if (user && channelInfo.new) {
if (!userState.has(userInfo.id)) {
initUserState(userInfo, newState_);
console.log(userState.get(userInfo.id));
} else {
const { selfDeaf, selfMute, selfVideo, streaming } = userState.get(
userInfo.id
);
handleEventStateChange(
newState_.selfDeaf,
oldState_.selfDeaf,
selfDeaf,
"selfDeaf"
);
handleEventStateChange(
newState_.selfMute,
oldState_.selfMute,
selfMute,
"selfMute 발생"
);
handleEventStateChange(
newState_.selfVideo,
oldState_.selfVideo,
selfVideo,
"selfVideo"
);
handleEventStateChange(
newState_.streaming,
oldState_.streaming,
streaming,
"streaming"
);
console.log("이벤트 처리 완료", userState.get(userInfo.id));
}
} else if (channelInfo.old && userState.has(userInfo.id)) {
console.log("퇴장 처리~~~");
userState.delete(userInfo.id);
} else {
console.log("그냥 다른 채널에서 발생한 일");
}*/

// 추적하는 채널 내에서 이벤트 발생한 경우
if (user && channelInfo.new) {
// 처음 입장한 경우
if (!userState.has(userInfo.id)) {
initUserState(userInfo, newState_);
console.log(userState.get(userInfo.id));
} else {
const { selfDeaf, selfMute, selfVideo, streaming } = userState.get(
userInfo.id
);
//console.log("이벤트 발생 전", userState.get(userInfo.id))
// 기타 마이크, 화면 공유, 이어폰 해제, 소리 끄기 이벤트 발생 여부 확인

if (isEventHappened(newState_.selfDeaf, oldState_.selfDeaf)) {
// 이어폰
if (oldState_.selfDeaf) {
// 꺼짐 => 켜짐
selfDeaf.time = getTime();
} else {
// 켜짐 => 꺼짐
console.log(
"self Deaf 발생: 이어폰 DB 저장 필요",
getDuration(selfDeaf.time, getTime())
);
selfDeaf.time = null;
}
selfDeaf.state = newState_.selfDeaf;
console.log("self Deaf 발생: 이어폰");
}
if (isEventHappened(newState_.selfMute, oldState_.selfMute)) {
// 마이크
if (oldState_.selfMute) {
// 꺼짐 => 켜짐
selfMute.time = getTime();
} else {
// 켜짐 => 꺼짐
console.log(
"self Mute 발생: 마이크 DB 저장 필요",
getDuration(selfMute.time, getTime())
);
selfMute.time = null;
}
selfMute.state = newState_.selfMute;
console.log("self Mute 발생: 마이크");
}
if (isEventHappened(newState_.selfVideo, oldState_.selfVideo)) {
// 카메라

if (newState_.selfVideo) {
// 꺼짐 -> 켜짐
selfVideo.time = getTime();
} else {
// 켜짐 -> 꺼짐
console.log(
"self Video 발생: 카메라 DB 저장 필요",
getDuration(selfVideo.time, getTime())
);
selfVideo.time = null;
}
selfVideo.state = newState_.selfVideo;
console.log("self Video 발생 : 카메라");
}
if (isEventHappened(newState_.streaming, oldState_.streaming)) {
// 화면 공유

if (newState_.streaming) {
streaming.time = getTime();
} else {
console.log(
"streaming 발생: 화면 공유 DB 저장 필요",
getDuration(streaming.time, getTime())
);
streaming.time = null;
}
console.log("streaming 발생: 화면 공유");
streaming.state = newState_.streaming;
}
console.log("이벤트 처리 완료", userState.get(userInfo.id));
}
} else {
// console.log(oldChannelTrack, userState.has(user.id))
if (channelInfo.old && userState.has(user.id)) {
console.log("퇴장 처리~~~");
userState.delete(userInfo.id);
} else {
console.log("그냥 다른 채널에서 발생한 일");
}
}

// checkJoinLeft(user, oldState, newState);



}


function handleEventStateChange(newState, oldState, state, type) {
if (isEventHappened(newState, oldState)) {

if(newState){
const duration = getDuration(state.time, getTime());
console.log(
`${type}: ${state.state ? "켜짐" : "꺼짐"}, DB 저장 필요`,
duration
);
}

state.time = state.state ? getTime() : null;
state.state = newState;
}
}

module.exports = {
voiceStateUpdate,
};
24 changes: 20 additions & 4 deletions duration.js → model/userState.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
// const supabase = require("./supabase");

const { getTime } = require("../utils");

const userState = new Map();

function initUserState(userInfo, state) {
//console.log(user)
const { id, name, globalName } = userInfo;
// Record the start time
userState.set(id, {
enter: new Date(),
...state
enter: getTime(),
selfDeaf: {
state: state.selfDeaf,
time: state.selfDeaf ? null : getTime(),
},
selfMute: {
state: state.selfMute,
time: state.selfMute ? null : getTime(),
},
selfVideo: {
state: state.selfVideo,
time: state.selfVideo ? getTime() : null,
},
streaming: {
state: state.streaming,
time: state.streaming ? getTime() : null,
},
});
console.log(`${id}:${name} joined`);
// console.log(userState.get(id))


}


function endDuration(durationInfo) {
durationInfo.endTime = new Date();
durationInfo.totalDuration = Math.floor(
Expand Down Expand Up @@ -43,7 +61,5 @@ async function saveDuration(dsUId, dsGlobalName, dsTag, start, end, duration) {

module.exports = {
initUserState,
// endDuration,
// saveDuration,
userState,
};
Loading

0 comments on commit 4d3bda9

Please sign in to comment.