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

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.

Related

how to mention current channel to another channel

I'd like it so when I type .done it'll send the channel the command was ran in, and send it to another channel. It needs to be mentioned (Clickable) The current code I have doesn't mention the channel, but only the channel’s name. I have no idea what I'm doing, I've just been copying from the internet.
if (command == "done")
return client.channels.cache.get('744836547391652000') .send(message.channel.send)
To mention a channel you need to convert it to a string using the channel's .toString() method. Or simply use template literals like this:
client.channels.cache.get("744836547391652000").send(`${message.channel}`);

Discord.js | Assign specific roles uppon a user receiving other roles

I'm developing a bot to work on a server which already uses other bots for role assignments and such. All I need it to do is to, whenever a user is assigned a specific role, give that same user another specific role. The other parts of the bot were figured out just fine, but I cannot seem to get this one straight. Does anyone know how to do it? Is it possible? Thanks in advance.
You can track when a role is assigned with the guildMemberUpdate event.
client.on('guildMemberUpdate', (oldMember, newMember) => {
// first, make sure the role was assigned
if (
!oldMember.roles.cache.has('Role ID') &&
newMember.roles.cache.has('Role ID')
) {
newMember.roles.add('Role ID');
// do something...
};
});

I cant get an item from an array for my Discord bot to use to take action

I am trying to program a bot for a server, and I am currently working on making a 'Mute' command. My idea was to make it so when the command is called, the bot must first check if the person has the role which allows them to mute other members, if that condition is met, then the bot would take the second argument, aka the Discord id of the member which must be muted, and give them a role which inhibits them from speaking in the server.
For some reason when I was testing the code, I wasn't able to get past the bot checking whether the second argument was a valid ID for a member of the server.
Here is the mute command code:
if (message.member.roles.cache.has('765334017011613717')) {
console.log('Oh oh');
const person = message.guild.member(
message.mentions.users.first() || message.guild.members.get(args[1])
);
if (!person) {
console.log('Oh oh 2');
}
} else message.channel.send('You do not have permission to use this command.');
Note: The console.log() functions were added so I could see where the problem was happening when running the code.
There are two things that you might do in order to make this work with the first being more of a suggestion.
First, ideally you always want to check if the role exists and return if it doesn't. This prevents you from having to put your entire logic to mute the person into the if statement. You do that like this:
if (!message.member.roles.cache.has('your role ID')) {
return message.channel.send('You do not have permission to use this command.');
}
The actual problem in your code stems from your way of finding the member. The right way to do that is, either checking the first mention of a member or taking a provided ID from your first argument. Your first argument being not args[1] but args[0]. If the ID is being used you need to use the cache property. Source
let muteMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
You should then check if a member has actually been found and return if not.
if (!muteMember) {
return message.channel.send("That member doesn't exist!")
}
Note: You already have this in your code but I wanted to include it for completeness.
After that you can continue with your code to mute someone.

discord.js user specific snowflake id commands

so im trying to get my bot to detect a user id to allow a command so a command for only one user and im having a bit of trouble understanding what i am doing wrong
run(message) {
//member id 184191493919997952
if(message.member.id.find('id', '184191493919997952')){
return message.say('True')
}else{
return message.say('False')
}
}}
so ive tried to set it up to check whoever used the command and if their snowflake id isnt equal to the one listed then it should return false but i keep getting a TypeError am i missing something?
ID is a property of User (which is message.author).
You can directly use "==" to check if it equals to something.
run(message) {
if (message.author.id == "184191493919997952") {
message.reply("true")
} else {
message.reply("false")
};
};
https://discord.js.org/#/docs/main/stable/class/User?scrollTo=id
Also, there is no method called "say" of message.
You can check available methods here (for example, message.reply()):
https://discord.js.org/#/docs/main/stable/class/Message

Add role to user from DMs?

So I'm making a verification bot for one of my Discord Servers, and I've come across a problem.
Purpose:
When the user first joins, they will be only allowed to talk in one specific channel, the verification channel. When they type $verify {username on-site}, the bot will reply in-server telling the user to check their DMs to continue. A unique string linked to a verification row in the database will be given, and the discord user will be told to change their status on-site to the given unique ID. Once they do that, they will be given the "Verified" role and will be able to speak on the discord.
The problem:
When everything goes right, and the user is ready to be given the role on the discord server, the bot will not give the role. I'm assuming this is because the user that the bot is trying give the role to is not linked to the actual discord server for it, because it's in DMs.
What I'm looking for:
If possible, give me an example on how to give a role to a user on a discord server from DMs, or documentation. That would be really helpful!
Code: I'll only include the part that is important, there is no other errors.
snekfetch.get("https://vertineer.com/api/getUniq.php?discord_id="+message.author.id).then(function(r){
console.log("here 2");
let uniq = JSON.parse(r.text);
if(uniq.id == uniq.status){
message.member.addRole("Verified");
message.reply("Success!");
} else {
message.reply("Looks like you didn't change your status to `"+uniq.id+"`, try again with `$finish`.");
}
});
If this is only to work for one server you can do:
client.guilds.get('myguildID').members.get(message.author.id).addRole('193654001089118208')
And also, to give a role to a user you can't use the name. You need to use the ID or the Role Object.
If you would prefer to use the name you would need to do something like this:
var role = client.guilds.get('myguildID').roles.find(role => role.name === "Verified");
client.guilds.get('myguildID').members.get(message.author.id).addRole(role);
This is what worked for me:
client.guilds.fetch(<guild.id>).then(async(guild) => {
let role = guild.roles.cache.find(r => r.name === <role.name>);
let member = await guild.members.fetch(<member.id>);
member.roles.add(role);
});
Substitute <guild.id>, <member.id> and <role.name> for the desired values respectively

Categories

Resources