I am making a command where it will overwritepermissions in the channel settings, I have made it change the channel settings for the user but can not figure out how to delete the user from the channel settings. The code below is one of the ways I have tried to do this if you could please help me that would be very helpful thxs.
let usr = msg.mentions.members.first();
msg.guild.channels.cache.forEach(channel => {
channel.replacePermissionOverwrites({
"overwrites": channel.permissionOverwrites.filter(o => o.id !== usr.id)
});
})
You can use overwrites.delete():
let usr = msg.mentions.members.first();
message.guild.channels.cache.forEach((channel) => {
const permOverwrites = channel.permissionOverwrites.get(usr.id);
if(permOverwrites) permOverwrites.delete();
});
Related
I want to make my bot autorole if username of anyone member contains a specific tag and the code i made isn't working, bot starts fine but didn't assign role if user have tag in username than also so i thought to use stackoverflow to get some help
Here is the code:
client.on("userUpdate", async (oldUser, newUser) => {
if (oldUser.username !== newUser.username) {
let tag = "⚡";
let server_id = "793143091350470708";
let role = "888436049620119562";
if (newUser.username.includes(tag) && !client.guilds.get(server_id).members.get(newUser.id).roles.has(role)) {
client.guilds.get(server_id).members.get(newUser.id).addRole(role)
} if (!newUser.username.includes(tag) && client.guilds.get(server_id).members.get(newUser.id).roles.has(role)) {
client.guilds.get(server_id).members.get(newUser.id).removeRole(role)
}
}
})
You would require to define USER_UPDATE, GUILD_MEMBER_UPDATE, and
PRESENCE_UPDATE intents if you are on discord.js version 13 and further enable the members and presence intent too, they can be located in the Discord Developer Portal further I would like to suggest using the Client#guildMemberUpdate listener instead since your code has nothing to do with presence you would not need the "extra" intents.
Additional Information
Directed by this comment It has come to my notice that you are using version 12 of discord.js so you would want to make adequate changes to your code for that ( your code as it currently is, is clearly written for discord.js v11) the following changes would be made in your code:
client.on("guildMemberUpdate", async (oldUser, newUser) => {
if (oldUser.username !== newUser.username) {
let tag = "⚡";
let server_id = "793143091350470708";
let role = "888436049620119562";
if (newUser.username.includes(tag) && !client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.has(role)) {
client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.add(role)
} if (!newUser.username.includes(tag) && client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.has(role)) {
client.guilds.cache.get(server_id).members.cache.get(newUser.id).roles.remove(role)
}
}
})
is there any way to put the user who created the channel in .setAuthor? I searched in https://discord.js.org/#/docs/main/stable/class/GuildChannel but found nothing
client.on("channelCreate", function(channel) {
const logchannel = channel.guild.channels.cache.find(ch => ch.name === "logchannel")
if (!logchannel) return
const embed = new Discord.MessageEmbed()
.setTitle("Channel created)
.setDescription(`Channel <#${channel.id}> has created.`)
.setTimestamp()
.setAuthor(//user who created the channel)
logchannel.send(embed)
})
I think it's best to use the AuditLogs, you may want to take a look at it.
I have a porblem. My leave message. There I have:
client.on("guildMemberAdd", member => {
const welcomeChannel = member.guild.channels.cache.find(
channel => channel.name === "welcome"
);
welcomeChannel.send(`Hosgeldin :heart: ${member}`);
});
client.on("guildMemberRemove", member => {
const welcomeChannel = member.guild.channels.cache.find(
channel => channel.name === "gelen-giden"
);
welcomeChannel.send(`Bye ${member}!`);
});
and then when somebody left then came <#id>.
I want to find his id because then I can make an link of the person who left.
It look like https://discordapp.com/users/732569703142129685 .
And then it should write 'Member' and this 'member' should be the link.
Please in v12.
Discord returns <#id> because the member that left doesn't have more guilds(servers) in common with you and he is no more in your cache. If you need his id for some deletion of rows in db you can get it as member.id
When a user leaves, he is no longer on the guild, so you can't find a channel on a server he isn't anymore in. You'll need to use client.channels.cache.find() in this case.
have a good day.
I have had reports off my support server of my discord js bot of my bot being abused in multiple ways.
I want to have a way upon launch of my bot to see a list of servers as well as invite links to those servers. I do not know the id of server or anything.
The most I've managed to find out how to do is this
var server = client.guilds.get("idk the id part");
console.log('I am in the following servers:');
server.createInvite().then(invite =>
console.log(server.name + "-" + invite.url)
);
});```
In your ready event (client.on('ready', () => {}), add the following lines:
client.guilds.tap(guild => {
console.log(`Name: ${guild.name} ID: ${guild.id}`);
})
This should output
Name: Test1 ID: 00000000000000
in the console for each server.
Also, considering that making a lot of invites might clog up your bot, and is generally more of a hindrance than help to server admins, you might consider making a createinvite command:
const svID = args[0];
const guild = client.guilds.find(guild => guild.id === svID);
const channel = guild.channels.find(channel => channel.position === 1);
channel.createInvite()
.then(invite => message.reply(invite.code));
You can either wait for the bot to emit it's ready event and loop through the guild collection:
client.once('ready', () => {
// client.guilds should be ready
});
or handle each guild individually:
client.on('guildCreate', (guild) => {
// The guild the bot just joined/connected to
// Should also be emitted when the bot launches
});
Either should work, but my recommendation would be the second approach, as this will also allow you to track join events whilst the bot is running.
Basically, I need to grab the permissions from the current text channel the user is in. I have already got the channel name, if I need to get the ID that should be pretty easy to do.
const Discord = require("discord.js");
module.exports.run = async (client, message, args) => {
let currentChannel = message.channel.name;
let category = message.channel.parent;;
message.guild.createChannel(currentChannel).then(mchannel => {
mchannel.setParent(category).then(() => {
message.channel.delete();
});
});
}
module.exports.help = {
name: "a.cleanchannel"
}
// Need the channel permissions to overwrite the new channel's permissions with the old ones
The expected results are that the channel should have the same permissions as the old one.
To answer your question directly, you can use GuildChannel#permissionOverwrites to create the new channel with the same permissions as the old one. For example...
message.guild.createChannel(message.channel.name, {
type: 'text',
permissionOverwrites: message.channel.permissionOverwrites
});
However, it looks like you're trying to clone a channel. To help make that easier, there's a method built into Discord.js for it - GuildChannel#clone(). You can use it like so...
message.channel.clone(undefined, true, true) // Same name, same permissions, same topic
.then(async clone => {
await clone.setParent(message.channel.parent);
await clone.setPosition(message.channel.position);
await message.channel.delete();
console.log(`Cloned #${message.channel.name}`);
})
.catch(console.error);