Add fm set username command
Beginning of the command. Can't do anything other than setting and updating your lastfm username.
This commit is contained in:
parent
bdc08b3153
commit
8c02dc5517
4 changed files with 109 additions and 0 deletions
65
util/lastfm/fmlogin.js
Normal file
65
util/lastfm/fmlogin.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
const sqlite3 = require('sqlite3').verbose();
|
||||
module.exports = async function(userID, lastfmUsername) {
|
||||
let sendText = "";
|
||||
// If the user already has a last.fm username set, just update it.
|
||||
let entryExists = await new Promise((resolve, reject)=>{
|
||||
const db = new sqlite3.Database('data/database.db');
|
||||
db.get(
|
||||
`SELECT * FROM lastfm WHERE userID = ?`,
|
||||
[userID],
|
||||
(error, row) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
reject(error);
|
||||
} else {
|
||||
resolve(row);
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
);
|
||||
});
|
||||
if (!entryExists) {
|
||||
|
||||
sendText = await new Promise((resolve, reject)=>{
|
||||
const db = new sqlite3.Database('data/database.db');
|
||||
|
||||
db.run(
|
||||
`INSERT INTO lastfm (userID, lastfmUsername) VALUES (?, ?)`,
|
||||
[userID, lastfmUsername],
|
||||
function (error) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
let sendText = "Error while adding last.fm username. Check console.";
|
||||
reject(sendText);
|
||||
} else {
|
||||
let sendText = `Your last.fm username has been set to '${lastfmUsername}'.`;
|
||||
console.log(sendText);
|
||||
resolve(sendText);
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
);
|
||||
});
|
||||
}else {
|
||||
sendText = await new Promise((resolve, reject)=>{
|
||||
const db = new sqlite3.Database('data/database.db');
|
||||
db.run(
|
||||
`UPDATE lastfm SET lastfmUsername = ? WHERE userID = ?`,
|
||||
[lastfmUsername, userID],
|
||||
function (error) {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
let sendText = "Error while updating last.fm username. Check console.";
|
||||
reject(sendText);
|
||||
} else {
|
||||
let sendText = `Your last.fm username has been updated to '${lastfmUsername}'.`;
|
||||
console.log(sendText);
|
||||
resolve(sendText);
|
||||
}
|
||||
db.close();
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
return sendText;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue