Georgy Martynovich
Georgy Martynovich

Reputation: 533

Discord bot that posts custom welcome message

I want my bot to post a simple welcome message in a 'welcome' text channel for every new member.

I've read a lot of posts here and there but still having trouble with making it work as intended

Here is the code:

console.log('Beep Boop Beep'); //prints 'Beep Boop Beep'

require('dotenv').config(); //load the dotenv node pachage and call a config() func to load thea values from .env

const {Client, MessageEmbed} = require('discord.js');

//EDIT: const client = new Client()
const client = new Client({ ws: { intents: [
    'GUILDS',
    'GUILD_MESSAGES',
    'GUILD_PRESENCES',
    'GUILD_MEMBERS'
] } });

const embed = new MessageEmbed()
    .setTitle("DM thing")
    .setColor(0xFF0000)
    .setDescription("Add describtion here")

client.login(process.env.BOTTOKEN); 

module.exports = {client, embed};

function readyDiscord() {
    console.log('readyDiscord func executed. Discord is ready.'); 
}

client.on('ready', readyDiscord);

const commandHandler = require('./commands'); 

client.on('message', commandHandler);

//EDIT: 'guildMembersAdd'
client.on('guildMemberAdd', async member => { 
    console.log(member)

    const message = `Hey, <@${member.id}>! Welcome. DM me if anytime you want.`
    
    //EDIT: const channel = member.guild.channels.cache.get(WELCOME_CHANNEL_ID)
    const channel = await 

//EDIT: pocess.env
member.guild.channels.resolve(process.env.WELCOME_CHANNEL_ID);
    channel.send(message)
})

PRESENCE INTENT and SERVER MEMBERS INTENT are enabled. Each time i try to add a Test Account bot spits out those errors:

Errors:

(base) admin@MacBook-Pro-11 discord_binder % node bot.js
Beep Boop Beep
readyDiscord func executed. Discord is ready.
GuildMember {
  guild: <ref *1> Guild {
    members: GuildMemberManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular *1]
    },
    channels: GuildChannelManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular *1]
    },
    roles: RoleManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]],
      guild: [Circular *1]
    },
    presences: PresenceManager {
      cacheType: [class Collection extends Collection],
      cache: [Collection [Map]]
    },
    voiceStates: VoiceStateManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *1]
    },
    deleted: false,
    available: true,
    id: '779767191012638721',
    shardID: 0,
    name: 'testovoe_nazvaniye',
    icon: '0b54ebc0b473b6571b8157d207392af0',
    splash: null,
    discoverySplash: null,
    region: 'russia',
    memberCount: 5,
    large: false,
    features: [],
    applicationID: null,
    afkTimeout: 300,
    afkChannelID: null,
    systemChannelID: '779768178296750080',
    embedEnabled: undefined,
    premiumTier: 0,
    premiumSubscriptionCount: 0,
    verificationLevel: 'NONE',
    explicitContentFilter: 'DISABLED',
    mfaLevel: 0,
    joinedTimestamp: 1620485504837,
    defaultMessageNotifications: 'ALL',
    systemChannelFlags: SystemChannelFlags { bitfield: 0 },
    maximumMembers: 100000,
    maximumPresences: null,
    approximateMemberCount: null,
    approximatePresenceCount: null,
    vanityURLCode: null,
    vanityURLUses: null,
    description: null,
    banner: null,
    rulesChannelID: null,
    publicUpdatesChannelID: null,
    preferredLocale: 'en-US',
    ownerID: '660217837016842260',
    emojis: GuildEmojiManager {
      cacheType: [class Collection extends Collection],
      cache: Collection(0) [Map] {},
      guild: [Circular *1]
    }
  },
  joinedTimestamp: 1620486298424,
  lastMessageID: null,
  lastMessageChannelID: null,
  premiumSinceTimestamp: 0,
  deleted: false,
  nickname: null,
  _roles: [],
  user: User {
    id: '462330953734291457',
    system: null,
    locale: null,
    flags: UserFlags { bitfield: 0 },
    username: 'Test Account',
    bot: false,
    discriminator: '4317',
    avatar: '343601cdd6da4d03329351def9ffbffb',
    lastMessageID: null,
    lastMessageChannelID: null
  }
}
(node:2459) UnhandledPromiseRejectionWarning: ReferenceError: pocess is not defined
    at Client.<anonymous> (/Users/admin/Desktop/projects/re_bot/discord_binder/bot.js:43:57)
    at Client.emit (events.js:315:20)
    at Object.module.exports [as GUILD_MEMBER_ADD] (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/handlers/GUILD_MEMBER_ADD.js:16:14)
    at WebSocketManager.handlePacket (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
    at WebSocket.onMessage (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/ws/lib/event-target.js:132:16)
    at WebSocket.emit (events.js:315:20)
    at Receiver.receiverOnMessage (/Users/admin/Desktop/projects/re_bot/discord_binder/node_modules/ws/lib/websocket.js:825:20)
    at Receiver.emit (events.js:315:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:2459) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:2459) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

EDIT:

The overall idea of code was right, but there were lots of typos which you see as //EDIT: comments. So maybe you want to go and check your code for same typos

Upvotes: 0

Views: 787

Answers (3)

Akio
Akio

Reputation: 723

Just enabling Intents in Developer Portal dosen't mean you're using Intents
You'll have to tell your client which intents to use.

So, Instead of

const client = new Client();

You need to do

const client = new Client({ ws: { intents: [
    'GUILDS',
    'GUILD_MESSAGES',
    'GUILD_PRESENCES',
    'GUILD_MEMBERS'
] } });

This will tell the client to load the GUILD_MEMBERS Intent so that you can use guildMemberAdd

This code should work after that. Comment if any error.

Edit: In your code you've misspelled guildMemberAdd for guildMembersAdd You should also fix that.

Resource:

Upvotes: 2

Pedro Fracassi
Pedro Fracassi

Reputation: 3905

I see you have a comment in your code saying that WELCOME_CNAHHEL_ID is an environment variable. Just like what you did with BOTTOKEN, you might want to prefix the variable with process.env, so it should look like this:

process.env.WELCOME_CHANNEL_ID

You also have a typo in your original code: CNAHHEL instead of CHANNEL. Make sure the name on the .env file and the name you use in your code match.

Upvotes: 1

Juan Pablo
Juan Pablo

Reputation: 71

Could you let us know what is being logged to the console from the console.log(member)?

My best bet would be that the WELCOME_CHANNEL_ID is wrong and thus returning a bogus channel.

If that is not the case, then maybe the bot is not caching the channels, so use this:

const channel = await member.guild.channels.resolve(WELCOME_CHANNEL_ID);

Let us know if it doesn't work.

Upvotes: 2

Related Questions