Checking the channel name - javascript

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', (oldMessage, newMessage, role, args, guild) => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
if (message.content === '.') {
if (message.guild.channel === 'dot-wars') {
message.guild.members.forEach(member => {
var role = message.guild.roles.find(role => role.name === 'Dot Master!');
member.removeRole(role);
})
}
var role = message.guild.roles.find(role => role.name === 'Dot Master!');
message.member.addRole(role);
}
});
okay so what i want to do is when someone sends a '.' the bot will remove the 'Dot Master!' role from everyone in the server and then add the 'Dot Master!' role to the person that sent it, but only if it is in the 'dot-wars' channel.

A text channel has a name property for reading its name. However, make sure you're checking the channel the message was sent in, not the guild (Message#channel).
if (message.channel.name === 'dot-wars') {
...
}

Related

add roles command not working (discord.js)

I am Coding my own discord bot for my server, I have a issue that the bot says no role doesn't exit even if I mentions the role. Here is the code:
const Discord = require('discord.js')
const client = new Discord.Client()
client.on("message", message => {
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase()
if (command === "add") {
if (!message.member.hasPermission("MANAGE_ROLES"))
return message.channel.send("Insufficient permissions")
const member = message.mentions.members.first()
if (!member)
return message.channel.send("No user mentioned")
const add = args.slice(1).join(" ")
if (!add)
return message.channel.send("No role said")
const roleAdd = message.guild.roles.find(role => role.name === add);
if (!roleAdd)
return message.channel.send("Role does not exist")
if (member.roles.has(roleAdd.id)){
return message.channel.send("User already has role")
}
if (member) {
member.addRole(roleAdd).catch((error) =>{
message.channel.send("I cant add...")
}).then((member) => {
message.channel.send(`:thumbsup: ${roleAdd} added to ${member.displayName}`)
})
}
}
})
client.login(config.token)
What did I made a mistake? Thanks.
First make sure you are using discord.js v12. Type npm i discord.js#latest in your terminal. Here you can see all changes in discord.js v12.
If you are using discord.js v12 now, this code should work for you:
if (!message.member.hasPermission("MANAGE_ROLES"))
return message.channel.send("Insufficient permissions")
const member = message.mentions.members.first()
if (!member)
return message.channel.send("No user mentioned")
const add = args.slice(1).join(" ");
if (!add)
return message.channel.send("No role said")
const roleAdd = message.guild.roles.cache.find(role => role.name === add);
if (!roleAdd)
return message.channel.send("Role does not exist")
if (member.roles.cache.has(roleAdd.id)) {
return message.channel.send("User already has role")
}
if (member) {
member.roles.add(roleAdd).catch((error) => {
message.channel.send("I cant add...")
}).then((member) => {
message.channel.send(`:thumbsup: ${roleAdd} added to ${member.displayName}`)
})
}
Mention the role you want to add won't work because here
const roleAdd = message.guild.roles.cache.find(role => role.name === add);
you just find the role by its name. To fix that you can change that line into:
const roleAdd = message.guild.roles.cache.find(role => role.name === add) || message.mentions.roles.first();
This will allow a user to mention the role as well.

Can i set my created channels permisions like only my staff role can edit or delete it. My code so far:

I want my staff role can edit, delete this created channel.
client.on('message', message =>{
if (!message.content.startsWith('*open-channel')) return; //This line for some bug happens in my bot
if (message.channel.id !== '759430340972118026') return; // I set a channel for the users can only use
//this command in
if(message.author.bot || message.channel.type === "dm") return; //For bugs again
if(!message.member.roles.cache.some(role => role.name === 'OWNER')) { //This command only
//for owner role now.
return message.reply('You should be OWNER for using this command.').then(message => {
message.delete({ timeout: 8000 })
})
}
const messageArray = message.content.split(' ');
const cmd = messageArray[0];
const args = messageArray.slice(1).join(' ').toUpperCase();
if (message.content.startsWith('*open-channel'))
var kanal = message.guild.channels.create(`${args} - ${message.author.tag}`,{type : 'voice'})
.then(channel => channel.setParent(message.guild.channels.cache.find(channel => channel.name === "USER CHANNELS"))); //setParent moves my channel to choosen catagory. And 'args - message.author.tag' is channel name
message.channel.send("Its done now buddy :3");
})
Sorry for my bad english if i wrote something wrong. :(
You can use GuildChannelManager.create.options.permissionOverwrites(). Also, you can set the channel parent directly when creating it.
message.guild.channels.create(`${args} - ${message.author.tag}`, {
type: 'voice',
permissionOverwrites: [
{
id: '<Staff Role ID>',
allow: ['MANAGE_CHANNELS'],
},
],
parent: message.guild.channels.cache.find(
(channel) => channel.name === 'USER CHANNELS'
),
});

discord.js v12 send message to first channel

Hi how can i send message to first channel where bot can send messages in Discord.js v12.
Please help me.
This didnt work for me:
client.on("guildCreate", guild => {
let channelID;
let channels = guild.channels;
channelLoop:
for (let c of channels) {
let channelType = c[1].type;
if (channelType === "text") {
channelID = c[0];
break channelLoop;
}
}
let channel = client.channels.get(guild.systemChannelID || channelID);
channel.send(`Thanks for inviting me into this server!`);
});
You can do it like this.
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", (guild) => {
const channel = guild.channels.cache.find(
(c) => c.type === "text" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
);
if (channel) {
channel.send(`Thanks for inviting me into this server!`);
} else {
console.log(`can\`t send welcome message in guild ${guild.name}`);
}
});
Updated for Discord v13
Note the c.type has been changed from text to GUILD_TEXT
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("guildCreate", (guild) => {
const channel = guild.channels.cache.find(
(c) => c.type === "GUILD_TEXT" && c.permissionsFor(guild.me).has("SEND_MESSAGES")
);
// Do something with the channel
});
Update for from v13 to v14
on event of guildCreate is now Events.GuildCreate
c.type changed from GUILD_TEXT to ChannelType.GuildText
guild.me is now guild.members.me
const { Events, ChannelType } = require('discord.js')
const client = new Client({
//your intents here
intents: []
})
client.on(Events.GuildCreate, guild => {
const channel = guild.channels.cache.find(c =>
c.type === ChannelType.GuildText &&
c.permissionsFor(guild.members.me).has('SEND_MESSAGES')
)
//do stuff with the channel
})

addRole to a mentioned user isn't working

I'm trying to make a discord bot, which can add a specific role to an user.
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', (message) => {
let allowedRole = message.guild.channels.find(channel => channel.name === "Streamer");
let gRole = message.guild.channels.find(channel => channel.name === "Stream 1");
mention = message.mentions.users.first();
let member = message.mentions.users.first();
if (message.content === "addRole") {
members.get(message.mentions.user.id).addRole(gRole);
}
});
client.login('mytoken');
I expect that the bot can add role to a specific person
First of all, your first mistake was, that you searched for channel names and not role names in your variable gRole and allowedRole. I changed this 2 variables, so they search for roles instead of channels.
You defined twice the same, once as mention and once as member = message.mentions.users.first(). Because of this, I removed your variable named mention and used the variable member. I found that the code for this variable didn't match the name of the variable because your code returned the user Object and I changed it to the member Object (because your variable is named member).
Lastly, I used your predefined variable member and assigned the role gRole to it, so you don't have to get the member another time.
Try to use the following code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
const allowedRole = message.guild.roles.find(role => role.name === 'Streamer'); // isn't used
const gRole = message.guild.roles.find(role => role.name === 'Stream 1');
const member = message.mentions.members.first();
if (message.content === 'addRole') {
member.addRole(gRole);
}
});
client.login('mytoken');

How to delete all user inputs except one command in specific channel?

I was working on a discord bot and for a verification channel. I want users to type only the /verify command: every message or command except /verify they type should get deleted automatically. How can I do this?
Current code:
if (command === "verify") {
if (message.channel.id !== "ChannelID") return;
let role = message.guild.roles.find(rol => rol.name === 'Member')
const reactmessage = await message.channel.send('React with 👌 to verify yourself!');
await reactmessage.react('👌');
const filter = (reaction, user) => reaction.emoji.name === '👌' && !user.bot;
const collector = reactmessage.createReactionCollector(filter, {
time: 15000
});
collector.on('collect', async reaction => {
const user = reaction.users.last();
const guild = reaction.message.guild;
const member = guild.member(user) || await guild.fetchMember(user);
member.addRole(role);
message.channel.send(`Verification Complete.. ${member.displayName}. You have got access to server. `)
});
message.delete();
}
You can add a check at the top of your client.on('message') listener:
client.on('message', message => {
let verified = !!message.member.roles.find(role => role.name == 'Member');
// ... command parsing ect...
if (!verified && command == 'verify') {...}
else if (verified) {
// other commands...
}
});

Categories