Skip to content
Closed
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
17 changes: 16 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down Expand Up @@ -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/
4 changes: 4 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
TODO:
Implement Module Loading System.
Implement Auto Generating Help System

2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require('./modules/ping-pong');
require('./modules/customize');
11 changes: 11 additions & 0 deletions core/client.js
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 52 additions & 0 deletions core/commands.js
Original file line number Diff line number Diff line change
@@ -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;
});
18 changes: 18 additions & 0 deletions modules/customize.js
Original file line number Diff line number Diff line change
@@ -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');
20 changes: 20 additions & 0 deletions modules/ping-pong.js
Original file line number Diff line number Diff line change
@@ -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');
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}