Added basic functionality

This commit is contained in:
plane000
2018-05-20 01:18:47 +01:00
parent 9523cd874a
commit 5a764b0a71
8 changed files with 193 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2018 plane000
Copyright (c) 2018 Benjamin Kyd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

1
commandmanager.js Normal file
View File

@@ -0,0 +1 @@
// TODO: commandmanager

27
commands/commands.js Normal file
View File

@@ -0,0 +1,27 @@
const Discord = require('discord.js');
const ping = require('ping');
const fs = require('fs');
const Config = require('../config');
/*message object, messaage full, message args, discord client*/
module.exports.say = function(message, msg, args, discordclient) {
}
module.exports.version = function(message, msg, args, discordclient) {
var em = new Discord.RichEmbed();
em.setColor('BLUE');
em.setTitle('Version:');
em.setDescription(Config.getconfig().Version);
message.channel.send(em);
return;
}
module.exports.ping = function(message, msg, args, discordclient) {
ping.promise.probe("discordapp.com", {
timeout: 10
}).then((output) => {
message.channel.send(`:white_check_mark: \`${output.avg}ms\``);
});
}

31
config.js Normal file
View File

@@ -0,0 +1,31 @@
const fs = require("fs");
var config = null;
module.exports.getconfig = function() {
return config;
};
module.exports.setconfig = function(con) {
config = con;
};
module.exports.loadDefaults = function() {
config = {
Token: "DISCORD_TOKEN",
NowPlaying: "RealLife.exe",
Prefix: "+",
Version: "2.0.1",
Rules: ["Rule 0", "Rule 1"]
};
};
module.exports.loadFromFile = function() {
config = JSON.parse(fs.readFileSync("resources/config.json"));
}
module.exports.writeToFile = function() {
fs.writeFileSync("resources/config.json", JSON.stringify(config, null, 4));
}

View File

@@ -1,23 +1,50 @@
module.exports.logMSG = function (msg) {
var t = new Date();
var time = (pad(t.getHours(), 2) + ":" + pad(t.getMinutes(), 2) + ":" + pad(t.getSeconds(), 2))
module.exports.logMSG = function(msg) {
var time = getTime();
console.log(
time
+ ' in '
'[' + time + '] '
+ 'In '
+ msg.guild.name
+ ', '
+ msg.author.username
+ '#'
+ msg.author.discriminator
+ ' issued the command: '
+ msg.content);
+ msg.content
);
}
module.exports.log = function(msg) {
var time = getTime();
console.log(
'[' + time + '] '
+ msg
);
}
module.exports.success = function() {
var time = getTime();
console.log(
'[' + time + '] '
+ msg
);
}
module.exports.failed = function(msg) {
var time = getTime();
console.log(
'[' + time + '] '
+ msg
);
}
function pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
function getTime() {
var t = new Date();
var time = (pad(t.getHours(), 2) + ":" + pad(t.getMinutes(), 2) + ":" + pad(t.getSeconds(), 2))
return time;
}

86
main.js
View File

@@ -1,19 +1,79 @@
const logger = require('./logger');
const Logger = require('./logger');
const Config = require('./config');
const CommandManager = require('./commandmanager');
const http = require('http');
const ping = require('ping');
const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
client.login('');
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
logger.logMSG(msg);
msg.reply('pong');
/*checks if config exists*/
if (!fs.existsSync('resources/config.json')) {
Logger.log('Creating the config...');
try {
if (!fs.existsSync('resources/')) {
fs.mkdirSync('resources/');
}
Config.loadDefaults();
Config.writeToFile();
}
catch (e) {
Logger.failed(`Could not create the config: ${e.message}`);
}
}
/*loads config*/
Logger.log('Loading config...');
try {
Config.loadFromFile();
}
catch (e) {
Logger.failed(`Could not load the config: ${e.message}`);
}
/*connects to discord*/
try {
Logger.log("Starting discord client...");
client.login(Config.getconfig().Token);
} catch (e) {
Logger.failed(`Could not connect to discord: ${e.message}`)
}
/*once connected*/
client.on('ready', () => {
client.user.setPresence('online');
client.user.setActivity(Config.getconfig().NowPlaying);
Logger.log(`Logged in as ${client.user.tag}`);
Logger.log('Ready!')
console.log();
});
/*on message event*/
client.on('message', async (message) => {
/*if it starts with prefix*/
if (message.content.startsWith(Config.getconfig().Prefix)) {
Logger.logMSG(message);
var msg = message.content.substring(Config.getconfig().Prefix.length);
var args = msg.split(" ");
/*temp switch statement to manage commands*/
switch (args[0].toUpperCase()) {
case 'PING':
ping.promise.probe("discordapp.com", {
timeout: 10
}).then((output) => {
message.channel.send(`:white_check_mark: \`${output.avg}ms\``);
});
return;
case 'VERSION':
var em = new Discord.RichEmbed();
em.setColor('BLUE');
em.setTitle('Version:');
em.setDescription(Config.getconfig().Version);
message.channel.send(em);
return;
default:
message.channel.send(`:no_entry: \`The command '${args[0]}' does not exist...\``);
break;
}
}
});

24
package-lock.json generated
View File

@@ -26,16 +26,35 @@
"resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
"integrity": "sha1-invTcYa23d84E/I4WLV+yq9eQdQ="
},
"http": {
"version": "0.0.0",
"resolved": "https://registry.npmjs.org/http/-/http-0.0.0.tgz",
"integrity": "sha1-huYybSnF0Dnen6xYSkVon5KfT3I="
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"ping": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/ping/-/ping-0.2.2.tgz",
"integrity": "sha1-GA+3UIwdx0eThJvONcgHP5llvOI=",
"requires": {
"q": "1.5.1",
"underscore": "1.9.0"
}
},
"prism-media": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.2.tgz",
"integrity": "sha512-L6yc8P5NVG35ivzvfI7bcTYzqFV+K8gTfX9YaJbmIFfMXTs71RMnAupvTQPTCteGsiOy9QcNLkQyWjAafY/hCQ=="
},
"q": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/q/-/q-1.5.1.tgz",
"integrity": "sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc="
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
@@ -51,6 +70,11 @@
"resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.0.tgz",
"integrity": "sha1-cT2LgY2kIGh0C/aDhtBHnmb8ins="
},
"underscore": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.0.tgz",
"integrity": "sha512-4IV1DSSxC1QK48j9ONFK1MoIAKKkbE8i7u55w2R6IqBqbT7A/iG7aZBCR2Bi8piF0Uz+i/MG1aeqLwl/5vqF+A=="
},
"ws": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-4.1.0.tgz",

View File

@@ -22,6 +22,8 @@
"homepage": "https://github.com/plane000/JefferyBot#readme",
"dependencies": {
"discord.js": "^11.3.2",
"fs": "0.0.1-security"
"fs": "0.0.1-security",
"http": "0.0.0",
"ping": "^0.2.2"
}
}