Skip to content

Commit

Permalink
add. store pick result to db
Browse files Browse the repository at this point in the history
  • Loading branch information
luswdev committed Oct 23, 2022
1 parent 010a138 commit a58a85b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
5 changes: 5 additions & 0 deletions cmds/CmdBase.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'

const ConnDB = require('../data/mysql.js')
const { db } = require('../config.json')

class CmdBase {

constructor (_key = '', _info = '') {
this.cmdKey = _key
this.cmdInfo = _info
this.infoUrlBase = 'https://splatoonwiki.org/wiki/'

this.mysql = new ConnDB(db.host, db.user, db.password, db.database, db.table)
}

infoUrl(_name) {
Expand Down
2 changes: 2 additions & 0 deletions cmds/CmdRandomMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class CmdRandomMap extends CmdBase {
.setFooter({ text: `Requested by ${_interaction.user.username}`, iconURL: _interaction.user.avatarURL()})
.setTimestamp()

this.mysql.saveResult(this.cmdKey, map.en, _interaction.user.id)

_interaction.reply({ embeds: [mapEmbed] })
}
}
Expand Down
2 changes: 2 additions & 0 deletions cmds/CmdRandomWeapon.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class CmdRandomWeapon extends CmdBase {
.setFooter({ text: `Requested by ${_interaction.user.username}`, iconURL: _interaction.user.avatarURL()})
.setTimestamp()

this.mysql.saveResult(this.cmdKey, weapon.en, _interaction.user.id)

_interaction.reply({ embeds: [weaponEmbed] })
}
}
Expand Down
4 changes: 2 additions & 2 deletions data/database.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict'

const mapList = require('../data/map.js')
const weaponList = require('../data/weapon.js')
const mapList = require('./map.js')
const weaponList = require('./weapon.js')

class Database {

Expand Down
31 changes: 31 additions & 0 deletions data/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const mysql = require('mysql')

class ConnDB {

constructor (_host, _user, _password, _database, _table) {
this.conn = mysql.createConnection({
host : _host,
user : _user,
password : _password,
database : _database,
})
this.table = _table

this.conn.connect()
}

saveResult (_cmd, _res, _user) {
let query = this.conn.query(`INSERT INTO ${this.table} (user_id, command, result) VALUES (?, ?, ?)`, [_user, _cmd, _res])
query
.on('error', (err) => {
console.log(`[${__filename}] cannot save result, error: `, err)
})
.on('end', () => {
console.log(`[${__filename}] save result end`)
})
}
}

module.exports = ConnDB

0 comments on commit a58a85b

Please sign in to comment.