Skip to content

Instantly share code, notes, and snippets.

@tcyrus
Last active May 24, 2021 04:21
Show Gist options
  • Save tcyrus/89c01c00c74c9f7244b3b00a35f56da0 to your computer and use it in GitHub Desktop.
Save tcyrus/89c01c00c74c9f7244b3b00a35f56da0 to your computer and use it in GitHub Desktop.
const { Client, RichEmbed } = require('discord.js');
const client = new Client();
const { Story } = require('inkjs');
const fs = require('fs');
const { Pool } = require('pg');
const pool = new Pool({
user: 'me',
host: 'localhost',
database: 'api',
password: 'password',
port: 5432,
});
const inkFile = fs.readFileSync('./intercept.ink.json', 'UTF-8').replace(/^\uFEFF/, '');
function backupStoryState(uid, state) {
const query = ['INSERT INTO user_progress (uid, state)',
'VALUES($1, $2)',
'ON CONFLICT (id) DO UPDATE',
'SET state = EXCLUDED.state'];
return pool.connect().then(client => {
return client.query({
name: 'backup-story-state',
text: query.join('\n'),
values: [uid, state.jsonToken],
});
});
}
function recoverStoryState(uid, state) {
return pool.connect().then(client => {
return client.query({
name: 'recover-story-state',
text: 'SELECT state FROM user_progress WHERE uid = $1',
values: [uid],
rowMode: 'array',
}).then(res => res.rows[0][0]);
}).then(jsonToken => {
const snapshot = state.Copy();
snapshot.jsonToken = jsonToken;
return snapshot;
});
}
function recoverStoryState(uid, state) {
return pool.connect().then(client => {
return client.query({
name: 'recover-story-state',
text: 'SELECT state FROM user_progress WHERE uid = $1',
values: [uid],
rowMode: 'array',
}).then(res => res.rows[0][0]);
}).then(jsonToken => {
state.jsonToken = jsonToken;
return snapshot;
});
}
function choicesToEmbed(choices) {
const re = new RichEmbed().setTitle('Choices').setColor(0xFF0000);
return choices.reduce((re, choice, idx) =>
re.addField(idx.toString(), choice), re);
}
// create a new story
//const myStory = new Story(inkFile);
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
client.login('token');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment