GithubHelp home page GithubHelp logo

androz2091 / discord-temp-channels Goto Github PK

View Code? Open in Web Editor NEW
108.0 4.0 21.0 145 KB

Simple framework to facilitate the creation of a temporary voice channels system using Discord.js

TypeScript 100.00%

discord-temp-channels's Introduction

Discord Temporary Voice Channels

Discord Temp Channels is a framework to facilitate the creation of a temporary voice channels system using Discord.js (v13)!

Installation

npm install --save discord-temp-channels

Example

Code

const Discord = require("discord.js");
const client = new Discord.Client();

const TempChannels = require("discord-temp-channels");
const tempChannels = new TempChannels(client);

// Register a new main channel
tempChannels.registerChannel("channel-id", {
    childCategory: "category-id",
    childAutoDeleteIfEmpty: true,
    childMaxUsers: 3,
    childFormat: (member, count) => `#${count} | ${member.user.username}'s lounge`
});

client.login("YOUR_TOKEN");

Result

temp

Methods

Register Channel

You have to register a channel to indicate to the package which channel will be used to create child channels.

// Register a new parent channel
tempChannels.registerChannel("channel-id", {
    childCategory: "category-id",
    childAutoDeleteIfEmpty: true,
    childAutoDeleteIfOwnerLeaves: true,
    childMaxUsers: 3,
    childBitrate: 64000,
    childFormat: (member, count) => `#${count} | ${member.user.username}'s lounge`
});

channelID: The ID of the channel the users will have to join to create a new channel.

