Skip to content

Instantly share code, notes, and snippets.

@NEOdinok
Created May 8, 2021 22:07
Show Gist options
  • Save NEOdinok/b81fa80962716298f53087cf7449f6ab to your computer and use it in GitHub Desktop.
Save NEOdinok/b81fa80962716298f53087cf7449f6ab to your computer and use it in GitHub Desktop.
client.on('message... event listener problem
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 Discord = require('discord.js');
const client = new Discord.Client({ ws: { intents: [
'GUILDS',
'GUILD_MESSAGES',
'GUILD_PRESENCES',
'GUILD_MEMBERS'
] } });
//this line authenticates the bot with the discord API
client.login(process.env.BOTTOKEN);
const embed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Some title')
.setURL('https://discord.js.org/')
.setAuthor('Some name', 'https://i.imgur.com/wSTFkRM.png', 'https://discord.js.org')
.setDescription('Some description here')
.setThumbnail('https://i.imgur.com/wSTFkRM.png')
.addFields(
{ name: 'Regular field title', value: 'Some value here' },
{ name: '\u200B', value: '\u200B' },
{ name: 'Inline field title', value: 'Some value here', inline: true },
{ name: 'Inline field title', value: 'Some value here', inline: true },
)
.addField('Inline field title', 'Some value here', true)
.setImage('https://i.imgur.com/wSTFkRM.png')
.setTimestamp()
.setFooter('Some footer text here', 'https://i.imgur.com/wSTFkRM.png');
//exporting those so that they are available in other .js files
module.exports = {client, embed};
//ready discord function prings stuff to terminal
function readyDiscord() {
console.log('readyDiscord func executed. Discord is ready.');
}
client.on('ready', readyDiscord);
//if a !command is typed send it to commandHandler
const commandHandler = require('./commands');
client.on('message', commandHandler);
//if a new member is added, DM him with a message
client.on('guildMemberAdd', async member => {
console.log(member)
const message = `Hey, <@${member.id}>! Welcome. DM me if anytime you want.`
const channel = await member.guild.channels.resolve(process.env.WELCOME_CHANNEL_ID);
channel.send(message)
})
const hey = require('./commands/hey.js');
const bind = require("./commands/bind.js");
const help = require("./commands/help.js");
const commands = { bind, hey, help };
module.exports = function (msg) {
//commands only work within 'bot commands channel'
if (msg.channel.type === 'dm') {
/*'.split()' method divides a string and puts it into an array.
* the division is done by searching for a pattern which is the first parameter
* in the method's call */
let tokens = msg.content.split(" ");
/* moves the whole array one element forward, removes the first element and.
* returns it. this is needed if some bot command has arguments.
* for instance: !bind 'key to bind to' */
let command = tokens.shift();
//bot command is only valid when it starts with a !
if (command.charAt(0) === '!') {
/*this one removes the first element of the. in our case it removes
* the ! of the command and places command itself into commnad vairable.
* so now command variable contains 'hey, bind' or smth, like that*/
command = command.substring(1);
/* take the 'commands' obj, take a function named [command] from it and
* pass (msg, tokens) to it. msg object contains all the message metadata
* and tokens is an array of the command with all of it's arguments */
commands[command](msg, tokens);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment