I have a code that sends a message when a member joins the guild and I want to shortly delete it afterwards.
My code:
JavaScript
x
12
12
1
client.on('guildMemberAdd', (member) => {
2
var server = member.guild.id;
3
if (server === '760511134079254610') {
4
let kanal = client.channels.cache.get('786172941703970860');
5
const embed = new Discord.MessageEmbed()
6
.setTitle('Hoşgeldin!')
7
.setDescription(`**<@${member.user.id}>** kullanıcısı sunucumuza geldi!`)
8
.setColor('BLUE')
9
10
kanal.send(embed)
11
}})
12
I tried embed.delete
or just the classic msg.delete
but it doesn’t work as I can/don’t know how to define “msg” as the last msg the bot has sent.
Advertisement
Answer
kanal.send(embed)
returns a promise. You can use async/await here to grab that message so you can delete it later. Check the following code; it should delete the message after 5s:
JavaScript
1
21
21
1
client.on('guildMemberAdd', async (member) => {
2
const server = member.guild.id;
3
if (server === '760511134079254610') {
4
let kanal = client.channels.cache.get('786172941703970860');
5
const embed = new Discord.MessageEmbed()
6
.setTitle('Hoşgeldin!')
7
.setDescription(`**<@${member.user.id}>** kullanıcısı sunucumuza geldi!`)
8
.setColor('BLUE');
9
10
try {
11
const sentMessage = await kanal.send(embed);
12
13
setTimeout(() => {
14
sentMessage.delete();
15
}, 5000);
16
} catch (error) {
17
console.log(error);
18
}
19
}
20
});
21
Update: Legendary Emoji mentioned that instead of setTimeout
you could also use an options object with a timeout
property in sentMessage.delete
like this:
JavaScript
1
8
1
try {
2
const sentMessage = await kanal.send(embed);
3
sentMessage.delete({ timeout: 5000 });
4
} catch (error) {
5
console.log(error);
6
}
7
// ...
8