How can I command my bot to lock the current channel? [closed] - javascript

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I want my bot to be able to lock down a channel when I use a command. This is my current code:
if (command === 'lock') {
guild.members.cache.find.permissions.remove('SEND_MESSAGES');
}
However, it's not working, and I'm not sure how to fix it.

The .updateOverwrite() method will update an existing permission setting or create one if none exists. This is how you use it:
.updateOverwrite(userOrRole, options, [reason])
Role: RoleResolveable (role object) or Snowflake,
User: UserResolveble (user object) or Snowflake
Reason: String (optional)
Example of how you could use this:
// First find the role we want to overwrite permissions for
const role = message.guild.roles.cache.find(r => r.name === "#everyone");
message.channel.updateOverwrite(role, { SEND_MESSAGES: false }, `Overwrite permissions`)
.then(() => console.log("Permissions overwritten."))
.catch(error => {
console.log("Oh no! Something went wrong! " + error.message);
});
If you want to overwrite all channels, simply map all channels in the guild and use channel.updateOverwrite(). You want to use updateOverwrite since overwritePermissions will replace all current permissions and replace it with the one you want to add or update.

First of all, you need message.channel to get the channel the command is sent in. Then, you can use channel.updateOverwrite():
// fun fact, the #everyone role shares the same id as the guild
message.channel.updateOverwrite(message.guild.id, {
SEND_MESSAGES: false // cannot send messages
}, 'Channel Lockdown')
.then(console.log)
.catch(consol.error); // handle any errors
The updateOverwrite() function accepts three parameters. First, a User or Role resolvable (typically the ID or object). In this case, I used message.guild.id, which is the ID of the #everyone role.
Then, the overwrite options. You can use any Permission Flag (for example, ADD_REACTIONS, and VIEW_CHANNEL) followed by false for denied, null for unset, and true for allowed.
Then finally, you can provide a reason which is what will appear in the audit logs!
You can do something similarly if you want to lockdown every channel:
// iterate a function over every channel in the guild
message.guild.channels.cache.forEach((channel) => {
message.channel.updateOverwrite(message.guild.id, {
SEND_MESSAGES: false
}, 'Channel Lockdown')
.then(console.log)
.catch(consol.error);
});
This will work the same with v11.x, just replace updateOverwrite() with overwritePermissions()

Related

discord.js Command that gives/removes role from everyone, and only an Admin can use it

I would like to make a command that gives everyone a role, and only an admin can use it.
I found this piece of code on the internet and I tried to modify it, but nothing is helping me out, and I've been reading the error, and I still get nothing
client.on("message", message => {
if (message.content === 'grimm!rainbow') {
let role = message.guild.roles.cache.find(r => r.name == 'Rainbow')
if (!role) return message.channel.send(`a Rainbow role was not found, create one and set it on top of all roles for this command to work!`)
message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role))
message.channel.send(`**${message.author.username}**, The Rainbow has been turned on!`)
}});
And another thing, I would like it if this command can only be used by an admin, but I've been struggling with code in that range, and I have no clue on how to work it.
If someone could help me out a little bit? and explain what I'm doing wrong, I would really appreciate it! Thanks!
To check if user have admin permission you need to using .hasPermission() docs here:
if(message.member.hasPermission('ADMINISTRATOR'){
// Doing Something
}
To add roles to all user you need to access guild members cached , doc here and then loop through all members , access their roles and you will have .add() for add specific role docs here. Similar for remove
message.guild.members.cache.filter(m => !m.user.bot).forEach(member => member.roles.add(role))

Custom status with guild number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have another discord.js query which involves custom statuses. I want my bot's custom status to be "Being Used In # Guilds/Servers" where # is the number of guilds the bot will be in.
Note that by "custom status" I don't mean a Playing status:
I mean a custom status (without "Playing"):
(Yes, that is OG Clyde, because I had my account since the Christmas Vacation 2020.)
So some answers say that Discord.js V12 only has Playing Statuses, not Custom Statuses. Now I had a conversation with #jabaa (in the comments) which said that I should share my own research and code, or else people will downvote. But I cannot share, because I don't know which code to use. I know one to display memberCount in the status, but not guild numbers.
Alright, I am using this in my bot too: here is the code
//...
client.on('ready', () => {
//...
client.user.setActivity('othertext' + client.guilds.cache.size, {type : 'PLAYING'})
}
client.on('guildCreate', guild => {
//same code as 'ready'
})
client.on('guildDelete', guild => {
//also the same code as 'ready'
})
Now this was from my human memory but this is a start, just modify it with any errors you may have, hopefully there are none.
NOTE: If you are just putting the number of guilds, for some reason, make sure the add the '' after the client.guilds.cache.size, otherwise you will get an error saying it got a number but expected string
DJS does not currently have support for Custom Statuses the same way users do, so this is not possible at this time.
As you can see here,
Bots cannot set a CUSTOM_STATUS, it is only for custom statuses
received from users
ClientUser.setStatus() takes a PresenceData type as an argument, which in turn has an object activity and its property type that can be either PLAYING, STREAMING, LISTENING, WATCHING or COMPETING

Is there a way to bind a bot to one specific channel?

I am making a Discord bot, and want to make it only be able to post on 1 specific channel. I can not make it have a lower role than a regular member, or it will not be able to give them roles.
I have tried an if/else statement based on the channel's ID.
case 'start':
if (message.channel === 615842616373805067) {
//somewhat unimportant command
return message.member.addRole('615166824849604619'),
message.channel.send(` ${message.author} has been given the role.)`,
message.delete);
//unimportant command ends here
} else {
console.log('Wrong channel, no role.')
}
I expect the bot to only be able to post in this one channel.
Instead of checking for the message.channel Class, try checking for the id property of that class
if(message.channel.id != "615842616373805067") return console.log('Wrong channel, no role.');
//rest of your code
Make sure to check the official documentation for detailed descriptions of each Class, Method, Property, etc.

Changing nickname of bot with a command [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I would like to make changes to my bot's name with a command like !Changenick nick - by 'nick' I mean the new bot's name. Could you guys help me? I just want bot to change username when someone writes !Changenick for example:
!changenick Freshname
bot changes his own name to Freshname
If you want to change a users nickname you need a GuildMember obj. You can apply this to the bot by searching the guild's member prop for the bots id. The following line will allow you to change the bot's username given a Message obj.
message.guild.me.setNickname('NICKNAME'); updated based on suggestion from #slothiful
If you need further assistance with how to create the entire command, reference https://discordjs.guide/ and https://discord.js.org/#/docs/
Update 1: I am having a hard time understanding your desired implementation with the examples you have provided.
Let's assume you want a command something like this: !changenick <#user> <nickname>
In this case, I would reference this. It explains how you can parse mentions. On to the actual question of changing the nickname, you can change the nickname of any user
(you will get this user via the mention) by using message.guild.members.find(user => user.id === mentionUser.id).setNickname('NICKNAME')
Update 2: I now see what you were getting at. My apologies. You do not need to restart the bot to allow the nickname to update. I have tested it.
The below code changes the bots nickname every 10 seconds.
const Discord = require('discord.js');
const Client = new Discord.Client();
Client.on('ready', () => {
let value = 0;
setInterval(() => {
Client.guilds.find(guild => guild.id === 'GUILD_ID').me.setNickname(value++);
}, 10000);
});
Client.login('TOKEN');
The important bit is
Client.guilds.find(guild => guild.id === 'GUILD_ID').me.setNickname('NICKNAME');
Edit: If this answers your question, mark the answer as accepted.

Role exclusive commands not working // Discord.js

My code is currently:
if (message.content == ",test") {
if (message.member.roles.find("name", "images")) {
message.channel.send( {
file: "http://www.drodd.com/images14/black11.jpg" // Or replace with FileOptions object
});
}
if (!message.member.roles.find("name", "images")) { // This checks to see if they DONT have it, the "!" inverts the true/false
message.reply('You need the \`images\` role to use this command.')
.then(msg => {
msg.delete(5000)
})
}
message.delete(100); //Supposed to delete message
return; // this returns the code, so the rest doesn't run.
}
If the user has the role 'images' I want the bot to send the image when they say ",test" and I want the user's ",test" message to be deleted after some seconds. However, this doesn't seem to be working.
I have tried to send the image without the role checking and that works.
How can I fix this?
This is because message.member.roles.find("name", "images") checks if the roles EXISTS in the guild. In order to find if someone has a role, you would use message.member.roles.has(myRole). To find the ID of a role dynamically, you would use this:
let myRole = message.guild.roles.find("name", "Moderators").id;.

Categories

Resources