![](https://discord-voice.js.org/icon-resized.png)
Discord Voice
![Documentation - https://discord-voice.js.org](https://img.shields.io/badge/Documentation-Click%20here-blue?style=for-the-badge)
What is Discord Voice?
Discord Voice is a powerful Node.js module that allows you to easily track the user's voice time and levels!
Features
- ✨ Easy to use!
- 📁 Support for all databases! (default is json)
- ⚙️ Very customizable! (ignored channels, ignored members, ignored permissions, xp amount to add, voice time to add etc...)
- 🚀 Super powerful: createUser, createConfig, removeUser, removeConfig, updateUser and updateConfig!
- 🕸️ Support for shards!
- and much more!
Installation
npm install --save discord-voice
Examples
You can use this example bot on GitHub: VoiceTimeTrackerBot
Usage of the modudle
const { Client, Intents } = require("discord.js"),
client = new Client({
intents: [Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
}),
settings = {
prefix: "v!",
token: "Your Discord Bot Token"
};
const { VoiceManager } = require("discord-voice");
const manager = new VoiceManager(client, {
userStorage: "./users.json",
configStorage: "./configs.json",
checkMembersEvery: 5000,
default: {
trackBots: false,
trackAllChannels: true
}
});
client.voiceManager = manager;
After that, user's who are in the voice channel's that the bot has cached will be checked. You can pass an options object to customize the config. For a list of them refer to the documentation.
Create an user
client.on("messageCreate", (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "create-user") {
client.voiceManager.createUser(message.author.id, message.guild.id, {
levelingData: {
xp: 0,
level: 0
}
});
}
});
This allow's you create a user in the database if the user is not already present in the database. You can pass an options object to customize the user's data. For a list of them refer to the documentation.
Create a config
client.on("messageCreate", (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "create-config") {
client.voiceManager.createConfig(message.guild.id, {
trackBots: false,
trackAllChannels: true,
exemptChannels: () => false,
channelIds: [],
exemptPermissions: [],
exemptMembers: () => false,
trackMute: true,
trackDeaf: true,
minUserCountToParticipate: 0,
maxUserCountToParticipate: 0,
minXpToParticipate: 0,
minLevelToParticipate: 0,
maxXpToParticipate: 0,
maxLevelToParticipate: 0,
xpAmountToAdd: () => Math.floor(Math.random() * 10) + 1,
voiceTimeToAdd: () => 1000,
voiceTimeTrackingEnabled: true,
levelingTrackingEnabled: true
});
}
});
This allow's you create a config in the database if the config is not already present in the database. You can pass an options object to customize the config's data. For a list of them refer to the documentation.
Remove an user
client.on("messageCreate", (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "remove-user") {
client.voiceManager.removeUser(message.author.id, message.guild.id);
}
});
Remove a config
client.on("messageCreate", (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "remove-config") {
client.voiceManager.removeConfig(message.guild.id);
}
});
Updating an user
client.on("messageCreate", (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "edit-user") {
client.voiceManager.updateUser(message.author.id, message.guild.id, {
newVoiceTime: {
channels: [],
total: 0
}
});
}
});
This allow's you edit a user's data. You need to pass an options object to edit the user's data. For a list of them refer to the documentation.
Updating a config
client.on("messageCreate", (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (command === "edit-config") {
client.voiceManager.updateConfig(message.guild.id, {
newTrackBots: true
});
}
});
This allow's you edit a config's data. You need to pass an options object to edit the config's data. For a list of them refer to the documentation.
Fetch users
const allUsers = client.voiceManager.users;
const onServer = client.voiceManager.users.filter((u) => u.guildId === "1909282092");
const user = client.voiceManager.users.filter((u) => u.guildId === "1909282092" && u.userId === "1234567890");
Fetch configs
const allConfigs = client.voiceManager.configs;
const config = client.voiceManager.configs.filter((c) => c.guildId === "1909282092");
Exempt Channels
client.voiceManager.updateConfig(message.guild.id, {
exemptChannels: (channel) => channel.name === "private")
});
⚠️ Note: If the function should be customizable
const channelName = "private";
client.voiceManager.updateConfig(message.guild.id, {
exemptChannels: new Function("channel", `return channel.name === \'${channelName}\'`)
});
Exempt Members
client.voiceManager.updateConfig(message.guild.id, {
exemptMembers: (member) => !member.roles.cache.some((r) => r.name === "Nitro Boost")
});
⚠️ Note: If the function should be customizable
const roleName = "Nitro Boost";
client.voiceManager.updateConfig(message.guild.id, {
exemptMembers: new Function("member", `return !member.roles.cache.some((r) => r.name === \'${roleName}\')`)
});
Voice Time To Add
client.voiceManager.updateConfig(message.guild.id, {
xpAmountToAdd: () => Math.floor(Math.random() * 10) + 1
});
⚠️ Note: The returned value should be a number or the default value (Math.floor(Math.random() * 10) + 1
) will be used.
Xp Amount To Add
client.voiceManager.updateConfig(message.guild.id, {
voiceTimeToAdd: () => 1000
});
⚠️ Note: The returned value should be a number or the default value (1000
) will be used.
Level Multiplier
client.voiceManager.updateConfig(message.guild.id, {
levelMultiplier: () => 0.1
});
⚠️ Note: The returned value should be a number or the default value (0.1
) will be used.
Custom Database
You can use your custom database to save users and configs, instead of the json files (the "database" by default for discord-voice
). For this, you will need to extend the VoiceManager
class, and replace some methods with your custom ones. There are 8 methods you will need to replace:
getAllUsers
: this method returns an array of stored users.getAllConfigs
: this method returns an array of stored configs.saveUser
: this method stores a new user in the database.saveConfig
: this method stores a new config in the database.editUser
: this method edits a user already stored in the database.editConfig
: this method edits a config already stored in the database.deleteUser
: this method deletes a user from the database (permanently).deleteConfig
: this method deletes a config from the database (permanently).
⚠️ All the methods should be asynchronous to return a promise!
Here is an example, using quick.db
, a SQLite database. The comments in the code below are very important to understand how it works!
Other examples:
const { Client, Intents } = require("discord.js"),
client = new Client({
intents: [Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
}),
settings = {
prefix: "v!",
token: "Your Discord Bot Token"
};
const db = require("quick.db");
if (!Array.isArray(db.get("users"))) db.set("users", []);
if (!Array.isArray(db.get("configs"))) db.set("configs", []);
const { VoiceManager } = require("discord-voice");
const VoiceManagerWithOwnDatabase = class extends VoiceManager {
async getAllUsers() {
return db.get("users");
}
async getAllConfigs() {
return db.get("configs");
}
async saveUser(userId, guildId, userData) {
db.push("users", userData);
return true;
}
async saveConfig(guildId, configData) {
db.push("configs", configData);
return true;
}
async editUser(userId, guildId, userData) {
const users = db.get("users");
const user = users.find((u) => u.guildId === guildId && u.userId === userId);
const newUsersArray = users.filter((u) => u !== user);
newUsersArray.push(userData);
db.set("users", newUsersArray);
return true;
}
async editConfig(guildId, configData) {
const configs = db.get("configs");
const newConfigsArray = configs.filter((config) => config.guildId !== guildId);
newConfigsArray.push(configData);
db.set("configs", newConfigsArray);
return true;
}
async deleteUser(userId, guildId) {
const users = db.get("users");
const user = users.find((u) => u.guildId === guildId && u.userId === userId);
const newUsersArray = users.filter((u) => u !== user);
db.set("users", newUsersArray);
return true;
}
async deleteConfig(guildId) {
const configs = db.get("configs");
const newConfigsArray = configs.filter((config) => config.guildId !== guildId);
db.set("configs", newConfigsArray);
return true;
}
};
const manager = new VoiceManagerWithOwnDatabase(client, {
checkMembersEvery: 5000,
default: {
trackBots: false,
trackAllChannels: true
}
});
client.voiceManager = manager;
client.on("ready", () => {
console.log("I'm ready!");
});
client.login(settings.token);
Shards Support
To make discord-voice
work with shards, you will need to extend the VoiceManager
class and update the refreshStorage()
method. This method should call the getAllUsers()
and getAllConfigs()
method for every shard, so all VoiceManager
's synchronize their cache with the updated database.
const { Client, Intents } = require("discord.js"),
client = new Client({
intents: [Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
}),
settings = {
prefix: "v!",
token: "Your Discord Bot Token"
};
const { VoiceManager } = require("discord-voice");
const VoiceManagerWithShardSupport = class extends VoiceManager {
async refreshStorage() {
return client.shard.broadcastEval(() => this.voiceManager.getAllUsers() && this.voiceManager.getAllConfigs());
}
};
const manager = new VoiceManagerWithShardSupport(client, {
userStorage: "./users.json",
configStorage: "./configs.json",
checkMembersEvery: 5000,
default: {
trackBots: false,
trackAllChannels: true
}
});
client.voiceManager = manager;
client.on("ready", () => {
console.log("I'm ready!");
});
client.login(settings.token);