DEV Community

Steve Martinelli for IBM Developer

Posted on

3 2

Byte-Sized Tech Tips Round-Up: Week 3

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

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more