Discord.js channel manage channel permissions - javascript

I'm having a little issue creating a new channel. I want to create a new channel using the following code:
message.guild.channels.create('channel name', {
type: 'voice',
permissionOverwrites: [
{
id: some_id,
deny: ['VIEW_CHANNEL'],
},
{
id: bot_id,
deny: ['MANAGE_CHANNEL'],
},
],
})
However, I'm getting an error saying the bitfield of manage channel is invalid. I have tried to lookup a list of channel permissions but I couldn't find any.
Error : [Symbol(code)]: 'BITFIELD_INVALID'
Can anyone help me ?

const { ChannelType, PermissionsBitField } = require('discord.js');
guild.channels.create({
name: 'new-channel',
type: ChannelType.GuildText,
permissionOverwrites: [
{
id: interaction.guild.id,
deny: [PermissionsBitField.Flags.ViewChannel],
},
{
id: interaction.user.id,
allow: [PermissionsBitField.Flags.ViewChannel],
},
],
});
Discord.js#v14 has change all BitField check it out this link: https://discordjs.guide/popular-topics/permissions.html#adding-overwrites

Related

discord.js - TypeError [INVALID_TYPE]: Supplied parameter is not a User nor a Role

If I press the button, the following error occurs: TypeError [INVALID_TYPE]: Supplied parameter is not a User nor a Role. However, I can also put a solid id there and this error still occurs.
button.guild.channels.create('cs', {
type: "text",
parent_id: '802172599836213258',
permissionOverwrites: [
{
id: button.guild.roles.everyone,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
},
{
id: button.clicker.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
}
]
})
In DiscordJS 13 (maybe previous version as well), you must passed the type parameter for it to work if the role/user is not yet cached.
button.guild.channels.create('cs', {
type: "text",
parent_id: '802172599836213258',
permissionOverwrites: [
{
id: button.guild.roles.everyone,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
},
{
id: button.clicker.id,
type: "member",
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
}
]
})
Explanation:
The reason why it does not work without the type is due to this code when creating a channel:
https://github.com/discordjs/discord.js/blob/34ba9d1c4c80eff7e6ac199a40232d07491432cc/packages/discord.js/src/structures/PermissionOverwrites.js#L183
if (typeof overwrite.id === 'string' && overwrite.type in OverwriteType) {
return {
id: overwrite.id,
type: overwrite.type,
allow: PermissionsBitField.resolve(overwrite.allow ?? PermissionsBitField.DefaultBit).toString(),
deny: PermissionsBitField.resolve(overwrite.deny ?? PermissionsBitField.DefaultBit).toString(),
};
}
const userOrRole = guild.roles.resolve(overwrite.id) ?? guild.client.users.resolve(overwrite.id);
if (!userOrRole) throw new TypeError(ErrorCodes.InvalidType, 'parameter', 'User nor a Role');
const type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;
And here when using the PermissionOverwriteManager:
https://github.com/discordjs/discord.js/blob/5d8bd030d60ef364de3ef5f9963da8bda5c4efd4/packages/discord.js/src/managers/PermissionOverwriteManager.js#L95
async upsert(userOrRole, options, overwriteOptions = {}, existing) {
let userOrRoleId = this.channel.guild.roles.resolveId(userOrRole) ?? this.client.users.resolveId(userOrRole);
let { type, reason } = overwriteOptions;
if (typeof type !== 'number') {
userOrRole = this.channel.guild.roles.resolve(userOrRole) ?? this.client.users.resolve(userOrRole);
if (!userOrRole) throw new TypeError(ErrorCodes.InvalidType, 'parameter', 'User nor a Role');
type = userOrRole instanceof Role ? OverwriteType.Role : OverwriteType.Member;
}
....
If type is not given, then it will try to call this.client.role.resolve and this.client.users.resolve which will only check the cache.
Either update your cache by fetching the user/role, or precise the type to avoid to have to check the cache.
The only possible problem I can see is the button.guild.roles.everyone, there's no everyone property in button.guild.roles, the everyone id is the guild id, so if you replace button.guild.roles.everyone with button.guild.id, it should fix the problem.
button.guild.channels.create('cs', {
type: "text",
parent_id: '802172599836213258',
permissionOverwrites: [
{
id: button.guild.id,
deny: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
},
{
id: button.clicker.id,
allow: ['VIEW_CHANNEL', 'SEND_MESSAGES', 'READ_MESSAGE_HISTORY']
}
]
})

How do I specify options for commands?

I want to specify choices for an option for my command in Discord.js. How do I do that?
The command:
module.exports = {
name: 'gifsearch',
description: 'Returns a gif based on your search term.',
options: [{
name: 'type',
type: 'STRING',
description: 'Whether to search for gifs or stickers.',
choices: //***this is the area where I have a question***
required: true
},
{
name: 'search_term',
type: 'STRING',
description: 'The search term to use when searching for gifs.',
required: true,
}],
async execute(interaction) {
let searchTerm = interaction.options.getString('search_term')
const res = fetch(`https://api.giphy.com/v1/gifs/search?q=${searchTerm}&api_key=${process.env.giphyAPIkey}&limit=1&rating=g`)
.then((res) => res.json())
.then((json) => {
if (json.data.length <= 0) return interaction.reply({ content: `No gifs found!` })
interaction.reply({content: `${json.data[0].url}`})
})
},
};
I have read the discord.js documentation/guide and I know about the .addChoice() method, but it doesn't look like that will be compatible with my bot's current code.
The discord.js api describes this as ApplicationCommandOptionChoices.
So you basically just insert an array of this in your choices.
module.exports = {
...
choices: [
{
name: "name to display",
value: "the actual value"
},
{
name: "another option",
value: "the other value"
}
]
...
};

How can I make a discord bot creates a channel on a command

Hello there
I'm currently working on a discord server called "file storing" (name is subject to change)
and I'm wondering if there is a way that: when a user types a certain command such as ">fs create"
the bot creates a channel like "channelid-storing" that is only visible to the user that used the command?
Channel name format is going to show the channels id then "-storing"
let client = new Discord.Client()
client.on("message", message => {
if(message.content === "ping") {
message.channel.send("pong")
}
client.on(message, async (message) => {
if(message === `>fs create`){
const channel = await message.guild.channels.create("channel", {
permissionOverwrites: [
{
id: message.guild.roles.everyone,
deny: ["VIEW_CHANNEL"],
},
{
id: message.member,
allow: [
"VIEW_CHANNEL",
"SEND_MESSAGES",
"EMBED_LINKS",
"ATTACH_FILES",
"READ_MESSAGE_HISTORY",
],
},
{
id: message.guild.me,
allow: [
"VIEW_CHANNEL",
"SEND_MESSAGES",
"EMBED_LINKS",
"ATTACH_FILES",
"READ_MESSAGE_HISTORY",
],
},
],
});
channel.editName(`${channel.id}-store`)
}
})
})
client.login(process.env.DISCORD_TOKEN);```
You just have to supply user permissions, when creating the channel
if(commandname === `> fs create`){
const channel = await message.guild.channels.create("channelname", {
permissionOverwrites: [
{
id: message.guild.roles.everyone,
deny: ["VIEW_CHANNEL"],
},
{
id: message.member,
allow: [
"VIEW_CHANNEL",
"SEND_MESSAGES",
"EMBED_LINKS",
"ATTACH_FILES",
"READ_MESSAGE_HISTORY",
],
},
{
id: message.guild.me,
allow: [
"VIEW_CHANNEL",
"SEND_MESSAGES",
"EMBED_LINKS",
"ATTACH_FILES",
"READ_MESSAGE_HISTORY",
],
},
],
});
channel.editName(`${channel.id}-store`)
}
Btw, you have to add your stuff, so that it properly works, the example above, shows how the channel creating works..

Guild in the presence of discord bot

I am trying to put the guild in the presence of discord bot, but I can't get it, the version is v12 this is my code. example (Watching 76 servers)
function presence(){
client.user.setPresence({
status: "online",
activity: {
name: "{len(bot.guilds)}",
type: "WATCHING",
}
});
}
Sample image:
I'm assuming you're trying to show the number of guilds the bot is in.
// Discord.js v13
client.user.setPresence({
status: 'online',
activities: [{
name: `${client.guilds.cache.size} servers`,
type: 'WATCHING'
}]
)
// Discord.js v12
client.user.setPresence({
status: 'online',
activity: {
name: `${client.guilds.cache.size} servers`,
type: 'WATCHING'
}
)
Alternatively, you can use setActivity if you are not changing the client's status (as it defaults to online):
client.user.setActivity(`${client.guilds.cache.size} servers`, {type: 'WATCHING'})

overwritePermissions not functioning| Discord.js MASTER

I am in the process of converting my bot to work with the master branch of discord.js
I got to my ticket command and they changed a lot, I managed to do everything apart from the overwritePermissions section. I am unsure of why the code is not working.
If I remove the bottom 2 overwritePermissions sections, it works fine, but with all 3 present, none execute.
let role = message.channel.guild.defaultRole;
let role2 = message.guild.roles.find(x => x.name === "Support Team");
message.guild.channels.create(`ticket-test`, {type: 'text'}).then(c => {
c.overwritePermissions({
permissionOverwrites: [{
id: role.id,
deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
}]
});
c.overwritePermissions({
permissionOverwrites: [{
id: role2.id,
deny: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
}]
});
c.overwritePermissions({
permissionOverwrites: [{
id: message.author.id,
allow: ['SEND_MESSAGES', 'VIEW_CHANNEL'],
}]
});
});
I have done console.log(role.id) and console.log(role2.id and they both show the correct id, it's just not executing the code.
Instead of repeating the overwritePermissions() method, you can simply list your permission overwrites in the channel creation options in the first place. type's default is already a text channel, so you can also omit that option. Finally, you should always be catching Promises.
const everyone = message.guild.defaultRole;
const support = message.guild.roles.find(r => r.name === 'Support Team');
message.guild.channels.create('ticket-test', {
permissionOverwrites: [
{
id: everyone.id,
deny: 'VIEW_CHANNEL'
}, {
id: support.id,
allow: 'VIEW_CHANNEL'
}, {
id: message.author.id,
allow: 'VIEW_CHANNEL'
}
]
}).catch(err => console.error(err));
let role2 = message.guild.roles.find(x => x.name === "💻Mods");
but if you want
for this:
async function jointocreatechannel(user) {
//log it
console.log(" :: " + user.member.user.username + "#" + user.member.user.discriminator + " :: Created a Support")
//user.member.user.send("This can be used to message the member that a new room was created")
await user.guild.channels.create(`${user.member.user.username}'s Support`, {
type: 'voice',
parent: "973217461577080903", //or set it as a category id
}).then(async vc => {
//move user to the new channel
user.setChannel(vc);
//set the new channel to the map
jointocreatemap.set(`tempvoicechannel_${vc.guild.id}_${vc.id}`, vc.id);
//change the permissions of the channel
let role2 = message.guild.roles.find(x => x.name === "💻Mods");
await vc.overwritePermissions([
{
id: user.id,
allow: ['MANAGE_CHANNELS'],
},
{
id: user.guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: role2.id,
deny: ['VIEW_CHANNEL'],
},
]);
})
}
}

Categories

Resources