Search code examples
javascriptnode.jsdiscorddiscord.jschatbot

I want to DM the owner of the server once the bot has been invited


So I'm looking for a way to get my bot to message the owner of the server its been added too. After looking at lots of stackoverflow answers, they all looked similar to the following code:

client.on('guildCreate', guild =>{
    guild.author.send('Hello');
});

However, when I tried this and added the bot to another server, the bot not only crashed but I got the following error:

guild.author.send('Hello'); 
             ^ 

TypeError: Cannot read properties of undefined (reading 'send')

Can someone please help me out here. Thanks in advance.


Solution

  • You can get the owner by using two methods:

    1. await guild.fetchOwner(): This can be used to fetch the owner as a GuildMember and an example is like this:
    const owner = await guild.fetchOwner()
    
    1. guild.ownerId: This can used to get the id of the owner of the server and then can be used to fetch the owner and an example is like this:
    const ownerId = guild.ownerId
    const owner = guild.members.cache.get(ownerId)