client.on("presenceUpdate", (oldPresence, newPresence) => {
const user = newPresence.user;
// check if the user has a custom status
if (user.presence.status === "online" && user.presence.activities.length > 0) {
const customStatus = user.presence.activities[0].state;
// check if the custom status is ".gg/1655"
if (customStatus === ".gg/1655") {
// add the desired role to the user
const role = newPresence.guild.roles.find(r => r.id === "1068282448267460668")
newPresence.member.roles.add(role);
}
}
});
client.setInterval(function() {
main();
},30000);
its not giving an error but its not giving an role too
i tried to make the bot gives a role to someone who has the specified custom status
Related
When I'm online the bot gives me a role, as soon as I go offline the bot removes that role from me.
When it removes the role, I want the bot to give the role to a specific user. How can I do that?
I have my current code below:
client.on('presenceUpdate', (oldPresence, newPresence) => {
const member = newPresence.member;
if (member.id === 'user.id') {
if (oldPresence.status !== newPresence.status) {
var gen = client.channels.cache.get('channel.id');
if (
newPresence.status == 'idle' ||
newPresence.status == 'online' ||
newPresence.status == 'dnd'
) {
gen.send('online');
member.roles.add('role.id');
} else if (newPresence.status === 'offline') {
gen.send('offline');
member.roles.remove('role.id');
}
}
}
});
You could get the other member by its ID. newPresence has a guild property that has a members property; by using its .fetch() method, you can get the member you want to assign the role to. Once you have this member, you can use .toles.add() again. Check the code below:
// use an async function so we don't have to deal with then() methods
client.on('presenceUpdate', async (oldPresence, newPresence) => {
// move all the variables to the top, it's just easier to maintain
const channelID = '81023493....0437';
const roleID = '85193451....5834';
const mainMemberID = '80412945....3019';
const secondaryMemberID = '82019504....8541';
const onlineStatuses = ['idle', 'online', 'dnd'];
const offlineStatus = 'offline';
const { member } = newPresence;
if (member.id !== mainMemberID || oldPresence.status === newPresence.status)
return;
try {
const channel = await client.channels.fetch(channelID);
if (!channel) return console.log('Channel not found');
// grab the other member
const secondaryMember = await newPresence.guild.members.fetch(secondaryMemberID);
if (onlineStatuses.includes(newPresence.status)) {
member.roles.add(roleID);
secondaryMember.roles.remove(roleID);
channel.send('online');
}
if (newPresence.status === offlineStatus) {
member.roles.remove(roleID);
secondaryMember.roles.add(roleID);
channel.send('offline');
}
} catch (error) {
console.log(error);
}
});
What I'm trying to do is to get username for users who reacts on that message. It's working good but when the bot restarts only new reactions work.
how to make it send all users reactions
client.on('ready', () => {
client.guilds.get('guild_id').channels.get('chnl_id').fetchMessage('msg_id');
});
client.on('messageReactionAdd', (reaction, user) => {
const { message} = reaction;
if(message.channel.id == 'chnl_id'){
if(reaction.emoji.name === "✅") {
message.guild.fetchMember(user.id).then(member => {
if(user.bot) return;
else {
message.channel.send(reaction.users.map(u => u.username.toString()))
}
})
}}});
If you have the message, then you can filter the reaction of that message by emojis:
const reaction = await message.reactions.cache.filter(r=> r.emoji.name === '✅').first().fetch();
Then you can fetch all reactions with that specific emoji:
await reaction.users.fetch();
Then you can filter from that if you want to (for example your own bot), with:
const filteredReactions = reaction.users.cache.filter(r=> !r.bot);
And don't forget to put these in an async function.
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'
),
});
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
}
});
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...
}
});