This snippet will track the current voice channel (VC) of a user. It also provides a function that you can use anywhere to get the current VC of a user.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// ** DEV **
// Copy and use this function in other files to get the current VC of a user.
// Note the value will be 'undefined' if they are not in a VC.
// Usage:
// const channelId = await getUserVC('12345')
function getUserVC(user_id) {
return lib.utils.kv['@0.1.16']
.get({ key: 'user-current-vc', defaultValue: {} })
.then(vcs => vcs[user_id]);
}
// Get the user channel info
const event = context.params.event;
const { guild_id, channel_id, user_id } = event
const userChannels = await lib.utils.kv['@0.1.16']
.get({ key: 'user-current-vc', defaultValue: {} });
// Update the user channel info
if (channel_id) {
userChannels[user_id] = channel_id
} else {
delete userChannels[user_id]
}
// Save the user channel info
await lib.utils.kv['@0.1.16'].set({ key: 'user-current-vc', value: userChannels });