Peaceful D&D Bot
@raymondcamden
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
Cron Scheduler
Deploy to configure a custom schedule
This workflow runs on Pipedream's servers and is triggered on a custom schedule.
steps.
get_monsters
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
}
27
const { default: fetch } = await import('node-fetch');

// length to cache in ms (five days)
const CACHE_LENGTH = 5 * 24 * 60 * 60 * 1000;

if($checkpoint && $checkpoint.monsterCache && $checkpoint.monsterCache.lasthit) {
  let lasthit = $checkpoint.monsterCache.lasthit;
  let now = new Date().getTime();
  console.log(`duration is ${now-lasthit}ms`);
  if(now - lasthit < CACHE_LENGTH) this.monsters = $checkpoint.monsterCache.monsters;
  if(this.monsters) console.log('i used the cached version');
}

if(!this.monsters) {
  console.log('need to fetch monsters');
  //first get all the monsters
  let resp = await fetch('https://www.dnd5eapi.co/api/monsters');
  let data = await resp.json();
  this.monsters = data.results;
  if(!$checkpoint) $checkpoint = {};
  $checkpoint.monsterCache = {
    lasthit:new Date().getTime(),
    monsters:data.results
  }
}
steps.
select_monsters
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
}
28
const { default: fetch } = await import('node-fetch');

getRandomInt = function(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

randomAlignment = function() {
  let law = ["lawful","neutral","chaotic"];
  let good = ["good","neutral","evil"];
  let alignment = law[getRandomInt(0,3)] + ' '+good[getRandomInt(0,3)];
  if(alignment === 'neutral neutral') alignment = 'neutral';
  return alignment;
}

this.monsterA = steps.get_monsters.monsters[getRandomInt(0, steps.get_monsters.monsters.length )].name;
//theoretically possible to select the same monter twice, I'm ok with that
this.monsterB = steps.get_monsters.monsters[getRandomInt(0, steps.get_monsters.monsters.length )].name;

// sometimes a creature name is: Werewolf, human form. Drop that
this.monsterA = this.monsterA.replace(/,.*/, '');
this.monsterB = this.monsterB.replace(/,.*/, '');

this.monsterAAlignment = randomAlignment();
this.monsterBAlignment = randomAlignment();
steps.
generate_conflict
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
}
34
// import { generateSlug } from "random-word-slugs";
const indefinite = require('indefinite');
const { generateSlug } = await import('random-word-slugs');
const fightTypes = ["argument","heated discussion","Facebook comments argument","fight","misunderstanding",
"war of words","confrontation","verbal battle","debate","violent disagreement"];
const resolveTypes = [
  "over a cup of tea",
  "with a good hug",
  "by calmly discussing their problem",
  "with an epic dance off",
  "by discussing the merits of cats instead"

];
getRandomInt = function(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}

let conflict = generateSlug(1, { 
    partsOfSpeech:['noun'], 
    format: 'lower' });

let fight = fightTypes[getRandomInt(0, fightTypes.length)];

let resolution = resolveTypes[getRandomInt(0, resolveTypes.length)];
    
this.conflict_text = `
${indefinite(steps.select_monsters.monsterAAlignment, {capitalize:true})} ${steps.select_monsters.monsterA} and ${indefinite(steps.select_monsters.monsterBAlignment)} ${steps.select_monsters.monsterB} are having a ${fight} over ${indefinite(conflict)}.
They resolve their issue ${resolution}.
`;
console.log(this.conflict_text);
steps.
post_tweet
Post a tweet.
auth
(auths.twitter)
params
Status
 
string ·params.status
Optional
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
}
23
const axios = require('axios')
const {status, in_reply_to_status_id, auto_populate_reply_metadata, exclude_reply_user_ids, attachment_url, media_ids, possibly_sensitive, lat, long, place_id, display_coordinates, trim_user, enable_dmcommands, fail_dmcommands, card_uri} = params
const body = {
  config: {
    url: `https://api.twitter.com/1.1/statuses/update.json`,
    method: 'POST',
    params,
  },
  token: {
    key: auths.twitter.oauth_access_token,
    secret: auths.twitter.oauth_refresh_token,
  },
}
const proxy = "https://api.pipedream.com/v1/oauth1/app_13GhY1"
const resp = await axios.post(proxy,body)

const {messages, data} = resp.data
for (const message of messages) {
  console.log(message)
}
return data