33
loading...
This website collects cookies to deliver better user experience
│ .env
│ index.js
│ package-lock.json
│ package.json
│
├───commands
│ owo.js
│
├───events
│ ready.js
│
└───node_modules
│ ...
./.env
TOKEN=<your-token-here>
PREFIX=<your-bot-prefix>
./index.js
const { Client } = require('yuuko');
const path = require('path');
const dotenv = require('dotenv');
var env = dotenv.config();
env = process.env;
const bot = new Client({
token: env.TOKEN,
prefix: env.PREFIX,
ignoreBots: true,
});
bot.extendContext({
variableOne: 'Variable number 1!',
});
bot.editStatus('dnd'); // edits bot status
bot.on('error', (err) => {
console.error(err);
});
bot.globalCommandRequirements = {
guildOnly: true,
};
bot
.addDir(path.join(__dirname, 'commands'))
.addDir(path.join(__dirname, 'events'))
.connect();
./package.json
+ ./package-lock.json
yuuko
, eris
, and dotenv
installed../commands/owo.js
const { Command } = require('yuuko');
module.exports = new Command('owo', (message, args, context) => {
message.channel.createMessage('OwO');
});
./events/ready.js
const { EventListener } = require('yuuko');
module.exports = new EventListener('ready', ({client}) => {
console.log(`Logged in as ${client.user.usename}`);
});
Meme
command! For this, we will need to get the memes from reddit. For that, we will be using got
to get the JSON from https://www.reddit.com/r/memes/random/.json
.got
first:npm i got --save
./commands
and name it meme.js
.const { Command } = require('yuuko');
const got = require('got');
module.exports = new Command('meme', (message) => {
got('https://www.reddit.com/r/memes/random/.json')
.then((response) => {
const [list] = JSON.parse(response.body);
const [post] = list.data.children;
const permalink = post.data.permalink;
const memeUrl = `https://reddit.com${permalink}`;
const memeImage = post.data.url;
const memeTitle = post.data.title;
const memeUpvotes = post.data.ups;
const memeNumComments = post.data.num_comments;
message.channel.createMessage({
embed: {
title: memeTitle,
url: memeUrl,
image: {
url: memeImage,
},
color: 15267908,
footer: {
text: `👍 ${memeUpvotes} 💬 ${memeNumComments}`,
},
},
});
})
.catch(err => {
console.error(err);
});
});
node index.js
nodemon
installednodemon index.js
const { Command } = require('yuuko');
const got = require('got');
module.exports = new Command('meme', (message) => {
// code here
})
got('https://www.reddit.com/r/memes/random/.json').then((response) => {
// code here
}).catch(err => {
console.error(err);
});
got
to get the JSON from reddit (the subreddit r/memes
actually), and save the response as the response
variable. Note that we are using Promises here, thus the .then().catch()
in the code. You can, however, use the async/await
in ES6.const [list] = JSON.parse(response.body);
const [post] = list.data.children;
JSON.parse
(Note: You will get an error if you just use JSON.parse(response)
), and get the information about the reddit post which we saved inside the post
variable. Understand? Excellent.const permalink = post.data.permalink;
const memeUrl = `https://reddit.com${permalink}`;
const memeImage = post.data.url;
const memeTitle = post.data.title;
const memeUpvotes = post.data.ups;
const memeNumComments = post.data.num_comments;
memeUrl
, the meme image url as memeImage
, the meme title as memeTitle
, the number of meme upvotes as memeUpvotes
, and the number of comments as memeNumComments
.message.channel.createMessage({
embed: {
title: memeTitle,
url: memeUrl,
image: {
url: memeImage,
},
color: 15267908,
footer: {
text: `👍 ${memeUpvotes} 💬 ${memeNumComments}`,
},
},
});
whois
command. See you until next time!