Reputation: 15
Good evening, having tried to make a system to know who invited someone, how many uses has this invitation (but I didn't succeed so I call you)... As soon as a new member arrives on the Discord. I want this message to be sent in a message.
Thank you in advance !
Upvotes: 0
Views: 616
Reputation: 5623
To track when a new member joins a server, you can listen to your client's guildMemberAdd
event.
client.on('guildMemberAdd', member => {
// Code in here is executed whenever
// someone joins a server the bot is
// also in.
});
1. In order to send a message, you first have to acquire the channel to send said message to. A Collection of GuildChannels is available with Guild.channels
. Unless you're using the channel's ID in the next step, you should filter the Collection to contain only TextChannels with Collection.filter
.
const channels = member.guild.channels.filter(channel => channel.type === 'text');
2. Assuming you have the ID of the channel, you can use Map.get()
(a Collection is a Map, but a Map is not a Collection) to retrieve the channel. Otherwise, you can find it with Collection.find()
, or simply select the first with Collection.first()
.
const channel = channels.get('someID');
or
const channel = channels.find(channel => channel.name === 'welcome');
or
const channel = channels.first();
3. To send a message to the channel, you can use TextChannel.send()
. Keep in mind, it returns a Promise.
channel.send(`Hello, ${member}!`)
.catch(console.error);
client.on('guildMemberAdd', member => {
const channels = member.guild.channels.filter(channel => channel.type === 'text');
const channel = channels.get('someID');
if (!channel) return console.error('Unable to find specified welcome channel!');
channel.send(`Hello, ${member}!`)
.catch(console.error);
});
Alternatively, you could create a Webhook, which would be more efficient.
1. To create the Webhook in Discord, navigate to the channel's options, click on Webhooks
, then Create Webhook
. Customize your Webhook as you wish, and then copy the link.
https://discordapp.com/api/webhooks/THIS_IS_THE_ID/THIS_IS_THE_TOKEN
2. Back to your code, you can create a Webhook Client via its constructor.
const webhookID = 'pasteTheIDHere';
const webhookToken = 'pasteTheTokenHere';
const webhook = new Discord.WebhookClient(webhookID, webhookToken);
3. Then, you can simply send messages directly to the channel via the Webhook's send
method.
webhook.send(`Hello ${member}!`)
.catch(console.error);
client.on('guildMemberAdd', member => {
const webhookID = 'pasteIDHere';
const webhookToken = 'pasteTokenHere';
const webhook = new Discord.WebhookClient(webhookID, webhookToken);
webhook.send(`Hello, ${member}!`)
.catch(console.error);
});
If you didn't have direct access to the Discord server, you could use TextChannel.createWebhook()
to create the Webhook for you. Then, you'd store the ID and token for each guild's "welcome" Webhook, and use the code above.
To track what invite was used to join the server, you have to track invites themselves.
1. In the client's ready
event, create a new object to hold Invites.
client.on('ready', () => {
client.invites = {};
});
2. Loop through the client's guilds, and add the invites from each into the object. You can acquire the invites within a guild using Guild.fetchInvites()
(returns a Promise).
for (const [id, guild] of client.guilds) {
guild.fetchInvites()
.then(invites => client.invites[id] = invites)
.catch(console.error);
}
3. Back to the guildMemberAdd
event, you need to check which invite has been used. You can do so by checking whether or not it exists, or which one went up in uses (Invite.uses
).
member.guild.fetchInvites()
.then(invites => {
const existing = client.invites[member.guild.id];
client.invites[member.guild.id] = invites;
const invite = invites.find(invite => !existing.get(invite.code) || existing.get(invite.code).uses < invite.uses);
})
.catch(console.error);
client.on('ready', () => {
client.invites = {};
for (const [id, guild] of client.guilds) {
guild.fetchInvites()
.then(invites => client.invites[id] = invites)
.catch(console.error);
}
});
// Any time the client joins a guild, you should also add their invites to the cache.
client.on('guildCreate', guild => {
guild.fetchInvites()
.then(invites => client.invites[id] = invites)
.catch(console.error);
});
client.on('guildMemberAdd', member => {
member.guild.fetchInvites()
.then(invites => {
const existing = client.invites[member.guild.id];
client.invites[member.guild.id] = invites;
const inviteUsed = invites.find(invite => !existing.get(invite.code) || existing.get(invite.code).uses < invite.uses);
const channels = member.guild.channels.filter(channel => channel.type === 'text');
const channel = channels.get('someID');
if (!channel) return console.error('Unable to find specified welcome channel!');
channel.send(`${member} joined, using an invite from ${inviteUsed.inviter} (${inviteUsed.uses} use${inviteUsed.uses !== 1 ? 's' : '')!`);
})
.catch(console.error);
});
Upvotes: 1