I'm trying to make a bot reply when it's mentioned. I'm using TypeScript for this and I am confused. I searched multiple solutions but I think its for js because it's always an error and some code doesn't exist. Here's the code I originally tried
const client = new DiscordJS.Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
]
})
client.on('ready',()=>{
console.log('bot is ready')
})
client.on('messageCreate',(message)=>{
if(message.content === '@bot-test'){
message.reply({
content: 'I heard you'
})
}
})
It doesn't work with mentions but works with messages.
CodePudding user response:
The reason is to do with the whay you look with mentions
When you see a mention, it looks to you as @username
, however the discord bot see's it as: <@!userid>
For example @abisammy
is actually <@!468128787884670986>
To fix this change the if statement from
if (message.content === "@bot-test"){
to:
if(message.content === `<@!${client.user.id}>`){
This should then work
Note when using typescript you will have to add client.user
to the if statement
Typescript only
if(client.user && message.content === `<@!${client.user.id}>`){