Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moved sensitive information to .env #144

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
EMAIL=
PASSWORD=
BOT_TOKEN=
CLIENT_ID=
YOUTUBE_API_KEY=
GOOGLE_CUSTOM_SEARCH=
IMGFLIP_USERNAME=
IMGFLIP_PASSWORD=
WOLFRAM_API_KEY=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
logs
*.log

.env

# Runtime data
pids
*.pid
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ You can create an rss.json file adding rss feeds as commands. See rss.json.examp
Make sure you also have your Google server API key, which is located in the "youtube_api_key" section, or the search will fail.

# Running
Before the first run you will need to create an `auth.json` file. A bot token or the email and password for a discord account are required. The other credentials are not required for the bot to run, but they are highly recommended as commands that depend on them will malfunction. See `auth.json.example`.
Before the first run you will need to create an `.env` file. A bot token or the email and password for a discord account are required. The other credentials are not required for the bot to run, but they are highly recommended as commands that depend on them will malfunction. See `.env.example`.

To start the bot just run
`node discord_bot.js`.
Expand Down
12 changes: 0 additions & 12 deletions auth.json.example

This file was deleted.

20 changes: 6 additions & 14 deletions discord_bot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var fs = require('fs');
const fs = require('fs');
require('dotenv').config()

process.on('unhandledRejection', (reason) => {
console.error(reason);
Expand All @@ -16,15 +17,6 @@ try {
console.log("Starting DiscordBot\nNode version: " + process.version + "\nDiscord.js version: " + Discord.version); // send message notifying bot boot-up



// Get authentication data
try {
var AuthDetails = require("./auth.json");
} catch (e){
console.log("Please create an auth.json like auth.json.example with a bot token or an email and password.\n"+e.stack); // send message for error - no token
process.exit();
}

// Load custom permissions
var dangerousCommands = ["eval","pullanddeploy","setUsername","cmdauth"]; // set array if dangerous commands
var Permissions = {};
Expand Down Expand Up @@ -244,11 +236,11 @@ commands = { // all commands list below
}
};

if(AuthDetails.hasOwnProperty("client_id")){
if(process.env.hasOwnProperty("CLIENT_ID")){
commands["invite"] = {
description: "Generates an invite link you can use to invite the bot to your server.",
process: function(bot,msg,suffix){
msg.channel.send("Invite link: https://discordapp.com/oauth2/authorize?&client_id=" + AuthDetails.client_id + "&scope=bot&permissions=470019135"); // send link to invite bot into server.
msg.channel.send("Invite link: https://discordapp.com/oauth2/authorize?&client_id=" + process.env.CLIENT_ID + "&scope=bot&permissions=470019135"); // send link to invite bot into server.
}
}
}
Expand Down Expand Up @@ -446,9 +438,9 @@ exports.addCommand = function(commandName, commandObject){
exports.commandCount = function(){
return Object.keys(commands).length;
}
if(AuthDetails.bot_token){
if(process.env.BOT_TOKEN){
console.log("logging in with token");
bot.login(AuthDetails.bot_token);
bot.login(process.env.BOT_TOKEN);
} else {
console.log("Logging in with user credentials is no longer supported!\nYou can use token based log in with a user account; see\nhttps://discord.js.org/#/docs/main/master/general/updating.");
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"cleverbot-node": "^0.2.8",
"d20": "^1.4.1",
"discord.js": "^11.5.1",
"dotenv": "^8.1.0",
"feedparser": "1.1.x",
"html-to-text": "^3.3.0",
"imgflipper": "^1.0.1",
Expand All @@ -33,15 +34,15 @@
"node-wolfram": "0.0.1",
"npm": "",
"querystring": "0.2.x",
"request": "^2.85.0",
"request": "^2.88.0",
"request-promise": "^4.2.1",
"sinon": "1.14.x",
"tumblr.js": "0.0.x",
"underscore": "1.8.x",
"urban": "^0.3.1",
"wikijs": "^0.1.4",
"youtube-dl": "^2.0.0",
"youtube-node": "^1.3.3",
"youtube-node": "^1.2.0",
"ytdl-core": "^0.29.3",
"zucc": "^0.1.2"
},
Expand Down
11 changes: 5 additions & 6 deletions plugins/Google/google.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var request = require("request");
var AuthDetails = require("../../auth.json");
try {
var yt = require("./youtube_plugin");
var youtube_plugin = new yt();
Expand All @@ -18,13 +17,13 @@ exports.image = {
usage: "<search query>",
description: "gets the top matching image from google",
process: function(bot, msg, args) {
if(!AuthDetails || !AuthDetails.youtube_api_key || !AuthDetails.google_custom_search){
if(!process.env.YOUTUBE_API_KEY || !process.env.GOOGLE_CUSTOM_SEARCH){
msg.channel.send("Image search requires both a YouTube API key and a Google Custom Search key!");
return;
}
//gets us a random result in first 5 pages
var page = 1; //we request 10 items
request("https://www.googleapis.com/customsearch/v1?key=" + AuthDetails.youtube_api_key + "&cx=" + AuthDetails.google_custom_search + "&q=" + (args.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page, function(err, res, body) {
request("https://www.googleapis.com/customsearch/v1?key=" + process.env.YOUTUBE_API_KEY + "&cx=" + process.env.GOOGLE_CUSTOM_SEARCH + "&q=" + (args.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page, function(err, res, body) {
var data, error;
try {
data = JSON.parse(body);
Expand Down Expand Up @@ -52,13 +51,13 @@ exports.rimage = {
usage: "<search query>",
description: "gets a random image matching tags from google",
process: function(bot, msg, args) {
if(!AuthDetails || !AuthDetails.youtube_api_key || !AuthDetails.google_custom_search){
if(!process.env.YOUTUBE_API_KEY || !process.env.GOOGLE_CUSTOM_SEARCH){
msg.channel.send( "Image search requires both a YouTube API key and a Google Custom Search key!");
return;
}
//gets us a random result in first 5 pages
var page = 1 + Math.floor(Math.random() * 5) * 10; //we request 10 items
request("https://www.googleapis.com/customsearch/v1?key=" + AuthDetails.youtube_api_key + "&cx=" + AuthDetails.google_custom_search + "&q=" + (args.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page, function(err, res, body) {
request("https://www.googleapis.com/customsearch/v1?key=" + process.env.YOUTUBE_API_KEY + "&cx=" + process.env.GOOGLE_CUSTOM_SEARCH + "&q=" + (args.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page, function(err, res, body) {
var data, error;
try {
data = JSON.parse(body);
Expand Down Expand Up @@ -88,7 +87,7 @@ exports.ggif = {
process : function(bot, msg, args) {
//gets us a random result in first 5 pages
var page = 1 + Math.floor(Math.random() * 5) * 10; //we request 10 items
request("https://www.googleapis.com/customsearch/v1?key=" + AuthDetails.youtube_api_key + "&cx=" + AuthDetails.google_custom_search + "&q=" + (args.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page+"&fileType=gif", function(err, res, body) {
request("https://www.googleapis.com/customsearch/v1?key=" + process.env.YOUTUBE_API_KEY + "&cx=" + process.env.GOOGLE_CUSTOM_SEARCH + "&q=" + (args.replace(/\s/g, '+')) + "&searchType=image&alt=json&num=10&start="+page+"&fileType=gif", function(err, res, body) {
var data, error;
try {
data = JSON.parse(body);
Expand Down
3 changes: 1 addition & 2 deletions plugins/Google/youtube_plugin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
var util = require('util');
var youtube_node = require('youtube-node');
var AuthDetails = require("../../auth.json");
var Config = require("../../config.json");


function YoutubePlugin () {
this.RickrollUrl = 'http://www.youtube.com/watch?v=oHg5SJYRHA0';
this.youtube = new youtube_node();
this.youtube.setKey(AuthDetails.youtube_api_key);
this.youtube.setKey(process.env.YOUTUBE_API_KEY);
this.youtube.addParam('type', 'video');
};

Expand Down
3 changes: 1 addition & 2 deletions plugins/Imgflip/imgflip.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ exports.commands = [
"meme"
]

var AuthDetails = require("../../auth.json");
var Config = require("../../config.json");

//https://api.imgflip.com/popular_meme_ids
Expand Down Expand Up @@ -36,7 +35,7 @@ exports.meme = {
var memetype = tags[0].split(" ")[1];
//msg.channel.send(tags);
var Imgflipper = require("imgflipper");
var imgflipper = new Imgflipper(AuthDetails.imgflip_username, AuthDetails.imgflip_password);
var imgflipper = new Imgflipper(process.env.IMGFLIP_USERNAME, process.env.IMGFLIP_PASSWORD);
imgflipper.generateMeme(meme[memetype], tags[1]?tags[1]:"", tags[3]?tags[3]:"", function(err, image){
console.log(arguments);
console.log(image);
Expand Down
10 changes: 6 additions & 4 deletions plugins/Random/random.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const request = require("request")

exports.commands = [
"date_fact",
"year_fact",
Expand All @@ -9,7 +11,7 @@ exports.math_fact = {
usage: "<random math>",
description: "Gives a Random Math Fact",
process: function(bot, msg, suffix) {
require("request")("http://numbersapi.com/random/math?json",
request("http://numbersapi.com/random/math?json",
function(err, res, body) {
var data = JSON.parse(body);
if (data && data.text) {
Expand All @@ -22,7 +24,7 @@ exports.math_fact = {
exports.year_fact = {
description: "Gives a Random Year Fact",
process: function(bot, msg, suffix) {
require("request")("http://numbersapi.com/random/year?json",
request("http://numbersapi.com/random/year?json",
function(err, res, body) {
var data = JSON.parse(body);
if (data && data.text) {
Expand All @@ -35,7 +37,7 @@ exports.math_fact = {
exports.joke = {
description: "Gives a Random Joke",
process: function(bot, msg, suffix) {
require("request")("http://tambal.azurewebsites.net/joke/random",
request("http://tambal.azurewebsites.net/joke/random",
function(err, res, body) {
var data = JSON.parse(body);
if (data && data.joke) {
Expand All @@ -48,7 +50,7 @@ exports.math_fact = {
exports.date_fact = {
description: "Gives a Random Date Fact",
process: function(bot, msg, suffix) {
require("request")("http://numbersapi.com/random/date?json",
request("http://numbersapi.com/random/date?json",
function(err, res, body) {
var data = JSON.parse(body);
if (data && data.text) {
Expand Down
8 changes: 3 additions & 5 deletions plugins/Twitch/twitch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var request = require("request");
var AuthDetails = require("../../auth.json");
var Discord = require("discord.js");

exports.commands = [
"twitch_user",
Expand All @@ -16,7 +14,7 @@ exports.twitch_user = {
request({
url: "https://api.twitch.tv/helix/"+user_query,
headers: {
'Client-ID': AuthDetails.twitch_client_id
'Client-ID': process.env.TWITCH_CLIENT_ID
}
},
function(err,res,body){
Expand Down Expand Up @@ -53,7 +51,7 @@ exports.twitch = {
request({
url: twitch_api+user_query,
headers: {
'Client-ID': AuthDetails.twitch_client_id
'Client-ID': process.env.TWITCH_CLIENT_ID
}
},
function(err,res,body){
Expand All @@ -77,7 +75,7 @@ exports.twitch = {
request({
url: twitch_api+stream_query,
headers: {
'Client-ID': AuthDetails.twitch_client_id
'Client-ID': process.env.TWITCH_CLIENT_ID
}
},

Expand Down
3 changes: 1 addition & 2 deletions plugins/Wolfram Alpha/wolfram_plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var Wolfram = require('node-wolfram');
var AuthDetails = require("../../auth.json");

function WolframPlugin () {
this.wolfram = new Wolfram(AuthDetails.wolfram_api_key)
this.wolfram = new Wolfram(process.env.WOLFRAM_API_KEY)
};

WolframPlugin.prototype.respond = function (query, channel, bot,tmpMsg) {
Expand Down
3 changes: 1 addition & 2 deletions wolfram_plugin.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
var Wolfram = require('node-wolfram');
var AuthDetails = require("./auth.json");

function WolframPlugin () {
this.wolfram = new Wolfram(AuthDetails.wolfram_api_key)
this.wolfram = new Wolfram(process.env.WOLFRAM_API_KEY)
};

WolframPlugin.prototype.respond = function (query, channel, bot,tmpMsg) {
Expand Down
3 changes: 1 addition & 2 deletions youtube_plugin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var util = require('util');
var youtube_node = require('youtube-node');
var AuthDetails = require("./auth.json");


function YoutubePlugin () {
this.RickrollUrl = 'http://www.youtube.com/watch?v=oHg5SJYRHA0';
this.youtube = new youtube_node();
this.youtube.setKey(AuthDetails.youtube_api_key);
this.youtube.setKey(process.env.YOUTUBE_API_KEY);
this.youtube.addParam('type', 'video');
};

Expand Down