Fantasia18

AI Dungeon Deletion

Jun 6th, 2021
1,275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Licensed as Open Source under MIT. Full License: https://pastebin.com/gwqXTTLY
  2.  
  3. getAccessToken = () => {
  4.   let token;
  5.   try{
  6.       token = JSON.parse(localStorage.LOGGED_IN_USER).accessToken;
  7.   }
  8.   catch(err){
  9.       throw "Could not get access token. Are you logged in?"
  10.   }
  11.   if(!token){
  12.     throw "Empty token."
  13.   }
  14.   return token;
  15. };
  16.  
  17. runMutation = async(accessToken, publicID, mutationName) => {
  18.   return fetch("https://api.aidungeon.io/graphql", {
  19.     "headers": {
  20.       "accept": "*/*",
  21.       "cache-control": "no-cache",
  22.       "content-type": "application/json",
  23.       "pragma": "no-cache",
  24.       "x-access-token": accessToken
  25.     },
  26.     "referrer": "https://play.aidungeon.io/",
  27.     "referrerPolicy": "strict-origin-when-cross-origin",
  28.     "body": JSON.stringify({
  29.           query: `mutation ($publicId: String) {
  30.             ${mutationName}(publicId: $publicId) {
  31.               id
  32.               publicId
  33.               deletedAt
  34.               isSaved
  35.               __typename
  36.             }
  37.           }`,
  38.           variables: {
  39.             publicId: publicID
  40.           }
  41.         }),
  42.     "method": "POST",
  43.     "mode": "cors",
  44.     "credentials": "omit"
  45.   });
  46. }
  47.  
  48.  
  49. deleteAdventure = async (accessToken, publicID) => {
  50.     console.log(`Deleting Adventure: ${publicID}`);
  51.     return runMutation(accessToken, publicID, "deleteAdventure");
  52. }
  53.  
  54.  
  55. restoreAdventure = async (accessToken, publicID) => {
  56.     console.log(`Restoring Adventure: ${publicID}`);
  57.     return runMutation(accessToken, publicID, "restoreAdventure");
  58. }
  59.  
  60.  
  61. deleteScenario = async (accessToken, publicID) => {
  62.     console.log(`Deleting Scenario: ${publicID}`);
  63.     return runMutation(accessToken, publicID, "deleteScenario");
  64. }
  65.  
  66. restoreScenario = async (accessToken, publicID) => {
  67.     console.log(`Restoring Scenario: ${publicID}`);
  68.     return runMutation(accessToken, publicID, "restoreScenario");
  69. }
  70.  
  71. deletePost = async (accessToken, publicID) => {
  72.     console.log(`Deleting Post: ${publicID}`);
  73.     return runMutation(accessToken, publicID, "deletePost");
  74. }
  75.  
  76.  
  77. restorePost = async (accessToken, publicID) => {
  78.     console.log(`Restoring Post: ${publicID}`);
  79.     return runMutation(accessToken, publicID, "restorePost");
  80. }
  81.  
  82. emptyTrash = () => {
  83.   return fetch("https://api.aidungeon.io/graphql", {
  84.     "headers": {
  85.       "accept": "*/*",
  86.       "cache-control": "no-cache",
  87.       "content-type": "application/json",
  88.       "pragma": "no-cache",
  89.       "x-access-token": accessToken
  90.     },
  91.     "referrer": "https://play.aidungeon.io/",
  92.     "referrerPolicy": "strict-origin-when-cross-origin",
  93.     "body": JSON.stringify({
  94.           query: `mutation {
  95.             emptyTrash
  96.           }`,
  97.           variables: {}
  98.         }),
  99.     "method": "POST",
  100.     "mode": "cors",
  101.     "credentials": "omit"
  102.   });
  103. }
  104.  
  105.  
  106. fetchContent = async (accessToken, contentType, checkTrash) => {
  107.   const r = await fetch("https://api.aidungeon.io/graphql", {
  108.     "headers": {
  109.       "accept": "*/*",
  110.       "accept-language": "en-US,en;q=0.9",
  111.       "cache-control": "no-cache",
  112.       "content-type": "application/json",
  113.       "pragma": "no-cache",
  114.       "x-access-token": accessToken
  115.     },
  116.     "referrer": "https://play.aidungeon.io/",
  117.     "referrerPolicy": "strict-origin-when-cross-origin",
  118.     "body": JSON.stringify({
  119.           query: `query ($input: SearchInput) {
  120.             user {
  121.               id
  122.               search(input: $input) {
  123.                 publicId
  124.                 __typename
  125.               }
  126.               __typename
  127.             }
  128.           }`,
  129.           variables: {
  130.             input: {
  131.               contentType: contentType,
  132.               offset: 0,
  133.               saved: false,
  134.               searchTerm: "",
  135.               sortOrder: "actionCount",
  136.               trash: contentType === "trash" || checkTrash
  137.              }
  138.           }
  139.         }),
  140.     "method": "POST",
  141.     "mode": "cors",
  142.     "credentials": "omit"
  143.   })
  144.   return r.json();
  145. };
  146.  
  147. // data.adventure.actions is an array of objects where the text field is the story data. You'll have to pull that together.
  148.  
  149.  
  150. sleep = (ms) => {
  151.   return new Promise(resolve => setTimeout(resolve, ms));
  152. }
  153.  
  154.  
  155. deleteItemsOnFirstPage = async (accessToken, contentType) => {
  156.   console.log(`Checking for content...`)
  157.   r = await fetchContent(accessToken, contentType);
  158.   if(r.data.user.search.length > 0){
  159.     await Promise.all(r.data.user.search.map(
  160.       ea => {
  161.         __typename = ea.__typename.toLowerCase();
  162.         if(__typename === "scenario"){
  163.           deleteScenario(accessToken, ea.publicId)
  164.         }
  165.         else if(__typename === "adventure"){
  166.           deleteAdventure(accessToken, ea.publicId)
  167.         }
  168.         else if(__typename === "post"){
  169.           deletePost(accessToken, ea.publicId)
  170.         }
  171.       }
  172.     ))
  173.      return true
  174.   }
  175.   else{
  176.     return false
  177.   }
  178. }
  179.  
  180. restoreItemsOnFirstPage = async (accessToken, contentTypes) => {
  181.   console.log(`Restoring trash...`)
  182.   items = await getTrash(accessToken, contentTypes);
  183.   if(items.length > 0){
  184.     await Promise.all(items.map(
  185.       ea => {
  186.         __typename = ea.__typename.toLowerCase();
  187.         if(__typename === "scenario"){
  188.           restoreScenario(accessToken, ea.publicId)
  189.         }
  190.         else if(__typename === "adventure"){
  191.           restoreAdventure(accessToken, ea.publicId)
  192.         }
  193.         else if(__typename === "post"){
  194.           restorePost(accessToken, ea.publicId)
  195.         }
  196.       }
  197.     ))
  198.      return true
  199.   }
  200.   else{
  201.     return false
  202.   }
  203. }
  204.  
  205.  
  206. restoreTrash = async (contentTypes = ["scenario","adventure", "post"]) => {
  207.   accessToken = getAccessToken();
  208.   hasMore = true;
  209.   console.log("restoring")
  210.   while(hasMore){
  211.     hasMore = await restoreItemsOnFirstPage(accessToken, contentTypes);
  212.     console.log("Waiting 1.5 seconds...")
  213.     await sleep(1500);
  214.     if(!hasMore){
  215.        console.log("Done. Content Restored.")
  216.     }
  217.   }
  218. }
  219.  
  220. deleteAllContent = async (contentType) => {
  221.   accessToken = getAccessToken();
  222.   hasMore = true;
  223.   console.log(`Initiating Delete of all ${contentType}`)
  224.   while(hasMore){
  225.     hasMore = await deleteItemsOnFirstPage(accessToken, contentType);
  226.     console.log("Waiting 1.5 seconds...")
  227.     await sleep(1500);
  228.     if(!hasMore){
  229.        console.log(`Done. Deleted all ${contentType}`)
  230.     }
  231.   }
  232. }
  233.  
Advertisement
Add Comment
Please, Sign In to add comment