Byte-Sized Tech Tips Round-Up: Week 3
Steve Martinelli
Posted on October 23, 2020
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);
package.json
{
"dependencies": {
"discord.js": "^12.3.1"
}
}
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" ]
Now you can build your image (use your Docker id here - and mind the trailing dot):
docker build -t <YOUR_DOCKER_ID>/node-container .
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
Now you can ping your bot! 🤖
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"
}
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.
- Part 1: Designing Mircoservices (October 20, 2020)
- Part 2: Deploying Mircoservices (October 27, 2020)
- Part 3: Debugging Mircoservices (November 5, 2020)
- Part 4: Developing Microservices with Node.js (November 10, 2020)
- Part 5: Virtual Hands On Building Microservices Lab (November 17, 2020)
- Part 6: Data Handling in Mircoservices (November 24, 2020)
Posted on October 23, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.