diff --git a/package.json b/package.json index 0ff3f10..e0121b9 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "d.js-v12-bot", "version": "0.0.1", "description": "A Discord bot built on Discord.JS v12", - "main": "index.js", + "main": "src/index", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/Structures/BotClient.js b/src/Structures/BotClient.js index e69de29..6dc0361 100644 --- a/src/Structures/BotClient.js +++ b/src/Structures/BotClient.js @@ -0,0 +1,50 @@ +const { Client } = require('discord.js'); + +module.exports = class BotClient extends Client { + + constructor(options = {}) { + super({ + disableMentions: 'everyone' + }); + this.validate(options); + + this.once('ready', () => { + console.log(`Logged in as ${this.user.username}.`); + }); + + this.on('message', async (message) => { + const mentionRegex = RegExp(`^<@!${this.user.id}>$`); + const mentionRegexPrefix = RegExp(`^<@!${this.user.id}> `); + + if (!message.guild || message.author.bot) return; + + if (message.content.match(mentionRegex)) message.channel.send(`My prefix for ${message.guild.name} is \`${this.prefix}\`.`); + + const prefix = message.content.match(mentionRegexPrefix) ? + message.content.match(mentionRegexPrefix)[0] : this.prefix; + + // eslint-disable-next-line no-unused-vars + const [cmd, ...args] = message.content.slice(prefix.length).trim().split(/ +/g); + + if (cmd.toLowerCase() === 'hello') { + message.channel.send('Hello!'); + } + }); + } + + validate(options) { + if (typeof options !== 'object') throw new TypeError('Options should be a type of Object.'); + + if (!options.token) throw new Error('You must pass a token for the client.'); + this.token = options.token; + + if (!options.prefix) throw new Error('You must pass a prefix for the client.'); + if (typeof options.prefix !== 'string') throw new TypeError('Prefix should be a type of String.'); + this.prefix = options.prefix; + } + + async login(token = this.token) { + super.login(token); + } + +}; diff --git a/src/index.js b/src/index.js index b328438..ff14631 100644 --- a/src/index.js +++ b/src/index.js @@ -1,5 +1,5 @@ -const BotClient = require("./Structures/BotClient"); -const config = require("../config.json"); +const BotClient = require('./Structures/BotClient'); +const config = require('../config.json'); const client = new BotClient(config); -client.login(); \ No newline at end of file +client.login();