Skip to content

Commit

Permalink
Merge pull request #19 from wsknorth/dev
Browse files Browse the repository at this point in the history
dev
  • Loading branch information
Sabato Luca Guadagno authored Dec 12, 2017
2 parents ca9d9e1 + 46744cd commit b83a8bf
Show file tree
Hide file tree
Showing 24 changed files with 1,012 additions and 304 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
lib/configs/configs.json
database/midnabot.sqlite
Empty file added database/.gitkeep
Empty file.
20 changes: 16 additions & 4 deletions docs/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<p align="center">
<a href="https://github.com/standard/standard"><img src="https://img.shields.io/badge/Code_style-Standard-green.svg?style=for-the-badge" alt="Standard"></a>
<a href="https://github.com/wsknorth/midna/releases"><img src="https://img.shields.io/badge/Version-0.1.5-orange.svg?style=for-the-badge" alt="Version"></a>
<a href="https://github.com/wsknorth/midna/releases"><img src="https://img.shields.io/badge/Version-0.2.0-orange.svg?style=for-the-badge" alt="Version"></a>
<a href="/docs/License.md"><img src="https://img.shields.io/badge/License-MIT-blue.svg?style=for-the-badge" alt="License"></a>
<a href="https://t.me/midnabot"><img src="https://img.shields.io/badge/Telegram-midnabot-blue.svg?style=for-the-badge" alt="Telegram"></a>
<a href="https://www.gitcheese.com/donate/users/22490354/repos/113030252"><img src="https://img.shields.io/badge/Gitcheese-Donate-red.svg?style=for-the-badge" alt="Donate"></a>
Expand All @@ -17,11 +17,17 @@

## Commands

### About
### Start
```
/about
/start
```
Show information about midnabot
Enable bot

### Stop
```
/stop
```
Disable bot

### Sandbox
```
Expand All @@ -36,3 +42,9 @@ The `/sandbox` command use [gf3/sandbox](https://github.com/gf3/sandbox) to work
```
Roll dices.
The `/roll` command use [troygoode/node-roll](https://github.com/troygoode/node-roll) to work.

### About
```
/about
```
Show information about midnabot
17 changes: 9 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ const debug = require('debug')('midnabot')

const Telegraf = require('telegraf')

const { configs } = require('./lib/configs')
const { request } = require('./lib/request')
const { response } = require('./lib/response')
const { sandbox } = require('./lib/sandbox')
const { about } = require('./lib/about')
const { roll } = require('./lib/roll')
const { configs, sequelize } = require('./lib/modules')
const { request, response, after } = require('./lib/middlewares')
const { start, sandbox, roll, about, stop } = require('./lib/commands')

const bot = new Telegraf(configs.token, { username: configs.username })
sequelize.sync().then(() => debug(`Database connected`))

bot.use(request())
bot.context.response = response()
bot.use(response())
bot.command('start', start())
bot.use(after())
bot.command('sandbox', sandbox())
bot.command('about', about())
bot.command('roll', roll())
bot.command('about', about())
bot.command('stop', stop())

const production = () => {
debug(`${configs.username} setting webhook: ${configs.webhook}`)
Expand Down
2 changes: 1 addition & 1 deletion lib/about/index.js → lib/commands/about/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { name, version, author, homepage } = require('../../package.json')
const { name, version, author, homepage } = require('../../../package.json')

const about = () => (ctx) => {
const message = `*${name} ${version}*\n${author}\n${homepage}`
Expand Down
7 changes: 7 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { start } = require('./start')
const { sandbox } = require('./sandbox')
const { roll } = require('./roll')
const { about } = require('./about')
const { stop } = require('./stop')

module.exports = { start, sandbox, roll, about, stop }
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions lib/commands/start/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const debug = require('debug')('midnabot:start')

const { Starts } = require('../../models/starts')

const start = () => (ctx) => {
const name = (ctx.update.message.chat.type === 'private')
? ctx.update.message.from.username
: ctx.update.message.chat.title

const reply = (ctx.update.message.chat.type === 'private')
? `midnabot is now listening in this chat`
: `midnabot is now listening in this channel`

Starts.upsert({ name, start: true })
.then(() => ctx.response(ctx.reply, reply))
.catch((error) => {
debug(error)
return ctx.response(ctx.reply, `Ooops, something went wrong`)
})
}

module.exports = { start }
22 changes: 22 additions & 0 deletions lib/commands/stop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const debug = require('debug')('midnabot:stop')

const { Starts } = require('../../models/starts')

const stop = () => (ctx) => {
const name = (ctx.update.message.chat.type === 'private')
? ctx.update.message.from.username
: ctx.update.message.chat.title

const reply = (ctx.update.message.chat.type === 'private')
? `midnabot is not listening in this chat`
: `midnabot is not listening in this channel`

Starts.upsert({ name, start: false })
.then(() => ctx.response(ctx.reply, reply))
.catch((error) => {
debug(error)
return ctx.response(ctx.reply, `Ooops, something went wrong`)
})
}

module.exports = { stop }
13 changes: 13 additions & 0 deletions lib/middlewares/after/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const debug = require('debug')('midnabot:after')

const { Starts } = require('../../models/starts')

const after = () => (ctx, next) => {
const name = (ctx.update.message.chat.type === 'private')
? ctx.update.message.from.username
: ctx.update.message.chat.title
Starts.findOne({ where: { name } })
.then((starts) => (starts.dataValues.start) ? next() : debug('not started'))
}

module.exports = { after }
5 changes: 5 additions & 0 deletions lib/middlewares/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { request } = require('./request')
const { response } = require('./response')
const { after } = require('./after')

module.exports = { request, response, after }
File renamed without changes.
12 changes: 12 additions & 0 deletions lib/middlewares/response/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const debug = require('debug')('midnabot:response')

const response = () => (ctx, next) => {
ctx.response = (callback, message) => {
debug(message)
return callback(message)
}

next()
}

module.exports = { response }
3 changes: 3 additions & 0 deletions lib/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { Starts } = require('./starts')

module.exports = { Starts }
8 changes: 8 additions & 0 deletions lib/models/starts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { Sequelize, sequelize } = require('../../modules/sequelize')

const Starts = sequelize.define('starts', {
name: { type: Sequelize.STRING, allowNull: false, unique: true },
start: { type: Sequelize.BOOLEAN, defaultValue: false }
})

module.exports = { Starts }
12 changes: 12 additions & 0 deletions lib/modules/configs/configs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"development": {
"username": "midnadevbot",
"token": "488808043:AAEnrG_7_jtm6k2eJm_zHHhV67Yaqxflje4"
},
"production": {
"username": "midnabot",
"token": "426124500:AAHyqS64s1TEZjwMQbMFEeE7NxC8bl5CDgk",
"webhook": "https://wsknorth.io/midnabot",
"port": 4000
}
}
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { configs } = require('./configs')
const { sequelize } = require('./sequelize')

module.exports = { configs, sequelize }
10 changes: 10 additions & 0 deletions lib/modules/sequelize/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Sequelize = require('sequelize')

const sequelize = new Sequelize('midnabot', null, null, {
dialect: 'sqlite',
storage: './database/midnabot.sqlite',
operatorsAliases: false,
logging: false
})

module.exports = { Sequelize, sequelize }
8 changes: 0 additions & 8 deletions lib/response/index.js

This file was deleted.

Loading

0 comments on commit b83a8bf

Please sign in to comment.