├── .gitignore ├── README.md ├── gacha.js ├── index.js ├── package.json └── test └── gacha.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # TypeScript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | # next.js build output 61 | .next 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sample-gacha-bot 2 | Simple example for play pulling gacha with bot in discord 3 | 4 | **[NODEJS](https://nodejs.org/en/) must be installed** 5 | 6 | # Getting started 7 | ```javascript 8 | $ git clone https://github.com/up2code/sample-bot.git 9 | $ npm install 10 | ``` 11 | Find or create your discord bot token [here](https://discordapp.com/developers/applications/me) 12 | and replace at this login parameter to your token 13 | ```javascript 14 | bot.login('YOUR BOT TOKEN HERE!!!'); 15 | ``` 16 | Make sure you have assign your bot in to your server. You can use below URL for bot to login into your server. Replace to your bot client id. You can found in your discord app info [here](https://discordapp.com/developers/applications/me) 17 | ``` 18 | https://discordapp.com/oauth2/authorize?&client_id=&scope=bot&permissions=0 19 | ``` 20 | Edit code as your want. When everything is done, Let's TRY!! 21 | ```javascript 22 | $ npm start 23 | ``` 24 | 25 | Open discord and go to server which you had assigned bot, Then typing _gacha_ and see the result. -------------------------------------------------------------------------------- /gacha.js: -------------------------------------------------------------------------------- 1 | var Chance = require('chance'); 2 | var chance = new Chance(); 3 | 4 | var roll = function() { 5 | return chance.integer({ min: 1, max: 100 }) 6 | } 7 | 8 | var pull = function() { 9 | 10 | var result = roll(); 11 | 12 | if(result == 1 ) { 13 | return '5 ★ Servant' 14 | } 15 | 16 | if(result >= 2 && result <= 4) { 17 | return '4 ★ Servant' 18 | } 19 | 20 | if(result >= 5 && result <= 44) { 21 | return '3 ★ Servant' 22 | } 23 | 24 | if(result >= 45 && result <= 48) { 25 | return '5 ★ Craft Essence' 26 | } 27 | 28 | if(result >= 49 && result <= 60) { 29 | return '4 ★ Craft Essence' 30 | } 31 | 32 | return '3 ★ Craft Essence'; 33 | } 34 | 35 | module.exports.roll = roll 36 | module.exports.pull = pull -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | //Get discord.js 2 | const Discord = require('discord.js'); 3 | 4 | //Create client instance as bot 5 | const bot = new Discord.Client(); 6 | 7 | const gacha = require('./gacha'); 8 | 9 | //Set listener on 'ready' 10 | bot.on('ready', () => { 11 | console.log('Gacha ready!'); 12 | }); 13 | 14 | //Set listener on 'message' 15 | bot.on('message', message => { 16 | if (message.content === 'gacha') { 17 | message.channel.sendMessage(gacha.pull()); 18 | } 19 | }); 20 | 21 | bot.login('YOUR BOT TOKEN HERE!!!'); 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sample-gacha-bot", 3 | "version": "1.0.0", 4 | "description": "Simple example for play pulling gacha with bot in discord", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "mocha" 8 | }, 9 | "dependencies": { 10 | "chance": "^1.0.16", 11 | "discord.js": "^10.0.1" 12 | }, 13 | "devDependencies": { 14 | "chai": "^4.2.0" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/up2code/sample-gacha-bot.git" 19 | }, 20 | "keywords": [ 21 | "bot", 22 | "discord", 23 | "discord-bot", 24 | "discord-js", 25 | "gacha" 26 | ], 27 | "author": "up2up", 28 | "license": "ISC", 29 | "bugs": { 30 | "url": "https://github.com/up2code/sample-gacha-bot/issues" 31 | }, 32 | "homepage": "https://github.com/up2code/sample-gacha-bot#readme" 33 | } 34 | -------------------------------------------------------------------------------- /test/gacha.test.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect 2 | var gacha = require('./../gacha'); 3 | 4 | describe('Gacha', function() { 5 | 6 | it('should roll and got result in range 1 - 100', function() { 7 | 8 | var result = gacha.roll(); 9 | 10 | expect(result).to.be.within(1, 100); 11 | 12 | }) 13 | 14 | it('should got gacha from pull', function() { 15 | 16 | var result = gacha.pull(); 17 | 18 | expect(result).to.be.oneOf([ 19 | '5 ★ Servant', 20 | '4 ★ Servant', 21 | '3 ★ Servant', 22 | '5 ★ Craft Essence', 23 | '4 ★ Craft Essence', 24 | '3 ★ Craft Essence' 25 | ]); 26 | 27 | }) 28 | }); --------------------------------------------------------------------------------