Skip to content

Code cleanup, command handler and better config #12

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-runtime",
"@babel/plugin-syntax-dynamic-import"
]
}
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ root = true

[*]
indent_style = space
indent_size = 4
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
Expand Down
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": [
"wesbos"
]
}
4 changes: 4 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"semi": true,
"singleQuote": true
}
10 changes: 10 additions & 0 deletions commands/help.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import help from '../messages/help.json';

exports.run = (client, message, args) => {
const [, cmd] = args;
if (Object.prototype.hasOwnProperty.call(help, cmd)) {
message.channel.send({
embed: help[cmd],
});
}
};
40 changes: 40 additions & 0 deletions commands/mdn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { searchMDN } from 'mdn-search-docs';
import discord from 'discord.js';

exports.run = (client, message, args) => {
const [...term] = args;
let result = '';
const collector = new discord.MessageCollector(
message.channel,
m => m.author.id === message.author.id,
{ time: 10000 }
);
searchMDN({ term })
.then(res => {
res.documents.forEach((r, i) => {
result += `${i + 1}) ${r.title}\n`;
});
message.channel.send(` \`\`\`css\n${result}\`\`\` `);
// eslint-disable-next-line no-shadow
collector.on('collect', message => {
if (!Number.isNaN(message.content) && message.content <= 10) {
const test = res.documents[message.content - 1];

message.channel.send({
embed: {
title: test.title,
description: test.excerpt.replace(/<\/?[^>]+>/gi, ''),
url: test.url,
},
});
} else {
message.channel.send(
'Invalid number. Please select a number from the list.'
);
}
});
})
.catch(err => {
console.log(err);
});
};
3 changes: 3 additions & 0 deletions commands/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.run = (client, message) => {
message.channel.send('pong!').catch(console.error);
};
15 changes: 15 additions & 0 deletions commands/reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
exports.run = (client, message, args) => {
if (!args || args.size < 1)
return message.reply('Must provide a command name to reload.');
const commandName = args[0];
if (!client.commands.has(commandName)) {
return message.reply('That command does not exist');
}
delete require.cache[require.resolve(`./${commandName}.js`)];
client.commands.delete(commandName);
const props = require(`./${commandName}.js`);
client.commands.set(commandName, props);
message.reply(`The command ${commandName} has been reloaded`);
};
6 changes: 3 additions & 3 deletions config-sample.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"token": "token",
"prefix": "!fed",
"spotifyToken": "token"
"token": "token",
"prefix": "!fed",
"spotifyToken": "token"
}
9 changes: 9 additions & 0 deletions events/guildMemberAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import welcome from '../messages/welcome.json';

module.exports = (client, member) => {
if (member.id) {
client.users.get(member.id).send({
embed: welcome.message,
});
}
};
20 changes: 20 additions & 0 deletions events/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = (client, message) => {
const prefix = '!fed';
// Ignore all bots
if (message.author.bot) return;
// Ignore messages not starting with the prefix (in config.json)
if (message.content.indexOf(prefix) !== 0) return;
// Our standard argument/command name definition.
const args = message.content
.slice(prefix.length)
.trim()
.split(/ +/g);
const command = args.shift().toLowerCase();
// Grab the command data from the client.commands Enmap
const cmd = client.commands.get(command);
// If that command doesn't exist, silently exit and do nothing
if (!cmd) return;

// Run the command
cmd.run(client, message, args);
};
3 changes: 3 additions & 0 deletions events/ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = (client, message) => {
console.log('I am ready!');
};
64 changes: 26 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
const discord = require("discord.js");
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
import discord from 'discord.js';
import Enmap from 'enmap';
import fs from 'fs';
import config from './config.json';

const client = new discord.Client();
const config = require("./config.json");
const help = require("./messages/help.json");
const welcome = require("./messages/welcome.json");

client.on("ready", () => {
console.log("I am ready!");
fs.readdir('./events/', (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith('.js')) return;
const event = require(`./events/${file}`);
const eventName = file.split('.')[0];
client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
});
});

client.on("message", message => {
if (message.author.bot) return;
if (message.content.indexOf(config.prefix) !== 0) return;
if (!message.member.roles.some(r => ["Administrators"].includes(r.name)))
return message.reply("Sorry, you don't have permissions to use this!");

const args = message.content
.slice(config.prefix.length)
.trim()
.split(/ +/g);
const command = args.shift().toLowerCase();

if (command === "help") {
let [person, msg] = args;

for (let cmd in help) {
if (help.hasOwnProperty(cmd)) {
if (msg === cmd) {
message.channel.send({
embed: help[cmd]
});
}
}
}
}
});
client.commands = new Enmap();

client.on("guildMemberAdd", member => {
if (member.id) {
client.users.get(member.id).send({
embed: welcome.message
});
}
fs.readdir('./commands/', (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith('.js')) return;
const props = require(`./commands/${file}`);
const commandName = file.split('.')[0];
console.log(`Attempting to load command ${commandName}`);
client.commands.set(commandName, props);
});
});

client.login(config.token);
Loading