options.childCategory: Optional - This will be the category ID in which the new channels will be created.
options.childAutoDeleteIfEmpty: Whether, when a channel is empty, it should be deleted.
options.childAutoDeleteIfOwnerLeaves: Whether, when the member who created a channel left it, it should be deleted (even if it's not empty).
options.childMaxUsers: Optional - This will be the maximum number of users that can join a channel
options.childBitrate: Optional - This will be the new channel bitrate
options.childFormat: This is a function which takes two parameters: the member (the one who created the channel, and the number of voice channels created from the same parent channel)

Un-Register Channel

You can un-register a channel, so the users who join the it won't create a new channel.

// Unregister a parent channel
tempChannels.unregisterChannel("channel-id");

channelID: The ID of the channel you want unregister.

Events

// Emitted when a child channel is created
tempChannels.on("childCreate", (member, channel, parentChannel) => {
    console.log(member); // The member who created the new channel
    console.log(channel); // The channel which was created
    console.log(parentChannel); // The channel the member joined to create the new channel
});

// Emitted when a child channel is deleted
tempChannels.on("childDelete", (member, channel, parentChannel) => {
    console.log(member); // The member who caused the deletion of the channel
    console.log(channel); // The channel which was deleted
    console.log(parentChannel); // The channel the member joined to create the deleted channel
});

// Emitted when a channels is registered
tempChannels.on("channelRegister", (channelData) => {
    console.log(channelData);
    /*
    {
        "channelID": "03909309383083"
        "options": {
            "childCategory": "380398303838398390",
            "childAutoDeleteIfEmpty": true
            etc...
        }
    }
    */
});

// Emitted when a channels is unregistered
tempChannels.on("channelUnregister", (channelData) => {
    console.log(channelData);
    /*
    {
        "channelID": "03909309383083"
        "options": {
            "childCategory": "380398303838398390",
            "childAutoDeleteIfEmpty": true
            etc...
        }
    }
    */
});

// Emitted when there is an error
tempChannels.on("error", (err, message) => {
    console.log(err);
    console.log(message);
});

Bot Example

This code stores temporary channels data in a database (quick.db in this case). When the bot starts, it registers all the channels in the database and there is a command to add new main channels (!set).

const Discord = require("discord.js");
const client = new Discord.Client();

const TempChannels = require("discord-temp-channels");
const tempChannels = new TempChannels(client);

const db = require("quick.db");

client.on("ready", () => {
    if (!db.get("temp-channels")) db.set("temp-channels", []);
    db.get("temp-channels").forEach((channelData) => {
        tempChannels.registerChannel(channelData.channelID, channelData.options);
    });
});

client.on("message", (message) => {

    if(message.content.startsWith("!set")){
        if(tempChannels.channels.some((channel) => channel.channelID === message.member.voice.channel.id)){
            return message.channel.send("Your voice channel is already a main voice channel");
        }
        const options = {
            childAutoDeleteIfEmpty: true,
            childAutoDeleteIfOwnerLeaves: true,
            childMaxUsers: 3,
            childBitrate: 64000,
            childFormat: (member, count) => `#${count} | ${member.user.username}'s lounge`
        };
        tempChannels.registerChannel(message.member.voice.channel.id, options);
        db.push("temp-channels", {
            channelID: message.member.voice.channel.id,
            options: options
        });
        message.channel.send("Your voice is now a main voice channel!");
    }

});

client.login("YOUR_TOKEN");

discord-temp-channels's People

Contributors

androz2091 avatar danthedev123 avatar dependabot-preview[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

discord-temp-channels's Issues

config.json

i need a config file like config.json to setup the bot, guild, token,.....
I tried but it was error

Error after restarting bot with quick.db

"stack": "TypeError: parentChannel.options.childFormat is not a function
Thats the error when i set the voicechannel with quick,db and restart my bot. When i then join the voicechannel the error pops up and doesnt create a new voice channel. When i run the set command, the channel is already set and recognized tho, so i dont think it deletes the channel. Idk what to do, because i also tried it with mongoDB and didnt got it working. Can u help me there ?
The code snippet from u: Here + the full error Here . Thanks!

[bug] discord_js_1.Intents is not a constructor

I recently upgraded my bot to Discord.js v14 and tried to use this module, but it gives me this error whenever I start the bot:

/home/chamln/Documents/chamlny/discord/aurora/node_modules/discord-temp-channels/lib/index.js:60
        if (!new discord_js_1.Intents(client.options.intents).has('GUILD_VOICE_STATES')) {
             ^
TypeError: discord_js_1.Intents is not a constructor
    at new TempChannelsManager (/home/chamln/Documents/chamlny/discord/aurora/node_modules/discord-temp-channels/lib/index.js:60:14)
    at new AuroraClient (/home/chamln/Documents/chamlny/discord/aurora/structures/AuroraClient.ts:39:22)
    at Object.<anonymous> (/home/chamln/Documents/chamlny/discord/aurora/index.ts:9:16)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Module.m._compile (/home/chamln/Documents/chamlny/discord/aurora/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1174:10)
    at Object.require.extensions.<computed> [as .ts] (/home/chamln/Documents/chamlny/discord/aurora/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:998:32)
    at Function.Module._load (node:internal/modules/cjs/loader:839:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at phase4 (/home/chamln/Documents/chamlny/discord/aurora/node_modules/ts-node/src/bin.ts:649:14)
    at bootstrap (/home/chamln/Documents/chamlny/discord/aurora/node_modules/ts-node/src/bin.ts:95:10)
    at main (/home/chamln/Documents/chamlny/discord/aurora/node_modules/ts-node/src/bin.ts:55:10)
    at Object.<anonymous> (/home/chamln/Documents/chamlny/discord/aurora/node_modules/ts-node/src/bin.ts:800:3)
    at Module._compile (node:internal/modules/cjs/loader:1120:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1174:10)

I tried to figure it out on my own and found out that the problem is that the module can't check if the discord.js client has the guild voice states intent, no matter if it's provided when initializing the Client or not.
Is it related to breaking changes in discord.js? And if so, will this module support v14 in the future?

[Feature Request]

  • Feature:
  • Set the timeout before deleting a voice channel
  • Manually created subchannels can be registered with commands to control even if the bot is reset
  • Reason:
  • For me and a few others, hosting bots with Heroku, Replit and using MongoDB was the limiting point as the bot was reset every day. That means that when the bot resets, it won't delete the temporary channels created. When registering manually, it helps the bot to have better control.
  • Thank you for reading my comments, although my English is not very good

error when connecting the bot

I'm using that example bot that is available on the npm page for this package, but when I try to connect the bot to this error

node . (node:11276) UnhandledPromiseRejectionWarning: TypeError: db.get(...).forEach is not a function at Client.<anonymous> (C:\Users\Akure\Desktop\test\index.js:10:29) at Client.emit (events.js:315:20) at WebSocketManager.triggerClientReady (C:\Users\Akure\Desktop\test\node_modules\discord.js\src\client\websocket\WebSocketManager.js:431:17) at WebSocketManager.checkShardsReady (C:\Users\Akure\Desktop\test\node_modules\discord.js\src\client\websocket\WebSocketManager.js:415:10) at WebSocketShard.<anonymous> (C:\Users\Akure\Desktop\test\node_modules\discord.js\src\client\websocket\WebSocketManager.js:197:14) at WebSocketShard.emit (events.js:315:20) at WebSocketShard.checkReady (C:\Users\Akure\Desktop\test\node_modules\discord.js\src\client\websocket\WebSocketShard.js:475:12) at WebSocketShard.onPacket (C:\Users\Akure\Desktop\test\node_modules\discord.js\src\client\websocket\WebSocketShard.js:447:16) at WebSocketShard.onMessage (C:\Users\Akure\Desktop\test\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10) at WebSocket.onMessage (C:\Users\Akure\Desktop\test\node_modules\ws\lib\event-target.js:132:16) at WebSocket.emit (events.js:315:20) at Receiver.receiverOnMessage (C:\Users\Akure\Desktop\test\node_modules\ws\lib\websocket.js:825:20) at Receiver.emit (events.js:315:20) at Receiver.dataMessage (C:\Users\Akure\Desktop\test\node_modules\ws\lib\receiver.js:437:14) at Receiver.getData (C:\Users\Akure\Desktop\test\node_modules\ws\lib\receiver.js:367:17) at Receiver.startLoop (C:\Users\Akure\Desktop\test\node_modules\ws\lib\receiver.js:143:22) (node:11276) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:11276) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. voice handling

"childDelete" Event not firing

no matter how hard I try, the "childDelete" event does not fire at all - this is because the temporary channels no longer get deleted by this package ๐Ÿ˜ข

Please fix this <3 I love this package

TypeError: parentChannel.options.childFormat is not a function

Trying to use the example shown in the README.md file, but I'm running into an error.

I updated some of the db stuff so it works with the latest version of quick.db, maybe that's the source of the error? I also added intents because thats what v13 needs.

When I start up the bot with a blank database (with temp-channels being set as an array already), it works just fine - set the main channel, join it, and it makes a temp channel. My error appears when I restart the bot, and try to join the hub channel again. It appears with: "TypeError: parentChannel.options.childFormat is not a function."

Odd though, if I delete the channelData.options in tempChannels.registerChannel(channelData.channelID, channelData.options);, it works ok, but it puts my id in the format <@xxxxxxxxx>'s lounge...

Can anyone help?
B1AA6VEs

QUESTION

How can I put it so that if the administrator leaves the channel does not close and if it remains empty it is eliminated? In the childAutoDeleteIfOwnerLeaves: false and childAutoDeleteIfEmpty: true; But when all the people leave the channel is not deleted

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.