Skip to content
Snippets Groups Projects
Commit 49047238 authored by Guillaume Vagner's avatar Guillaume Vagner
Browse files

saving usernames

parent de3cc1d7
No related branches found
No related tags found
No related merge requests found
......@@ -36,11 +36,15 @@ Promise.all([
expiration TEXT,
schedule TEXT
)`),
query(`CREATE TABLE IF NOT EXISTS groups(
chatId BIGINT NOT NULL,
grp INT,
primary key (chatId, grp)
)`),
query(`CREATE TABLE IF NOT EXISTS users(
userid INT PRIMARY KEY NOT NULL,
name TEXT,
username TEXT
)`)
]).then(_ => {
......@@ -127,5 +131,39 @@ function getSchedules() {
`)
}
function addUser(data) {
return query(`
SELECT *
FROM users
WHERE userid=${data.userid}
`).then(rep => {
if (rep.length === 0) {
return query(`
INSERT INTO users
VALUES(${data.userid}, "${data.name}", "${data.username}")
`)
} else {
return query(`
UPDATE users
SET username="${data.username}"
WHERE userid=${data.userid}
`)
}
})
}
module.exports = { query, getChanByChatId, createChan, deleteChanByChatId, modifyChan, getChanByState, addGroup, removeGroup, removeAllGroups, getCompos, getSchedules };
\ No newline at end of file
module.exports = {
query,
getChanByChatId,
createChan,
deleteChanByChatId,
modifyChan,
getChanByState,
addGroup,
removeGroup,
removeAllGroups,
getCompos,
getSchedules,
addUser
};
\ No newline at end of file
......@@ -4,7 +4,7 @@
const rp = require('request-promise');
// Modules propres
var { modifyChan, getChanByState } = require('./mysql');
var { modifyChan, getChanByState, addUser } = require('./mysql');
// Configurations
const config = require('./config');
......@@ -119,6 +119,15 @@ function getFirstToken(code, state) {
chan.refresh = rep.refresh_token;
chan.expiration = rep.expires_at;
chan.state = '';
if (chan.chatId > 0) {
getMe(chan).then(me => {
addUser({
userid: me.id,
name: `${me.firstName} ${me.lastName}`,
username: chan.username
})
})
}
return modifyChan(chan)
})
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment