I'm making a bot to detect when a user joined. The code below does not work when my test account joins. Does it only work when it's the first time a user joins. If so, how can I make it work everytime.
client.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const channel = member.guild.channels.cache.find(ch => ch.name === 'general-chat');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Welcome to the server, ${member}`);
});
You might have to enable Presence Intent on your bot settings on the Discord Developer Portal, which makes it able to detect new members.Here's an screenshot.
Related
I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel
I am trying to get a list of the permission that the user has in Discord. If it sends the message in the channel, it’s fine as we can use message.member.hasPermission, etc.
But what if the message is DM? I want my users to send a DM to the bot and the bot be able to check and see if the user has certain permissions.
I cannot find anything anywhere. I keep getting redirected to message.member, or message.guild which both are null when it’s in DM.
In DM no one has permissions. All you have is the permission to see messages and send messages which aren’t shown visually, or to bots. To make sure that it isn’t DM, just return if the channel type is dm, or guild is null.
if(!message.guild) return;
//before checking "perms"
If you want the permissions for a certain guild if the message is from DM, use this code
if(message.channel.type === 'dm') {
let guild = await client.guilds.fetch('THE ID OF THE GUILD YOU WANT TO SEE THE USER’S PERMS IN')
let member = await guild.members.fetch(message.author.id);
//you can now access member.permissions
}
Keep in mind await must be in an async callback.
You did not provide any code so I cannot give any more code than this.
You can fetch the member with the guild's context.
Fetching is recommended, as Guild#member() relies on cache and is also deprecated.
Member#hasPermission() will be deprecated as well, using MemberRoleManager#has() is recommended
The following example uses async/await, ensure you're inside an async function
// Inside async function
const guild = await client.guilds.fetch('guild-id');
const member = await guild.members.fetch(message.author.id);
const hasThisPermission = member.roles.cache.has('permission');
I've made a code which greets new members in a particular channel but I want to toggle it.
If a staff member uses !greet #channel then greet messages will be enabled in #channel. And if they use !greet, it disables greet message for the server.
GuildMemberAdd Event
client.on('guildMemberAdd', member =>{
const channel = member.guild.channels.cache.find(ch => ch.name == 'welcome');
if(!channel) return;
channel.send(`Welcome to **${member.guild.name}**, ${member}!`)
.then(message =>{
message.delete({ timeout: 5000 })
})
.catch(console.error);
});
More Info
I just want to make it a toggle command and I'm not sure how to. I'd be glad for an early answer.
So to toggle which channel will be send welcome message you need to store the channel ID somewhere like database or attach it with guild through collection .set . After that in your event before send message you get information from guild ID if it already set welcome channel so send message to that channel otherwise don't do anything
Discord,js 12.3.1
How I make bot sent message to every members with all servers?
I want to make bot at on_ready
client.on('ready', async (message) => {
});
Depending on how many servers your bot is on and how many users are in that server, your bot will most likely get banned. I do not recommend this in the slightest.
You can use GuildMemberManager.fetch(), which will fetch all members in a server, partnered with a few for... of loops.
// const client = new Discord.Client();
client.on('ready', async () => {
for (const guild of client.guilds.cache)
for (const user of await guild.members.fetch())
user.send('message').catch(() => console.log('User had DMs disabled'));
});
One problem with this method is that some users may be DMd multiple times if they're in multiple of these servers (aside from the obvious problems being it takes up a huge amount of memory, and your bot will probably get banned)
You could also enable enable the fetchAllMembers option when initializing the client.
Whether to cache all guild members and users upon startup, as well as upon joining a guild (should be avoided whenever possible)
Again, as the description says, this should be avoided whenever possible (I'm only showing it to be thorough).
// const client = new Discord.Client({ fetchAllMembers: true });
client.on('ready', () => {
for (const user of client.users.cache)
user.send('message').catch(() => console.log('User had DMs disabled'));
});
I cannot stress enough how bad of an idea this is.
So I'm making a kick command for my discord bot and I want the bot to DM the user telling them they have been kicked. So far I have got:
case 'kick':
const Embed = new
Discord.MessageEmbed()
.setTitle('Success!')
.setColor(0x00FF00)
.setDescription(`Successfully kicked **${args[2]}** \n \n**Message:** \n"${args.join(' ')}"`)
if(!message.member.hasPermission(['KICK_MEMBERS'])) return message.channel.send('*Error: You do not have permission to use* **kick**.');
if(!args[1]) return message.channel.send('*Error: Please specify a user to kick!*');
let member = message.mentions.members.first();
member.kick().then((member) => {
message.channel.send(Embed);
})
break;
So far, the user is successfully kicked so all this works.
All I need to know is how to make the bot DM the mentioned user to tell them they have been kicked. Any help is appreciated!
You are probably looking for this method: GuildMember#send()
member.send("Your DM Here");
Note that if the only reason your bot could send a member DMs was because of a mutual server in which the user had DMs from server members enabled (user disabled other types of stranger DMs), then your bot would not be able to send the DM. It would probably be a good idea to send them the DM and wait for the method's returned promise to resolve before kicking them, for a higher chance that the DM actually reaches them.