How to create announcement channel - discord

I'm trying to create an announcement channel, but it just creates another text channel. This is my code
guild.channels.create("test", {
type: "announcement",
parent: cate,
})

There is no channel with "announcement" Type
Channel#type
Replace announcement with "news"
guild.channels.create("test", {
type: "news"
})

Related

Storing messages in new conversation collections; MongoDB

I'm a student working on a chat application for my internship, where I use socket.io.
Right now I am busy thinking of a good way to store the messages send in conversations.
As of now I do the following:
For each conversation between one user and another user, a new collection is made.
On every message sent, the message is stored in the according conversation collection in a single document.
The collections:
Where the document looks as follows:
Now I wonder if there is a good argument to be made to have just one collection "conversations", and store all the messages in multiple documents, where each conversation is a new document.
Creating a new collection for every message is very bad idea instead of that you use a simple schema as given below to store your messages
const conversation_schema = new Schema({
from: {
type: ObjectID,
ref: 'User'
},
to: {
type: ObjectID,
ref: 'User'
},
messageBody: { // body of the message(text body/ image blob/ video blob)
type: String,
},
messageType: { // type of the message(text, mp3, mp4, etc...)
type: String,
},
read: { // to boolean flag to mark whether the to user has read the message
type: Boolean,
default: false,
},
createdAt: { // when was this message goit created
type: Date,
default: new Date(),
},
});
you can fetch the conversation between the two users using the following query
conversations.find({
$or: [
{from: 'user1', TO: 'user2},
{from: 'user2', TO: 'user1},
],
}).populate({ path: 'to', model: User })
.populate({ path: 'from', model: User })
.sort({ createdAt: -1 })

How to get the channelid or name of a newly created channel

I have a command with my discord bot to make a channel, but I'm trying to figure out how to get the id or name of the channel right after its made.
Line of code that makes the channel:
message.guild.channels.create('ticket ' + message.member.displayName, { parent: '744477882730020965' });
The reason is that since displayname can have characters not possible in a channel name and discord will just automatically remove those characters there's no actual way to predict what the channel name will be in some cases. There's probably a simple solution I'm missing, and thanks in advance for any help.
The GuildChannelManager#create method returns a Promise with the channel that was just created. You can use Promise.then() to get the channel.
Guild.channels.create(`ticket-${message.member.displayName}`, {
parent: "744477882730020965"
}).then(channel => {
message.channel.send(`I've created the channel ${channel.name}!`);
}).catch(error => {
console.error(error);
message.channel.send(`Couldn't create the channel. Check the console for the error.`);
});
If you are creating the channel in an async function, you can use await to avoid .then() for readability.
const Channel = await Guild.channels.create(`ticket-${message.member.displayName}`, {
parent: "744477882730020965"
}).catch(error => {
console.error(error);
message.channel.send(`Couldn't create the channel. Check the console for the error.`);
});
What do you need the ID for? You can use a then function to do whatever to edit the channel.
Try this?
let role = message.guild.roles.find("name", "#everyone");
message.guild.channels.create('ticket ' + message.member.displayName, { parent: '744477882730020965' }).then(c => {
c.overwritePermissions(role, {
SEND_MESSAGES: false,
READ_MESSAGES: false
});
c.overwritePermissions(message.author, {
SEND_MESSAGES: true,
READ_MESSAGES: true
});
message.reply(`Application created!`)
c.send(`Wait for support staff`)
}).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);
}

Discord js creating discord category

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 });

Resources