├── .editorconfig ├── .env.example ├── .eslintrc.js ├── .gitignore ├── .prettierrc ├── .vscode └── extensions.json ├── README.md ├── app.json ├── package.json ├── pm2.json ├── slash-up.config.js └── src ├── commands └── hello.js └── index.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | DISCORD_APP_ID= 2 | DISCORD_PUBLIC_KEY= 3 | DISCORD_BOT_TOKEN= 4 | COMMANDS_DEBUG=false 5 | DEVELOPMENT_GUILD_ID= 6 | PORT=8020 7 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | commonjs: true, 4 | es6: true, 5 | node: true 6 | }, 7 | extends: ['eslint:recommended', 'plugin:prettier/recommended'], 8 | globals: {}, 9 | plugins: 'prettier', 10 | parserOptions: { 11 | ecmaVersion: 6, 12 | sourceType: 'module' 13 | }, 14 | rules: { 15 | 'prettier/prettier': 'warn', 16 | 'no-cond-assign': [2, 'except-parens'], 17 | 'no-unused-vars': 0, 18 | 'no-empty': ['error', { allowEmptyCatch: true }], 19 | 'prefer-const': ['warn', { destructuring: 'all' }], 20 | 'spaced-comment': 'warn' 21 | }, 22 | overrides: [ 23 | { 24 | files: ['slash-up.config.js'], 25 | env: { 26 | node: true 27 | } 28 | } 29 | ] 30 | }; 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Packages 2 | node_modules/ 3 | yarn.lock 4 | package_lock.json 5 | 6 | # Log files 7 | logs/ 8 | *.log 9 | 10 | # Miscellaneous 11 | .tmp/ 12 | .vscode/**/* 13 | !.vscode/extensions.json 14 | .env 15 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": true, 3 | "singleQuote": true, 4 | "tabWidth": 2, 5 | "useTabs": false, 6 | "trailingComma": "none", 7 | "printWidth": 120 8 | } 9 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": [ 3 | "deepscan.vscode-deepscan", 4 | "dbaeumer.vscode-eslint", 5 | "esbenp.prettier-vscode", 6 | "editorconfig.editorconfig" 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # slash-create-template 2 | This templates helps you in creating slash commands from a webserver. 3 | 4 | [View the TypeScript branch here](https://github.com/Snazzah/slash-create-template/tree/typescript) 5 | 6 | | [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/Snazzah/slash-create-template/tree/master) | [![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new/template/h6aVmv?referralCode=snazzah) | 7 | |:-:|:-:| 8 | 9 | ## Installation 10 | ```sh 11 | npx slash-up init js slash-commands 12 | cd slash-commands 13 | # this edit variables in the ".env" file! 14 | # Create and edit commands in the `commands` folder 15 | npx slash-up sync 16 | yarn start 17 | ``` 18 | 19 | ### From Railway/Heroku 20 | For Railway and Heroku users, you must sync commands locally to push any command changes to Discord. You can do this by using `slash-up sync` within your Git repository. 21 | 22 | Heroku users will have their commands synced when they initially deploy to Heroku. 23 | 24 | ### Using PM2 25 | ```sh 26 | npm i -g pm2 27 | # Follow the installation process above 28 | pm2 start pm2.json 29 | pm2 dump # recommended 30 | ``` 31 | -------------------------------------------------------------------------------- /app.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "/create", 3 | "description": "Deploy a slash-create server for Discord interactions.", 4 | "repository": "https://github.com/Snazzah/slash-create-template/tree/master", 5 | "logo": "https://slash-create.js.org/static/logo-nomargin.png", 6 | "scripts": { 7 | "postdeploy": "npx slash-up sync" 8 | }, 9 | "env": { 10 | "DISCORD_APP_ID": { 11 | "description": "The application ID of the Discord app" 12 | }, 13 | "DISCORD_PUBLIC_KEY": { 14 | "description": "The public key of the Discord app" 15 | }, 16 | "DISCORD_BOT_TOKEN": { 17 | "description": "The bot token of the Discord app" 18 | } 19 | }, 20 | "keywords": ["node", "fastify", "discord", "interactions"] 21 | } 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "slash-create-template", 3 | "version": "1.0.0", 4 | "description": "A template for slash-create", 5 | "main": "src/index.js", 6 | "scripts": { 7 | "sync": "slash-up sync", 8 | "sync:dev": "slash-up sync -e development", 9 | "start": "node src/index.js" 10 | }, 11 | "dependencies": { 12 | "cat-loggr": "^1.1.0", 13 | "dotenv": "^8.2.0", 14 | "fastify": "^3.9.2", 15 | "slash-create": "^5.2.0" 16 | }, 17 | "devDependencies": { 18 | "eslint": "^7.15.0", 19 | "eslint-config-prettier": "^7.0.0", 20 | "eslint-plugin-prettier": "^3.3.0", 21 | "prettier": "^2.2.1", 22 | "slash-up": "^1.0.11" 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /pm2.json: -------------------------------------------------------------------------------- 1 | { 2 | "apps": [ 3 | { 4 | "name": "slash-commands", 5 | "script": "node", 6 | "args": "src/index.js" 7 | } 8 | ] 9 | } -------------------------------------------------------------------------------- /slash-up.config.js: -------------------------------------------------------------------------------- 1 | // This is the slash-up config file. 2 | // Make sure to fill in "token" and "applicationId" before using. 3 | // You can also use environment variables from the ".env" file if any. 4 | 5 | module.exports = { 6 | // The Token of the Discord bot 7 | token: process.env.DISCORD_BOT_TOKEN, 8 | // The Application ID of the Discord bot 9 | applicationId: process.env.DISCORD_APP_ID, 10 | // This is where the path to command files are, .ts files are supported! 11 | commandPath: './src/commands', 12 | // You can use different environments with --env (-e) 13 | env: { 14 | development: { 15 | // The "globalToGuild" option makes global commands sync to the specified guild instead. 16 | globalToGuild: process.env.DEVELOPMENT_GUILD_ID 17 | } 18 | } 19 | }; 20 | -------------------------------------------------------------------------------- /src/commands/hello.js: -------------------------------------------------------------------------------- 1 | const { SlashCommand, CommandOptionType } = require('slash-create'); 2 | 3 | module.exports = class HelloCommand extends SlashCommand { 4 | constructor(creator) { 5 | super(creator, { 6 | name: 'hello', 7 | description: 'Says hello to you.', 8 | options: [{ 9 | type: CommandOptionType.STRING, 10 | name: 'food', 11 | description: 'What food do you like?' 12 | }] 13 | }); 14 | } 15 | 16 | async run(ctx) { 17 | return ctx.options.food ? `You like ${ctx.options.food}? Nice!` : `Hello, ${ctx.member.displayName}!`; 18 | } 19 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | const { SlashCreator, FastifyServer } = require('slash-create'); 3 | const path = require('path'); 4 | const CatLoggr = require('cat-loggr'); 5 | const logger = new CatLoggr().setLevel(process.env.COMMANDS_DEBUG === 'true' ? 'debug' : 'info'); 6 | 7 | const creator = new SlashCreator({ 8 | applicationID: process.env.DISCORD_APP_ID, 9 | publicKey: process.env.DISCORD_PUBLIC_KEY, 10 | token: process.env.DISCORD_BOT_TOKEN, 11 | serverPort: parseInt(process.env.PORT, 10) || 8020, 12 | serverHost: '0.0.0.0' 13 | }); 14 | 15 | creator.on('debug', (message) => logger.log(message)); 16 | creator.on('warn', (message) => logger.warn(message)); 17 | creator.on('error', (error) => logger.error(error)); 18 | creator.on('synced', () => logger.info('Commands synced!')); 19 | creator.on('commandRun', (command, _, ctx) => 20 | logger.info(`${ctx.user.username}#${ctx.user.discriminator} (${ctx.user.id}) ran command ${command.commandName}`)); 21 | creator.on('commandRegister', (command) => 22 | logger.info(`Registered command ${command.commandName}`)); 23 | creator.on('commandError', (command, error) => logger.error(`Command ${command.commandName}:`, error)); 24 | 25 | creator 26 | .withServer(new FastifyServer()) 27 | .registerCommandsIn(path.join(__dirname, 'commands')) 28 | .startServer(); 29 | 30 | console.log(`Starting server at "localhost:${creator.options.serverPort}/interactions"`); 31 | --------------------------------------------------------------------------------