ncd-bot/lib/replybot.js
2023-02-09 20:18:05 -05:00

49 lines
1.7 KiB
JavaScript

'use strict';
import fetch from 'node-fetch';
import { getLatestStatuses } from './api.js';
const chooseStatusID = (array) => {
return array[Math.floor(Math.random() * array.length)];
};
const emoteChoice = async () =>{ // pulls current emojis from the instance
const res = await fetch(`https://${process.env.INSTANCE_NAME}/api/v1/pleroma/emoji`, {
method: "GET",
});
const data = await res.json();
let emoteArray = [];
for (let e in data){
emoteArray.push(e);
}
return emoteArray[Math.floor(Math.random() * emoteArray.length)];
};
const replyChoice = async () =>{
const replyArr = [ // add desired replies here - it's just a big ass array
"example1",
"example 2",
"EXAMPLE 3",
];
return replyArr[Math.floor(Math.random() * replyArr.length)];
};
export const postReply = async () =>{
const headers = {Authorization: `Bearer ${process.env.REPLYBOT_BEARER}`, "Content-Type": "application/json"};
const mentions = [];
const statusIDArr = await getLatestStatuses(20);
const target = chooseStatusID(statusIDArr);
if (target[3]) {
for (let i = 0; i < target[3].length; i++){
mentions.push(target[3][i].acct);
}
};
const emoji = await emoteChoice();
const word = await replyChoice();
const res = await fetch(`https://${process.env.INSTANCE_NAME}/api/v1/statuses`, {
method: "POST",
headers: headers,
body: JSON.stringify({status: `${word} :${emoji}:`, in_reply_to_id: target[0], to: mentions}),
});
if (res.status === 200) {console.log('Replybot posted!')} else {console.log('Replybot failed:', res.status, res.statusText, res.url, word, emoji)}
};