Discord.js slash command option - discord

Im using WOKCommands, and want to make so you can only choice a voice channel. How can i do this?
options: [
{
name: 'channel',
description: '🔊 Voice channel in which you want to play your activity',
required: true,
type: discord_js_1.default.Constants.ApplicationCommandOptionTypes.CHANNEL
},
],

You can find the channel type id's in the official Discord documentation. With that given, you can specify specific channel types. In this example, channel_types: [2] = only voice channels.
options: [
{
name: 'channel',
description: '🔊 Voice channel in which you want to play your activity',
required: true,
type: discord_js_1.default.Constants.ApplicationCommandOptionTypes.CHANNEL
channel_types: [2]
},
],
Hope This Helps

You can't put type of option as Voice Channel
as it's not a valid type but channel is
so in this case you can check if the type of channel is voice and give him an error message to provide a voice channel
Example:
let channel = interaction.options.getChannel(interaction.options.data[0].name);
if(channel.type !== "GUILD_VOICE") return interaction.channel.send({ content:"Please provide a valid voice channel" });

Related

My bot sends an embed that is invisible to all users and shows no message sent

I've been working on using JS to create a simple Discord Bot for my server. I have been trying to have it send a message with the rules of the server embedded into it. When the /rules command is run, I receive a notification to state a message was sent, but there is no message to be seen on any device. I am able to view message history so I do not understand why there is no visible embed or message.
My code was made using Autocode's Discord Embed Builder, and has worked for other embeds within the same Bot. A link to view the full embed code in the Builder and see how it is supposed to look is here.
It would be much easier to learn to make these yourself than using a generator and trying to reverse engineer how their coding works:
a simple example would be this:
const {
MessageEmbed
} = require('discord.js')
module.exports = {
name: "rules",
description: "Post rules",
run: async (client, interaction) => {
// build the message
const embed = new MessageEmbed()
.setColor('#00ff00') //change the color if you want
.setTitle('Some Title')
.setDescription(`Some description`)
.addFields({
name: 'Rule Name/Number',
value: 'Rule text',
inline: true
}, {
name: 'Rule Name/Number',
value: `Rule text`,
inline: true
}, {
name: 'Rule Name/Number',
value: 'Rule text',
inline: true
})
// add more if needed
// send the message
interaction.channel.send({
embeds: [embed]
})
// confirm they are sent and complete the interaction only visible to you
return interaction.reply({
content: 'Done',
ephemeral: true
})
}
}

How to create announcement channel

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

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.

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

Discord.js creating a private voice channel and role

I am trying to make a discord bot with Discord.js, that can create a private channel and a new role and assign that role to the channel. So any users with the new role can access the channel.
The following code, creates a channel from the name i give it through 'eventName' it also creates a role of the same name.
But how do i make the voice channel private, set a role to exclusively access it and set some permissions to the new role?
function addChannel(message,args,eventName){
var server = message.guild;
var permsName = eventName+"-"+message.author.username;
message.guild.createRole({
//data: {
name: permsName,
permissions: []
//},
//reason: 'new Event'
}).then(role => {
message.member.addRole(role,permsName)
.catch(error => client.catch(error))
}).catch(error => client.catch(error))
server.createChannel(eventName, 'voice').then( // Create the actual voice channel.
(chan) => {
chan.setParent("427382662240534535").then( // Move the voice channel to the current message's parent category.
(chan2) => {
console.log("stage 3");
console.log(chan2);
//console.log(`Set the category of ${chan2.name} to ${chan2.parent.name}`);
chan2.overwritePermissions(message.guild.roles.find('name', '#everyone'), { 'CREATE_INSTANT_INVITE' : false }); // Give the channel some standard permissions.
chan2.overwritePermissions(message.guild.roles.find('name', permsName), {
'CREATE_INSTANT_INVITE' : false, 'ADD_REACTIONS': true,
'READ_MESSAGES': true, 'SEND_MESSAGES': true,
'SEND_TTS_MESSAGES': true, 'MANAGE_MESSAGES': true,
'EMBED_LINKS': true, 'ATTACH_FILES': true,
'READ_MESSAGE_HISTORY': true, 'MENTION_EVERYONE': true,
'EXTERNAL_EMOJIS': true, 'CONNECT': true,
'SPEAK': true
});
console.log("stage 4");
}
).catch(console.error);
}
).catch(console.error);
return '```Added```';
}
Theres two problems here, for one you are using a lot of unnessecary permissions here, for Voice obviously reactions reading and sending messages etc are not important, important are only VIEW_CHANNEL, SPEAK, CONNECT, and CREATE_INSTANT_INVITE, and you have to explicitely forbid everyone for a standard discord server setup since everyone usually has these rights Serverwide unless locally overwritten.
So what you want for your overwrite permissions is:
chan2.overwritePermissions(message.guild.roles.find('name', '#everyone'), { // Disallow Everyone to see, join, invite, or speak
'CREATE_INSTANT_INVITE' : false, 'VIEW_CHANNEL': false,
'CONNECT': false, 'SPEAK': false
});
chan2.overwritePermissions(message.guild.roles.find('name', permsName), {//Explicitely allow the role to see, join and speak
'VIEW_CHANNEL': true, 'CONNECT': true, 'SPEAK': true,
});
Note that you do not have to explicitely disallow CREATE_INSTANT_INVITE since that is inherited from everyone if not explicitely changed.

Resources