When my Discord bot joins a guild, I want to send a private message to the member inviting the bot. I am using Discord.js.
Now, i know there is a guildCreate event i can use on the client, that fires when the bot joins a guild.
This event however, only provides the Guild the bot has been invited in.
I've seen multiple bots sending me private messages this way, so i know there must be a way. One could get the guild owner and send him private message, but there is a chance that the owner is not the one inviting the bot. And unfortunately, since i was always the owner when it happened, i can't say if it was this or not.
Is there a way to retrieve the GuildMember, or the User that invited the bot ?
This requires view audit log permissions for the bot:
let logs = await guild.fetchAuditLogs()
logs = logs.entries.filter(e => e.action === "BOT_ADD")
let user = logs.find(l => l.target?.id === client.user.id)?.executor
//user is the inviter
Related
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'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.
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.
I am making a quiz, when a person answers the last question, they get a 'Completed' role. When they get the role, I want my bot to send me a private message. Is it possible to do so?
Thanks
The on_member_update event will fire whenever a role is added. We can check that the role is not in the roles of the member before the event and is present after the update:
from discord.utils import get
#bot.event
async def on_member_update(before, after):
role_name = "Completed"
if not get(before.roles, name=role_name) and get(after.roles, name=role_name):
await bot.owner.send(f"{after.name} has finished")
I need to get #everyone role ID to create a private chat for to server members (I'll restrict others except for those 2 users from reading and sending messages). How?
I tried typing
\#Admin
to get Admin's role ID as an example.
But then I typed
\#everyone
and it creates a regular message mentioning everyone on the server (like typing #everyone in chat). No ID.
How do I get #everyone role ID?
The ID for the everyone role is the Guild ID.
If you try to do <#&123123> replacing 123123 with your guild id, it will almost tag everyone
Update: 10th April 2020
Now to mention everyone, all you need is to send the string #everyone: message.channel.send("#everyone");
To obtain the ID for the everyone role, you need to have the guild, which you get on some events like: message, guildCreate, and others.
From there: <something>.guild.roles.everyone, and with that you should be able to get the ID with <something>.guild.roles.everyone.id.
If you don't have access to the guild on a event, you can get the guild by it's ID with something like:
client.guilds.cache.get('guildID').roles.everyone.id, if you know that it will be in cache. If you want to be safe, you can do it like this:
client.guilds.fetch('guildID').then(guild => {
let id = guild.roles.everyone.id;
// do something with id
}
or with await:
let guild = await client.guilds.fetch('guildID');
let id = guild.roles.everyone.id;
Need to get #everyone Role?
<guild>.roles.everyone
It will get a role object. You can check that in Role Class
Discord.js
V12
The guild class has a roles property, which is a RoleManager.
This manager has the property everyone:
The #everyone role of the guild
You can get its id with the id property of the role class:
client.on('message', (msg) => {
console.log(msg.guild.roles.everyone.id);
});
V11
In V11 the guild class has a defaultRole property.
So the code is slightly different:
client.on('message', (msg) => {
console.log(msg.guild.defaultRole.id);
});
Discord
On Discord side, with dev option activated, you can right click in almost everything to grab it ID. For example go in the server settings, in role and right click on a role, you should have a copy ID option.