diff --git a/.gitignore b/.gitignore index ad46b30..e1da6ae 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,7 @@ coverage # nyc test coverage .nyc_output -# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) .grunt # Bower dependency directory (https://bower.io/) @@ -57,5 +57,20 @@ typings/ # dotenv environment variables file .env +# parcel-bundler cache (https://parceljs.org/) +.cache + # next.js build output .next + +# nuxt.js build output +.nuxt + +# vuepress build output +.vuepress/dist + +# Serverless directories +.serverless + +# FuseBox cache +.fusebox/ diff --git a/TODO b/TODO new file mode 100644 index 0000000..d2b54dc --- /dev/null +++ b/TODO @@ -0,0 +1,4 @@ +TODO: + Implement Module Loading System. + Implement Auto Generating Help System + \ No newline at end of file diff --git a/app.js b/app.js new file mode 100644 index 0000000..b4567e4 --- /dev/null +++ b/app.js @@ -0,0 +1,2 @@ +require('./modules/ping-pong'); +require('./modules/customize'); \ No newline at end of file diff --git a/core/client.js b/core/client.js new file mode 100644 index 0000000..5d91d24 --- /dev/null +++ b/core/client.js @@ -0,0 +1,11 @@ +const Discord = require('discord.js'); +const client = new Discord.Client(); + +client.on('ready', () => { + console.log("Discord Ready"); +}); + +client.login('NTQzMjc1NzYzOTQwNTg5NTg5.Dz6MsQ.8VPzmAK8t7-Ry-WX3Y5tcTEpeOw'); + +module.exports.discord = Discord; +module.exports.client = client; diff --git a/core/commands.js b/core/commands.js new file mode 100644 index 0000000..485f084 --- /dev/null +++ b/core/commands.js @@ -0,0 +1,52 @@ +const discord = require('./client'); + +const registeredCommands = []; +let prefix = '!'; + +const checkCommand = (command, body) => { + for (let i = 0; i < registeredCommands.length; i += 1) { + const c = registeredCommands[i]; + if (c.command === command && c.body === body) { + return false; + } + } + return true; +}; + +// Cleanest way to allow [] for param chaining... +Array.prototype.toString = function () { return this.join(' '); }; + +exports.register = (command, params, description, response) => { + const compiled = params === '' ? `${command}` : `${command} ${params}`; + + if (checkCommand(command, params)) { + registeredCommands.push({ + command, + params, + description, + response, + compiled, + }); + } +}; + +exports.setPrefix = (newPrefix) => { + prefix = newPrefix; +}; + +exports.getCommands = command => registeredCommands.filter(e => e.command === command); + +exports.getAllCommands = () => registeredCommands; + +discord.client.on('message', (msg) => { + const message = msg.content; + for (let i = 0; i < registeredCommands.length; i += 1) { + const c = registeredCommands[i]; + const r = new RegExp(`${prefix}${c.compiled}`); + const match = message.match(r) ? message.match(r) : []; + if (`${prefix}${c.compiled}` === message || match[1]) { + return c.response(msg, match); + } + } + return null; +}); diff --git a/modules/customize.js b/modules/customize.js new file mode 100644 index 0000000..a0cd61f --- /dev/null +++ b/modules/customize.js @@ -0,0 +1,18 @@ +const commands = require('../core/commands'); +const discord = require('../core/client'); + +commands.register('customize', '', 'Customize Help', (msg) => { + msg.reply('Customize the bot with other commands!'); +}); + +commands.register('customize', 'game (.*)', 'Change the bots game', (msg, extra) => { + discord.client.user.setActivity(extra[1]); + msg.reply(`Set game to: ${extra[1]}`); +}); + +commands.register('customize', ['command', 'prefix', '(.*)'], 'Change the bots command Prefix', (msg, extra) => { + commands.setPrefix(extra[1]); + msg.reply(`Set prefix to: ${extra[1]}`); +}); + +console.log('Registered Customize Module'); diff --git a/modules/ping-pong.js b/modules/ping-pong.js new file mode 100644 index 0000000..7152190 --- /dev/null +++ b/modules/ping-pong.js @@ -0,0 +1,20 @@ +const commands = require('../core/commands'); +const discord = require('../core/client'); + +commands.register('ping', '', 'Ping the bot', (msg) => { + msg.reply('PONG'); +}); + +commands.register('ping', 'pong', 'Pong the bot', (msg) => { + msg.reply('PING'); +}); + +commands.register('ping', ['pong', 'ping', 'pong', 'ping'], 'Pong the bot', (msg) => { + msg.reply('PONG'); +}); + +commands.register('ping', ['pong', 'ping', 'pong'], 'Ping the bot', (msg) => { + msg.reply('PING'); +}); + +console.log('Registered Ping-Pong Module'); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..7f896b6 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,59 @@ +{ + "name": "rwebdevdiscord", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "async-limiter": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/async-limiter/-/async-limiter-1.0.0.tgz", + "integrity": "sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==" + }, + "discord.js": { + "version": "11.4.2", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-11.4.2.tgz", + "integrity": "sha512-MDwpu0lMFTjqomijDl1Ed9miMQe6kB4ifKdP28QZllmLv/HVOJXhatRgjS8urp/wBlOfx+qAYSXcdI5cKGYsfg==", + "requires": { + "long": "^4.0.0", + "prism-media": "^0.0.3", + "snekfetch": "^3.6.4", + "tweetnacl": "^1.0.0", + "ws": "^4.0.0" + } + }, + "long": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz", + "integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==" + }, + "prism-media": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-0.0.3.tgz", + "integrity": "sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==" + }, + "safe-buffer": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" + }, + "snekfetch": { + "version": "3.6.4", + "resolved": "https://registry.npmjs.org/snekfetch/-/snekfetch-3.6.4.tgz", + "integrity": "sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==" + }, + "tweetnacl": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.1.tgz", + "integrity": "sha512-kcoMoKTPYnoeS50tzoqjPY3Uv9axeuuFAZY9M/9zFnhoVvRfxz9K29IMPD7jGmt2c8SW7i3gT9WqDl2+nV7p4A==" + }, + "ws": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-4.1.0.tgz", + "integrity": "sha512-ZGh/8kF9rrRNffkLFV4AzhvooEclrOH0xaugmqGsIfFgOE/pIz4fMc4Ef+5HSQqTEug2S9JZIWDR47duDSLfaA==", + "requires": { + "async-limiter": "~1.0.0", + "safe-buffer": "~5.1.0" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..fa91bfc --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "rwebdevdiscord", + "version": "1.0.0", + "description": "Discord bot for r/webdev", + "main": "app.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "rwebdev", + "license": "MIT", + "dependencies": { + "discord.js": "^11.4.2" + } +}