Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
David Ralph committed Sep 10, 2021
1 parent ff1133b commit ff3dd01
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ node_modules/
# Files
yarn.lock
config.json
package-lock.json
package-lock.json
43 changes: 21 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
{
"private": true,
"name": "hastebin-bot",
"version": "0.3.5",
"author": "David Ralph",
"license": "MIT",
"description": "An opensource Discord bot that posts data to Hastebin",
"main": "index.js",
"scripts": {
"start": "cd src && node index.js"
},
"bugs": {
"url": "https://github.com/davidjcralph/hastebin-bot/issues"
},
"homepage": "https://github.com/davidjcralph/hastebin-bot",
"repository": {
"type": "git",
"url": "https://github.com/davidjcralph/hastebin-bot"
},
"dependencies": {
"discord.js": "12.5.1",
"fs": "0.0.1-security"
}
"private": true,
"name": "hastebin-bot",
"version": "0.3.5",
"author": "David Ralph",
"license": "MIT",
"description": "An opensource Discord bot that posts data to Hastebin",
"main": "index.js",
"scripts": {
"start": "cd src && node index.js"
},
"bugs": {
"url": "https://github.com/davidjcralph/hastebin-bot/issues"
},
"homepage": "https://github.com/davidjcralph/hastebin-bot",
"repository": {
"type": "git",
"url": "https://github.com/davidjcralph/hastebin-bot"
},
"dependencies": {
"discord.js": "12.5.1"
}
}
28 changes: 15 additions & 13 deletions src/commands/haste.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@ const fetch = require('node-fetch');
const fs = require('fs');

exports.run = async (client, msg, args) => {
if (!args[0] || msg.attachments.size < 0) {
return msg.channel.send(':x: | I can\'t post nothing to Hastebin!');
}
if (!args[0] || msg.attachments.size < 0) {
return msg.channel.send(':x: | I can\'t post nothing to Hastebin!');
}

let body = []; //Define body
client.config.dir_uploader ? body = fs.readFileSync(args[0], 'utf8') : body = args.slice(0).join(' '); //using ternary operator to check if dir_uploader is true or false.
// Define body
let body = [];
// using ternary operator to check if dir_uploader is true or false.
client.config.dir_uploader ? body = fs.readFileSync(args[0], 'utf8') : body = args.slice(0).join(' ');

const options = {
method: 'POST',
body: body,
headers: {
'Content-Type': 'application/json'
}
const options = {
method: 'POST',
body: body,
headers: {
'Content-Type': 'application/json'
}
}

const res = await (await fetch(`${client.config.hasteurl}/documents`, options)).json();
const res = await (await fetch(`${client.config.hasteurl}/documents`, options)).json();

msg.channel.send(`:white_check_mark: | Posted text to Hastebin at this URL: ${client.config.hasteurl}/${res.key}`);
msg.channel.send(`:white_check_mark: | Posted text to Hastebin at this URL: ${client.config.hasteurl}/${res.key}`);
};
17 changes: 9 additions & 8 deletions src/commands/help.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { MessageEmbed } = require('discord.js');

exports.run = (_client, msg) => {
const embed = new MessageEmbed()
.setTitle('Commands:')
.addField('help', 'Returns this message.')
.addField('ping', 'Pong! Returns the bot\'s ping.')
.addField('stats', 'Get the bot stats.')
.addField('haste <text>', 'Upload text to Hastebin.')
.setFooter('Made by davidjcralph | https://github.com/davidjcralph/hastebin-bot');
msg.channel.send(embed);
const embed = new MessageEmbed()
.setTitle('Commands:')
.addField('help', 'Returns this message.')
.addField('ping', 'Pong! Returns the bot\'s ping.')
.addField('stats', 'Get the bot stats.')
.addField('haste <text>', 'Upload text to Hastebin.')
.setFooter('Made by davidjcralph | https://github.com/davidjcralph/hastebin-bot');

msg.channel.send(embed);
};
2 changes: 1 addition & 1 deletion src/commands/ping.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.run = (client, msg) => {
msg.channel.send(`:ping_pong: | Pong! The ping is **${(client.ws.ping)}**ms.`);
msg.channel.send(`:ping_pong: | Pong! The ping is **${(client.ws.ping)}**ms.`);
};
2 changes: 1 addition & 1 deletion src/commands/stats.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
exports.run = (client, msg) => {
msg.channel.send(`:robot: | I am on **${client.guilds.cache.size}** guilds with **${client.users.cache.size}** users!`);
msg.channel.send(`:robot: | I am on **${client.guilds.cache.size}** guilds with **${client.users.cache.size}** users!`);
};
8 changes: 4 additions & 4 deletions src/config.example.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"token": "Discord Bot Token",
"prefix": "hb!",
"hasteurl": "https://hastebin.com",
"dir_uploader": true
"token": "Discord Bot Token",
"prefix": "hb!",
"hasteurl": "https://hastebin.com",
"dir_uploader": true
}
28 changes: 14 additions & 14 deletions src/events/message.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module.exports = (client, msg) => {
if (msg.author.bot || msg.content.indexOf(client.config.prefix) !== 0) {
return;
}
if (msg.author.bot || msg.content.indexOf(client.config.prefix) !== 0) {
return;
}

const args = msg.content.slice(client.config.prefix.length).trim().split(/ +/g);
const cmd = client.commands.get(args.shift().toLowerCase());
const args = msg.content.slice(client.config.prefix.length).trim().split(/ +/g);
const cmd = client.commands.get(args.shift().toLowerCase());

if (!cmd) {
return;
}
if (!cmd) {
return;
}

try {
cmd.run(client, msg, args);
} catch (e) {
console.log(e);
msg.channel.send(':x: | Something went wrong ```' + e + '```');
}
try {
cmd.run(client, msg, args);
} catch (e) {
console.log(e);
msg.channel.send(':x: | Something went wrong ```' + e + '```');
}
};
2 changes: 1 addition & 1 deletion src/events/ready.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = () => {
console.log('Connected to Discord!');
console.log('Connected to Discord!');
};
38 changes: 19 additions & 19 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@ const { readdir } = require('fs');
const { Client, Collection } = require('discord.js');

const client = new Client({
disableEveryone: true,
autoReconnect: true
disableEveryone: true,
autoReconnect: true
});

readdir('./events/', (err, files) => {
if (err) {
return console.log(err);
}
if (err) {
return console.log(err);
}

files.forEach(file => {
let eventName = file.split('.')[0];
const event = require(`./events/${file}`);
console.log(`Loading ${eventName}.js!`);
client.on(eventName, event.bind(null, client));
});
files.forEach(file => {
let eventName = file.split('.')[0];
const event = require(`./events/${file}`);
console.log(`Loading ${eventName}.js!`);
client.on(eventName, event.bind(null, client));
});
});

client.config = require('./config.json');
client.commands = new Collection();

readdir('./commands/', (err, files) => {
if (err) {
return console.log(err);
}
if (err) {
return console.log(err);
}

files.forEach(file => {
let commandName = file.split('.')[0];
console.log(`Loading ${commandName}.js!`);
client.commands.set(commandName, require(`./commands/${file}`));
});
files.forEach(file => {
let commandName = file.split('.')[0];
console.log(`Loading ${commandName}.js!`);
client.commands.set(commandName, require(`./commands/${file}`));
});
});

client.login(client.config.token);

0 comments on commit ff3dd01

Please sign in to comment.