Discord js creating discord category - discord.js

I'm trying to make a category on discord server using Discord Bot but I couldn't find the method or something on internet. Also I looked the "discord.js.org". Then I thought that isn't there any possibility to do that. So is there any way to make a category on discord servers?

discordjs v13 needs GUILD_CATEGORY instead of just "category"
message.guild.channels.create("Name", { type: "GUILD_CATEGORY" });

You need to use the .createChannel method and then enter „category“ as type of the channel
<guild>.createChannel("NAME OF THE CHANNEL", "category")

I would advice the usage of a promise as it adds a lot of functionality and safety to your code
guild.createChannel('new-category', {
type: 'category',
permissionsOverwrites: [{
id: guild.id,
deny: ['MANAGE_MESSAGES'],
allow: ['SEND_MESSAGES']
}]
})
.then(console.log)
.catch(console.error);
This allows you to create the channel with permissions and actually handle any errors like the channel already existing or your bot not being able to create said channel cause of its permissions assigned.
This is the proper way to do this.
Example to create channel
guild.createChannel('new-general', { type: 'text' })
.then(console.log)
.catch(console.error);

v12:
message.guild.channels.create('Category', { type: 'category' });

I have made a command code for you to use. Modify it and use it.
if(message.content === `${prefix}create-channel`) {
message.guild.createChannel('name', {
//Channel type (text || voice || category)
type: 'text',
permissionsOverwrites: [{
id: guild.id,
deny: [],
allow: ['SEND_MESSAGES']
}]
})
.catch(console.error);
}

discordjs v14 needs ChannelType.GuildCategory and the name in the options
message.guild.channels.create({ name: "Name", type: ChannelType.GuildCategory });

Related

How can I clear my Discord bot's status after it has been set?

I have some code that gives my bot a status and I want it to clear with the use of a command. I am able to change the status, but I can't clear it.
Here's the code that I use to give the bot it's status:
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
targetGuild = client.guilds.cache.get('729676078599110776')
client.user.setPresence({
status: 'online',
activity: {
name: `${targetGuild.memberCount} members | !c help`,
type: "WATCHING"
}
});
});
I've tried clearing it by setting the presence without adding the status itself, but that seems to just leave it the same.
client.user.setPresence({
status: 'online'
});
Yes, the client.user.setPresence({ activity: null }) will clear the status, and if you want to log out of the bot so it's offline, you can do client.destroy()

How to design schema for nested data

I created a post Schema and I have trouble implementing the comment and comment reply schema since you can not predict how often one comment reply has it own reply.
I am using mongoose and express.
So how can I implement this type of schema design?
I think you're looking for something like this where you are referencing comments from within your comment schema.
I added a middleware to pre-populate the replies array when you call .find(). You can add more middleware for other calls like .findOne() etc.
const mongoose = require("mongoose");
const commentSchema = mongoose.Schema(
{
comment: {
type: String,
required: true
},
author: { // To reference the user that left the comment. If needed
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
replies:[{type: Schema.Types.ObjectId, ref: "Comment"}] // Array of comment replies
},
{
timestamps: true,
}
);
// Middleware to populate the replies when you call `find()`
commentSchema.pre('find', function() {
this.populate('replies');
});
module.exports = mongoose.model('Comment', commentSchema);
You can do more in-depth on this post which will show you how to pre-populate the replies field when returning comments etc.
https://www.makeschool.com/academy/track/standalone/reddit-clone-in-node-js/comments-on-comments

Why does this mute function show all the hidden channels?

message.guild.channels.cache.forEach((channel) => {
channel.overwritePermissions([
{
id: muteRole.id,
deny: ['SEND_MESSAGES', 'CONNECT', 'ADD_REACTIONS'],
},
], 'Mute role permissions');
});
This shows all hidden channels and resets all permissions in all channels.
The issue is that overwritePermissions() like it says overwrites every permission,
you can use updateOverwrite() instead
https://discord.js.org/#/docs/main/stable/class/TextChannel?scrollTo=updateOverwrite
message.channel.updateOverwrite(muteRole.id, {
SEND_MESSAGES: false
})
.then(console.log)
.catch(console.error);

Discord.Js options object

I'm actually doing a command to start a "conversation" with a player and take responses he give me. For doing that, I've the plan to use a temporary channel. I don't find a complet way to create a channel. I saw, that we have to create the channel, and after modifie it to adjust as we want. So I have this code :
m.guild.createChannel(`Candidature-${m.author.username}`, 'text', [{
type: 'role',
id: '605021521467146279',
permission: 0x400
}])
with this error :
(node:1904) DeprecationWarning: Guild#createChannel: Create channels with an options object instead of separate parameters
and I don't find real documentation about options object. Can I have some information about how it's work, and some link to learn more ?
Thanks for your help.
I can see the confusion here as the way channels are now created have been altered and here is how to create them: (var name = "blah" isn't needed but cleans it up a bit)
var name = `ticket-${numbers}`;
message.guild.createChannel(name, { type: "text" })
And to do the channel permissions you want to use .then like this:
message.guild.createChannel(name, { type: "text" }).then(
(chan) => {
chan.overwritePermissions(message.guild.roles.find('name', '#everyone'), {
'VIEW_CHANNEL': false
})
You can change the role or change it to other things such as message.author.id or mentioned users etc.
Hope this helps!
Thanks for your answers. I have do this :
m.guild.createChannel(
`Candidature-${m.author.username}`, {
type: 'text',
topic: `Salon de candidature créé par ${m.author.username} | Id du joueur : ${m.author.id}`,
parent: idCategorie,
permissionOverwrites: [{
id: m.guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: m.author.id,
allow: ['VIEW_CHANNEL'],
}
]
})
.then((chan) => {
console.log("Channel create");
});
With those links :
https://discord.js.org/#/docs/main/stable/typedef/ChannelData
https://discordjs.guide/popular-topics/permissions.html#roles-as-bot-permissions
It's creating a channel with a name, a topic, and a categorie as parent. Only the plyaer itself and administrator can see the channel.

Organizing Channels

I am trying to create a bot that sets up the server for you when the command !setup is input. I have got to the stage at which the bot creates all of the roles and channels. However, I now need the bot to organize the channels and place text/voice channels inside category channels and move them to the correct position.
message.guild.createChannel('server-tests', {
type: 'text',
});
You can define the position and parent properties of a channel when you create it (see here).
You can use the keyword await (async context needed) to create the channels in order.
try {
const generalCategory = await message.guild.createChannel('General Stuff', {
type: 'category',
position: 3
});
await message.guild.createChannel('general-chat', {
type: 'text',
parent: generalCategory
});
} catch(err) {
console.error(err);
}

Resources