├── .babelrc.json ├── index.js ├── .gitignore ├── src ├── Config │ ├── developers.json │ └── options.js ├── Database │ ├── Models.js │ └── Schemas │ │ ├── guilds.js │ │ ├── client.js │ │ └── users.js ├── main.js ├── Handlers │ ├── events.js │ ├── commands.js │ └── client.js ├── Events │ ├── Client │ │ └── ready.js │ └── Interactions │ │ └── interactionCreate.js ├── Utils │ ├── functions.js │ └── CLI │ │ ├── prettierrc.js │ │ ├── eslintrc.js │ │ └── beautify.js └── Commands │ └── Info │ └── ping.js ├── .prettierignore ├── .prettierrc.json ├── package.json ├── .eslintrc.json ├── README.md └── LICENSE /.babelrc.json: -------------------------------------------------------------------------------- 1 | {} -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | require("./src/main"); 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .upm/ 3 | .config/ 4 | .cache/ 5 | .package-lock.json 6 | -------------------------------------------------------------------------------- /src/Config/developers.json: -------------------------------------------------------------------------------- 1 | { 2 | "developers": [ 3 | "828677274659586068", 4 | "518207099302576160" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .upm/ 3 | .config/ 4 | .cache/ 5 | package.json 6 | package-lock.json 7 | .eslintrc.json 8 | .prettierrc.json 9 | -------------------------------------------------------------------------------- /src/Database/Models.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | guilds: require('./Schemas/guilds.js'), 3 | users: require('./Schemas/users.js'), 4 | client: require('./Schemas/client.js'), 5 | }; 6 | -------------------------------------------------------------------------------- /.prettierrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2, 3 | "semi": true, 4 | "singleQuote": true, 5 | "trailingComma": "es5", 6 | "bracketSpacing": true, 7 | "printWidth": 100, 8 | "parser": "babel" 9 | } 10 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | const Client = require('./Handlers/client'); 2 | 3 | require('dotenv').config() 4 | 5 | const options = require('./Config/options'); 6 | const client = new Client(options); 7 | 8 | client.login(process.env.TOKEN); 9 | -------------------------------------------------------------------------------- /src/Handlers/events.js: -------------------------------------------------------------------------------- 1 | class Event { 2 | constructor(client, options) { 3 | this.client = client; 4 | this.name = options.name; 5 | this.once = options.once || false; 6 | } 7 | } 8 | 9 | module.exports = Event; 10 | -------------------------------------------------------------------------------- /src/Database/Schemas/guilds.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const Schema = mongoose.Schema; 3 | 4 | const guildSchema = new Schema({ 5 | _gId: { 6 | type: String, 7 | }, 8 | }); 9 | 10 | const Guild = mongoose.model('Guilds', guildSchema); 11 | module.exports = Guild; 12 | -------------------------------------------------------------------------------- /src/Events/Client/ready.js: -------------------------------------------------------------------------------- 1 | const Events = require('../../Handlers/events'); 2 | 3 | module.exports = class extends Events { 4 | constructor(client) { 5 | super(client, { 6 | name: 'ready', 7 | once: true, 8 | }); 9 | } 10 | run = async () => { 11 | await this.client.registerCommands(); 12 | }; 13 | }; 14 | -------------------------------------------------------------------------------- /src/Database/Schemas/client.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const Schema = mongoose.Schema; 3 | 4 | const clientSchema = new Schema({ 5 | _cId: { 6 | type: String, 7 | }, 8 | developers: { 9 | type: String, 10 | default: '', // client id 11 | }, 12 | }); 13 | 14 | const Client = mongoose.model('Client', clientSchema); 15 | module.exports = Client; 16 | -------------------------------------------------------------------------------- /src/Handlers/commands.js: -------------------------------------------------------------------------------- 1 | class Command { 2 | constructor(client, options) { 3 | this.client = client; 4 | this.name = options.name || options.data.name; 5 | this.description = options.description || options.data.description; 6 | this.options = options.options || options.data?.options; 7 | this.permissions = options.permissions; 8 | this.onlyDevs = options.onlyDevs; 9 | this.defer = options.defer || false; 10 | this.dm_permission = false; 11 | this.name_localizations = options.name_localizations; 12 | } 13 | 14 | toJSON() { 15 | const { client, ...data } = this; 16 | return data; 17 | } 18 | } 19 | 20 | module.exports = Command; 21 | -------------------------------------------------------------------------------- /src/Utils/functions.js: -------------------------------------------------------------------------------- 1 | const types = ['error', 'system', 'commands', 'firebase', 'mongoose', 'success', 'client']; 2 | 3 | module.exports = { 4 | logger(message, type) { 5 | if (!type || types.indexOf(type) < 0) type = types[0]; 6 | console.log( 7 | `(${ 8 | types[types.indexOf(type)].charAt(0).toUpperCase() + types[types.indexOf(type)].slice(1) 9 | }) ${message}` 10 | ); 11 | }, 12 | 13 | msToDate(ms) { 14 | ms = Math.round(ms / 1000); 15 | const s = ms % 60, 16 | m = ~~((ms / 60) % 60), 17 | h = ~~((ms / 60 / 60) % 24), 18 | d = ~~(ms / 60 / 60 / 24); 19 | 20 | return `${d}d:${h}h:${m}m:${s}s`; 21 | }, 22 | }; 23 | -------------------------------------------------------------------------------- /src/Database/Schemas/users.js: -------------------------------------------------------------------------------- 1 | const mongoose = require('mongoose'); 2 | const Schema = mongoose.Schema; 3 | 4 | const userSchema = new Schema({ 5 | _uId: { 6 | type: String, 7 | required: true, 8 | }, 9 | _gId: { 10 | type: String, 11 | }, 12 | warn: { 13 | warns: { 14 | type: Array, 15 | }, 16 | count: { 17 | type: Number, 18 | default: 0, 19 | }, 20 | }, 21 | banClient: { 22 | data: { 23 | type: String, 24 | }, 25 | banned: { 26 | type: Boolean, 27 | default: false, 28 | }, 29 | reason: { 30 | type: String, 31 | }, 32 | }, 33 | }); 34 | 35 | const User = mongoose.model('Users', userSchema); 36 | module.exports = User; 37 | -------------------------------------------------------------------------------- /src/Commands/Info/ping.js: -------------------------------------------------------------------------------- 1 | const Commands = require('../../Handlers/commands'); 2 | 3 | module.exports = class extends Commands { 4 | constructor(client) { 5 | super(client, { 6 | name: 'ping', 7 | description: 'Veja o ping do bot', 8 | defer: true, 9 | onlyDevs: false, 10 | }); 11 | } 12 | run(interaction) { 13 | interaction 14 | .editReply({ 15 | content: 'Calculando sa bosta', 16 | fetchReply: true, 17 | }) 18 | .then((message) => { 19 | const textPing = `Latência da minha WS: \`${this.client.ws.ping}ms\`\nLatência da API: \`${ 20 | message.createdTimestamp - interaction.createdTimestamp 21 | }ms\``; 22 | 23 | interaction.editReply({ 24 | content: textPing, 25 | }); 26 | }); 27 | }; 28 | }; 29 | -------------------------------------------------------------------------------- /src/Utils/CLI/prettierrc.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | const { glob } = require('glob'); 4 | const { promisify } = require('util'); 5 | const proGlob = promisify(glob); 6 | const inquirer = require('inquirer'); 7 | const shell = require('shelljs'); 8 | 9 | const prompt = inquirer 10 | .prompt([ 11 | { 12 | message: 'Você quer iniciar o prettier?', 13 | type: 'confirm', 14 | name: 'prettier', 15 | }, 16 | ]) 17 | .then((confirm) => { 18 | if (!confirm.prettier) return; 19 | 20 | const prettier = async () => { 21 | const filepush = await proGlob(`${process.cwd().replace(/\\/g, '/')}/src/**/**/*.js`); 22 | 23 | let i = 0; 24 | filepush.forEach((file, i) => { 25 | let arquivo = file.substring(file.lastIndexOf('/') + 1); 26 | setTimeout(() => { 27 | shell.exec(`prettier --write ${file}`); 28 | }, 4000 * i); 29 | }); 30 | }; 31 | prettier(); 32 | }); 33 | -------------------------------------------------------------------------------- /src/Config/options.js: -------------------------------------------------------------------------------- 1 | const { GatewayIntentBits, Partials } = require('discord.js'); 2 | 3 | const options = { 4 | intents: [ 5 | GatewayIntentBits.GuildBans, 6 | GatewayIntentBits.GuildEmojisAndStickers, 7 | GatewayIntentBits.GuildIntegrations, 8 | GatewayIntentBits.GuildInvites, 9 | GatewayIntentBits.GuildMembers, 10 | GatewayIntentBits.GuildMessageReactions, 11 | GatewayIntentBits.GuildMessageTyping, 12 | GatewayIntentBits.GuildMessages, 13 | GatewayIntentBits.GuildPresences, 14 | GatewayIntentBits.GuildScheduledEvents, 15 | GatewayIntentBits.GuildVoiceStates, 16 | GatewayIntentBits.GuildWebhooks, 17 | GatewayIntentBits.Guilds, 18 | GatewayIntentBits.MessageContent, 19 | ], 20 | partials: [ 21 | Partials.Channel, 22 | Partials.GuildMember, 23 | Partials.GuildScheduledEvent, 24 | Partials.Message, 25 | Partials.Reaction, 26 | Partials.ThreadMember, 27 | Partials.User, 28 | ], 29 | }; 30 | 31 | module.exports = options; 32 | -------------------------------------------------------------------------------- /src/Utils/CLI/eslintrc.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | const { glob } = require('glob'); 4 | const { promisify } = require('util'); 5 | const proGlob = promisify(glob); 6 | const colors = require('colors'); 7 | const inquirer = require('inquirer'); 8 | const shell = require('shelljs'); 9 | 10 | const prompt = inquirer 11 | .prompt([ 12 | { 13 | message: 'Você quer iniciar o eslint?', 14 | type: 'confirm', 15 | name: 'eslint', 16 | }, 17 | ]) 18 | .then((confirm) => { 19 | if (!confirm.eslint) return; 20 | 21 | const eslint = async () => { 22 | const filepush = await proGlob(`${process.cwd().replace(/\\/g, '/')}/src/**/**/*.js`, { 23 | ignore: '**/CLI/**', 24 | }); 25 | const i = 0; 26 | filepush.forEach((file, i) => { 27 | let arquivo = file.substring(file.lastIndexOf('/') + 1); 28 | setTimeout(() => { 29 | shell.exec(`eslint ${file} --fix`); 30 | console.log(`[Check] Arquivo ${arquivo} aprimorado com sucesso!`.blue.dim); 31 | }, 4000 * i); 32 | }); 33 | }; 34 | eslint(); 35 | }); 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "CommunityStructure", 3 | "version": "1.0.0", 4 | "description": "Organizational structure for using the discord.js framework", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "dev": "nodemon index.js" 9 | }, 10 | "bin": { 11 | "beautify": "./src/Utils/CLI/beautify.js", 12 | "eslintrc": "./src/Utils/CLI/eslintrc.js", 13 | "prettierrc": "./src/Utils/CLI/prettierrc.js" 14 | }, 15 | "keywords": [], 16 | "author": [ 17 | "Juaum • 愛#4009", 18 | "ঔৣ☬✞𝓓𝖔𝖓✞☬ঔৣ#0552" 19 | ], 20 | "license": "ISC", 21 | "dependencies": { 22 | "chalk": "^4.1.2", 23 | "colors": "^1.4.0", 24 | "discord.js": "^14.7.1", 25 | "dotenv": "^16.0.3", 26 | "glob": "^8.1.0", 27 | "js-beautify": "^1.14.7", 28 | "moment": "^2.29.4", 29 | "mongoose": "^6.9.1" 30 | }, 31 | "devDependencies": { 32 | "@babel/core": "^7.20.12", 33 | "@babel/eslint-parser": "^7.19.1", 34 | "eslint": "^8.20.0", 35 | "eslint-config-prettier": "^8.6.0", 36 | "eslint-plugin-n": "^15.6.1", 37 | "eslint-plugin-node": "^11.1.0", 38 | "inquirer": "^8.2.5", 39 | "nodemon": "^2.0.20", 40 | "prettier": "2.8.4", 41 | "shelljs": "^0.8.5" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Utils/CLI/beautify.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | 3 | const { glob } = require('glob'); 4 | const { promisify } = require('util'); 5 | const proGlob = promisify(glob); 6 | const colors = require('colors'); 7 | const inquirer = require('inquirer'); 8 | const beautify = require('js-beautify').js; 9 | const fs = require('fs'); 10 | 11 | const prompt = inquirer 12 | .prompt([ 13 | { 14 | message: 'Você quer iniciar o beautify?', 15 | type: 'confirm', 16 | name: 'beautify', 17 | }, 18 | ]) 19 | .then((confirm) => { 20 | if (!confirm.beautify) return; 21 | 22 | const beautifier = async () => { 23 | const filepush = await proGlob(`${process.cwd().replace(/\\/g, '/')}/src/**/**/*.js`); 24 | const i = 0; 25 | filepush.forEach((print, i) => { 26 | setTimeout(() => { 27 | fs.readFile(print, (err, data) => { 28 | const beaut = beautify(Buffer?.from(data).toString()); 29 | const arquivo = print.substring(print.lastIndexOf('/') + 1); 30 | fs.writeFile(print, beaut, (err) => { 31 | if (err) { 32 | console.log(`[Error] Arquivo ${arquivo} falhou ao ser embelezado.`.red.dim); 33 | throw err; 34 | } 35 | console.log(`[Check] Arquivo ${arquivo} editado com sucesso!`.green.dim); 36 | }); 37 | }); 38 | }, 4000 * i); 39 | }); 40 | }; 41 | beautifier(); 42 | }); 43 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "@babel/eslint-parser", 3 | "env": { 4 | "browser": true, 5 | "commonjs": true, 6 | "es2021": true, 7 | "node": true 8 | }, 9 | "extends": [ 10 | "eslint:recommended", 11 | "prettier" 12 | ], 13 | "parserOptions": { 14 | "ecmaVersion": 2020, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "classes": true 18 | } 19 | }, 20 | "plugins": ["n", "node"], 21 | "rules": { 22 | "node/no-unsupported-features/es-syntax": "off", 23 | "node/no-missing-import": "error", 24 | "node/no-extraneous-import": "error", 25 | "node/no-unpublished-require": "error", 26 | "class-methods-use-this": "off", 27 | "no-undef": "off", 28 | "n/shebang": "error", 29 | "unicode-bom": ["error", "never"], 30 | "indent": ["error", 4], 31 | "linebreak-style": ["error", "windows"], 32 | "quotes": ["error", "single"], 33 | "semi": ["error", "always"], 34 | "require-await": ["error"], 35 | "no-lone-blocks": ["error"], 36 | "block-spacing": ["error", "always"], 37 | "eol-last": ["error", "always"], 38 | "brace-style": ["error", "stroustrup"], 39 | "prefer-const": [ 40 | "error", 41 | { 42 | "destructuring": "any", 43 | "ignoreReadBeforeAssign": false 44 | } 45 | ], 46 | "object-curly-spacing": [ 47 | "error", 48 | "always", 49 | { 50 | "arraysInObjects": true 51 | } 52 | ], 53 | "comma-spacing": [ 54 | "error", 55 | { 56 | "before": false, 57 | "after": true 58 | } 59 | ] 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Events/Interactions/interactionCreate.js: -------------------------------------------------------------------------------- 1 | const Events = require('../../Handlers/events'); 2 | const { developers } = require('../../Config/developers.json'); 3 | 4 | module.exports = class extends Events { 5 | constructor(client) { 6 | super(client, { 7 | name: 'interactionCreate', 8 | }); 9 | } 10 | run = async (interaction) => { 11 | 12 | if (!interaction.isChatInputCommand()) return; 13 | 14 | const commandName = interaction.commandName; 15 | const command = this.client.commandSlash.find((c) => c.name === commandName); 16 | 17 | if (command.onlyDevs && !developers.includes(interaction.user.id)) 18 | return interaction.editReply({ 19 | content: `${interaction.user} **|** este comando é privado apenas para desenvolvedores desta aplicação!`, 20 | ephemeral: true, 21 | }); 22 | 23 | if (command.permissions) { 24 | if (!interaction.member.permissions.has(command.permissions)) { 25 | return interaction.reply({ content: 'Você não tem perm' }); 26 | } 27 | else if (!interaction.guild.members.me.permissions.has(command.permissions)) { 28 | return interaction.reply({ content: 'Eu não tenho perm bro' }); 29 | } 30 | } 31 | 32 | if (!this.client.cooldown.has(interaction.user.id)) { 33 | if (!command) { 34 | return interaction.reply({ 35 | content: 'Ocorreu um erro ao executar este comando...', 36 | ephemeral: true, 37 | }); 38 | } 39 | if (command.defer) await interaction.deferReply(); 40 | command.run(interaction); 41 | } else { 42 | return interaction.reply({ 43 | content: 'Você está em cooldown, aguarde 5 segundos para usar os comandos novamente.', 44 | ephemeral: true, 45 | }); 46 | } 47 | 48 | await this.client.cooldown.add(interaction.user.id); 49 | setTimeout(async () => { 50 | await this.client.cooldown.delete(interaction.user.id); 51 | }, 5000); 52 | }; 53 | }; 54 | -------------------------------------------------------------------------------- /src/Handlers/client.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const utils = { functions: require('../Utils/functions') }; 3 | const { join } = require('path'); 4 | const { Client } = require('discord.js'); 5 | 6 | module.exports = class extends Client { 7 | constructor(options) { 8 | super(options); 9 | 10 | this.commandSlash = []; 11 | this.loadCommands(); 12 | this.loadEvents(); 13 | this.cooldown = new Set(); 14 | this.moment = require('moment'); 15 | this.utils = utils.functions; 16 | } 17 | 18 | registerCommands() { 19 | this.utils.logger('Carregando os comandos (/) da aplicação', 'commands'); 20 | this.application?.commands 21 | .set(this.commandSlash) 22 | .then(() => { 23 | this.utils.logger('Os comandos (/) da aplicação foram carregados com sucesso.', 'commands'); 24 | }) 25 | .catch((err) => { 26 | this.utils.logger(err, 'error'); 27 | }); 28 | } 29 | 30 | loadCommands(path = 'src/Commands') { 31 | const categories = fs.readdirSync(path); 32 | for (const category of categories) { 33 | const commands = fs.readdirSync(`${path}/${category}`); 34 | 35 | for (const command of commands) { 36 | const commandClass = require(join(process.cwd(), `${path}/${category}/${command}`)); 37 | const cmd = new commandClass(this); 38 | 39 | this.commandSlash.push(cmd); 40 | } 41 | } 42 | } 43 | 44 | loadEvents(path = 'src/Events') { 45 | const eventsFolders = fs.readdirSync(path); 46 | for (const folders of eventsFolders) { 47 | const eventsFiles = fs.readdirSync(`${path}/${folders}`); 48 | for (const files of eventsFiles) { 49 | if (!files.endsWith('.js')) return; 50 | const eventClass = require(join(process.cwd(), `${path}/${folders}/${files}`)); 51 | 52 | const evnt = new eventClass(this); 53 | if (!evnt.once) { 54 | this.on(evnt.name, evnt.run); 55 | } else { 56 | this.once(evnt.name, evnt.run); 57 | } 58 | } 59 | } 60 | } 61 | 62 | async connectToDatabase() { 63 | try { 64 | const { connect } = require('mongoose'); 65 | const mongoose = require('mongoose'); 66 | const Models = require('../Database/Models'); 67 | 68 | mongoose.set('strictQuery', true); 69 | 70 | await this.utils.logger('Conectando com a database...', 'mongoose'); 71 | const connection = connect(process.env.MONGODB_URL, { 72 | useNewUrlParser: true, 73 | useUnifiedTopology: true, 74 | }); 75 | 76 | this.db = { 77 | connection, 78 | ...Models, 79 | }; 80 | 81 | await this.utils.logger('Database carregada com sucesso', 'mongoose'); 82 | } catch (error) { 83 | await this.utils.logger( 84 | 'Ocorreu um erro ao tentar conectar com a database\n' + error, 85 | 'mongoose' 86 | ); 87 | } 88 | } 89 | }; 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
24 | Projeto | 25 | Como usar | 26 | Licença 27 |
28 | 29 | ## 📋 Projeto 30 | 31 | * 🔐 A estrutura do discord.js permite criar bots e interagir com o Discord, com objetos que representam servidores, canais, mensagens, entre outros. Há métodos e eventos disponíveis para manipular esses objetos e responder a ações do usuário. Conhecer bem a estrutura é importante para aproveitar ao máximo as capacidades do discord.js e criar soluções personalizadas.