'How can I deny @everyone and allow the author to view the channel?
I've been tinkering around with this for a while, and I have never done permission based channel creation. I am working on a ticket function for my bot and I can't quite seem to get it working. The idea is to create a channel with set permissions named the message.author entire tag with the permissions set to only allow the message.author to see the channel. For some reason I keep running into an error. How can I fix this? I've defined the everyone role and message.author should already be defined. Am I missing something?
if (message.channel.type !== "text") {
let active = db.fetch(`support_${message.author.id}`)
let guild = client.guilds.cache.get('took out ID')
let channel, found = true
try {
if (active) client.channels.cache.get(active.channelID).guild
} catch (e) {
found = false;
}
if(!active || !found) {
active = {}
let everyoneRole = msg.guild.roles.cache.find(r => r.name === '@everyone');
channel = await guild.channels.create(`${message.author.username}-${message.author.discriminator}`, {
parent: 'took out ID',
topic: `Support for ${message.author.tag} | ID: ${message.author.id}`,
permissionOverwrites: [
{
id: everyoneRole.id,
deny: ['VIEW_CHANNEL'],
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
],
})
Solution 1:[1]
You can either use roles.everyone
that returns the @everyone
role of the guild or simply use the guild's ID. Also, your overwrites are incorrect. It should contain two objects; one that denies views for everyone, and one that allows it for the member who sent the message.
Any of these will work:
let permissionOverwrites = [
{
id: message.guild.roles.everyone.id,
deny: ['VIEW_CHANNEL'],
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
];
let channel = await guild.channels.create(
`${message.author.username}-${message.author.discriminator}`,
{
parent: 'took out ID',
topic: `Support for ${message.author.tag} | ID: ${message.author.id}`,
permissionOverwrites,
},
);
Or:
let permissionOverwrites = [
{
id: message.guild.id,
deny: ['VIEW_CHANNEL'],
},
{
id: message.author.id,
allow: ['VIEW_CHANNEL'],
},
];
let channel = await guild.channels.create(
`${message.author.username}-${message.author.discriminator}`,
{
parent: 'took out ID',
topic: `Support for ${message.author.tag} | ID: ${message.author.id}`,
permissionOverwrites,
},
);
Solution 2:[2]
let everyoneRole = message.guild.id;
The role for @everyone
is the same as the guild id.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Zsolt Meszaros |
Solution 2 | Zsolt Meszaros |