This commit is contained in:
sheychen 2019-04-28 14:44:56 +02:00
parent e233291e48
commit 800af40c92
1 changed files with 28 additions and 21 deletions

View File

@ -2,36 +2,43 @@ require('dotenv').config()
const database = require('./database.json') const database = require('./database.json')
const Mastodon = require('mastodon-api') const Mastodon = require('mastodon-api')
function getSubKey(arr, key, lang) {
return arr[`${key}_${lang}`] || arr[lang]
}
function randomValue(arr) {
return arr[Math.floor(Math.random() * arr.length)]
}
const TOKEN = 'ACCESS_TOKEN' const TOKEN = 'ACCESS_TOKEN'
const URL = 'API_URL' const URL = 'API_URL'
const VISIBILITY = 'VISIBILITY' const VISIBILITY = 'VISIBILITY'
Object.entries(process.env) const CONF = process.env
Object.keys(CONF)
.filter(e => e[0].startsWith(TOKEN)) .filter(e => e[0].startsWith(TOKEN))
.forEach(e => { .map(key => key.substring(TOKEN.length + 1))
const lang = e[0].substring(TOKEN.length + 1) .forEach(async (lang) => {
const visi = process.env[`${VISIBILITY}_${lang}`] || process.env[VISIBILITY] const getOption = (key) => getSubKey(CONF, key, lang)
const visi = getOption(VISIBILITY)
const M = new Mastodon({ const M = new Mastodon({
access_token: process.env[`${TOKEN}_${lang}`] || process.env[TOKEN], access_token: getOption(TOKEN),
api_url: `${process.env[`${URL}_${lang}`] || process.env[URL]}/api/v1/`, api_url: `${getOption(URL)}/api/v1/`,
timeout_ms: 60 * 1000 timeout_ms: 60 * 1000
}) })
M.get('accounts/verify_credentials').then( const me = await M.get('accounts/verify_credentials')
me => { if (me.data.error) throw me.data.error
if (me.data.error) {
console.error(me.data.error)
return
}
M.get(`accounts/${me.data.id}/followers`, { limit: 9999 }).then(fol => { const fols = await M.get(`accounts/${me.data.id}/followers`, { limit: 9999 })
for (const follow of fol.data) { for (const follow of fols.data) {
const messages = database[Math.floor(Math.random() * database.length)] M.post('statuses', {
const text = lang.length > 0 ? messages[lang] : '\n' + Object.entries(messages).map(m => `[${m[0]}] ${m[1]}`).join('\n\n') status: `@${follow.acct} ${lang.length > 0 ?
randomValue(database)[lang] :
M.post('statuses', { status: `@${follow.acct} ${text}`, visibility: visi }) '\n' + Object.entries(randomValue(database)).map(m => `[${m[0]}] ${m[1]}`).join('\n\n')
} }`, visibility: visi
})
}) })
}) }
})