How to make bot autorole when someone join voice channel? - javascript

How can I make my bot give a role when a user joins a specific voice channel ?
const team1role = message.guild.roles.find((role) => role.name === "Team 1");
const team1members = team1role.members;
const chan1 = message.guild.channels.find((channel) => channel.name === "Team 1")
team1members.forEach((member) => {
member.setVoiceChannel(chan1);
});

You'll want to go inside your main file (the same one as where you put your client.login(TOKEN) function) and create a new event :
client.on('voiceStateUpdate', (oldState, newState) => {
if (!newState.channel || !newState.member) return; // Triggered if the user left a channel
const testChannel = newState.guild.channels.cache.find(c => c.name === 'Team 1');
if (newState.channelID === testChannel.id) { // Triggered when the user joined the channel we tested for
const role = newState.guild.roles.cache.find(r => r.name === 'Team 1');
if (!newState.member.roles.cache.has(role)) newState.member.roles.add(role); // Add the role to the user if they don't already have it
}
});

Related

remove message.author reaction

I wanted to remove the user's reaction when he reacts, leaving only one, the bot's own
I've already seen some tutorials on this, but I haven't found this method
const test = new Discord.MessageEmbed()
.setColor("random")
.setTitle("help")
.setDescription(`**this a test**`)
message.channel.send(test).then(msg => {
msg.react('📚').then(r => {
msg.react('📌').then(r => {
})
})
const hiFilter = (reaction, user) => reaction.emoji.name === '📚' && user.id === message.author.id;
const hi2Filter = (reaction, user) => reaction.emoji.name === '📌' && user.id === message.author.id;
const edit = msg.createReactionCollector(hiFilter);
const edit2 = msg.createReactionCollector(hi2Filter);
edit.on('collect', r2 => {
test.setTitle("test edited")
test.setDescription("edited")
msg.edit(test)
})
})
This code removes the reaction if the ID of the user that added the reaction is not your bot's ID. Replace <Client> with whatever you've declared your Discord client as.
edit.on('collect', (r2, user) => {
if (user.id !== <Client>.user.id) r2.remove().catch(console.log);
test.setTitle("test edited");
test.setDescription("edited");
msg.edit(test);
});

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 bot joining vc and playing audio

I want to detect when a specific user joins a voice channel so that the bot joins that same voice channel and starts playing audio.
My code doesn't work: the bot doesn't join the channel. How can I fix this?
const bot = new Discord.Client();
bot.login('BOT TOKEN');
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
let textChannel = oldMember.guild.channels.get('TEXTCHANNEL ID')
if(oldUserChannel === undefined && newUserChannel !== undefined) {
if (newMember.id === 'MEMEBER ID') //Member 1
{
newUserChannel.join()
.then(connection => {
console.log("Joined voice channel!");
const dispatcher = connection.playFile("C:\Users\NAME\Documents\Welcome_Bot\music\bossman.mp3");
dispatcher.on("end", end => {newUserChannel.leave()});
})
.catch(console.error);
}
else if (newMember.id === 'MEMEBER ID') //Member 2
{
textChannel.send('Hello Member 2')
}
else if (newMember.id === 'MEMEBER ID') //Member 3
{
textChannel.send('Hello Member 3')
}
else //Random
{
textChannel.send("Hello")
}
}
});

Checking the channel name

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') {
...
}

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