Byte-Sized Tech Tips Round-Up: Week 3

Steve Martinelli - Oct 23 '20 - - Dev Community

From

When I have started to develop a chatbot for Discord, I realized that everybody is using Node installed in their own environments. I was aiming at running a bot right away in the cloud. Instead of running your bot "natively" in your system using node, I created a Docker container that handles it all for me.

index.js

const Discord = require('discord.js');
const client = new Discord.Client();
const Token = process.env.token;
client.on('ready', () => {  
   console.log('Logged in as ${client.user.tag}!');
});
client.on('message', msg => {  if (msg.content === 'ping')
{
     msg.reply('Pong!');  
}});
client.login(Token);
Enter fullscreen mode Exit fullscreen mode

package.json

{
"dependencies": {
    "discord.js": "^12.3.1"
  }
}
Enter fullscreen mode Exit fullscreen mode

Dockerfile

FROM node:10
ENV NODE_CONTAINER_VERSION=1.0.0
# Create directory for application
WORKDIR /data/bot-app
# Install dependencies
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "node", "index.js" ]
Enter fullscreen mode Exit fullscreen mode

Now you can build your image (use your Docker id here - and mind the trailing dot):

docker build -t <YOUR_DOCKER_ID>/node-container .
Enter fullscreen mode Exit fullscreen mode

And to start your Docker container with the Discord bot (add in your Discord token):

docker run -e token="<YOUR_TOKEN>" -d <YOUR_DOCKER_ID>/node-container
Enter fullscreen mode Exit fullscreen mode

Now you can ping your bot! 🤖

discord

Read a long version of this post in my blog

Javascript: in vs of

From Nigel

This week I learned about a neat little trick when iterating through arrays in javascript. The important of in which gives you the position in the array, and of which gives you the value of the element in the array. Fun!

let myArray = ["cat", "dog", "mouse"]

for (let val in myArray) {
    console.log(val) // 0, 1, 2
}

for (let val of myArray) {
    console.log(val) // "cat", "dog", "mouse"
}
Enter fullscreen mode Exit fullscreen mode

Shout out to Nick Bourdakos for showing me this one.

Bee Travels

From


We're hosting a 6-part online series on microservices. If you are new to microservices or have been using microservices for some time – you will get high-quality developer education. Register for all six session below. The replay of each session will be available at the same link.

beetravels-session

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .