Glitch bot error

SyntaxError: Unexpected token {

2:07 PM

at createScript (vm.js:80:10)

2:07 PM

at Object.runInThisContext (vm.js:139:10)

2:07 PM

at Module._compile (module.js:617:28)

2:07 PM

at Object.Module._extensions…js (module.js:664:10)

2:07 PM

at Module.load (module.js:566:32)

2:07 PM

at tryModuleLoad (module.js:506:12)

2:07 PM

at Function.Module._load (module.js:498:3)

2:07 PM

at Module.require (module.js:597:17)

2:07 PM

at require (internal/module.js:11:18)

2:07 PM

at Object. (/rbd/pnpm-volume/de9b40d3-4883-4649-b67c-48e37e7b4c67/node_modules/.registry.npmjs.org/discord.js/12.2.0/node_modules/discord.js/src/index.js:8:11)

2:07 PM

a few seconds ago

/rbd/pnpm-volume/de9b40d3-4883-4649-b67c-48e37e7b4c67/node_modules/.registry.npmjs.org/discord.js/12.2.0/node_modules/discord.js/src/client/Client.js:41

2:07 PM

} catch {

2:07 PM

^

2:07 PM

2:07 PM

SyntaxError: Unexpected token {

2:07 PM

at createScript (vm.js:80:10)

2:07 PM

at Object.runInThisContext (vm.js:139:10)

2:07 PM

at Module._compile (module.js:617:28)

2:07 PM

at Object.Module._extensions…js (module.js:664:10)

2:07 PM

at Module.load (module.js:566:32)

2:07 PM

at tryModuleLoad (module.js:506:12)

2:07 PM

at Function.Module._load (module.js:498:3)

2:07 PM

at Module.require (module.js:597:17)

2:07 PM

at require (internal/module.js:11:18)

2:07 PM

at Object. (/rbd/pnpm-volume/de9b40d3-4883-4649-b67c-48e37e7b4c67/node_modules/.registry.npmjs.org/discord.js/12.2.0/node_modules/discord.js/src/index.js:8:11)

2:07 PM

Bot is no longer working. i used “add package” to update discord.js and then recieved this error

The error is because somewhere in your code there was an unexpected token { or }. Can you please share some of the code where the error is thrown from?

1 Like

Cannot open. Just copy the code in a code snippet here.

// Init project
const http = require(‘http’);
const express = require(‘express’);
const Discord = require(“discord.js”);
const fs = require(“fs”);

const app = express();
const client = new Discord.Client();

const prefix = “!”;

var writeChannel;
var message;
var mates = ;

// Keeping the project “alive”
app.get("/", (request, response) => {
console.log(Date.now() + " Ping Received");
main(); // Subsequent call
response.sendStatus(200);
});
app.listen(process.env.PORT);
setInterval(() => {
http.get(http://${process.env.PROJECT_DOMAIN}.glitch.me/);
}, 60000);

// Initialize the bot
client.on(“ready”, async () => {
client.user.setPresence({game: {name: “live payout countdowns”, type: 0}});
writeChannel = client.channels.get(process.env.writeChannelId);
initializeMessageObject();
})
client.login(process.env.botToken);

console.log(“App restarted”);

// Parse a JSON data file
let shardData = JSON.parse(fs.readFileSync("./shard-data.json", “utf8”));
parseData();

// Initial call
main();

// Below are the rest of the functions that make up the bot
async function main () {
try {
console.log(“Try”);
if (message) {
calculateSecondsUntilPayout();
await sendMessage();
} else if (writeChannel) {
initializeMessageObject();
} else {
console.log(“Something is wrong”);
}
} catch(err) {
console.log(err);
initializeMessageObject();
} finally {
// setTimeout(main, 60000 - Date.now() % 60000);
console.log(‘Try finally’);
}
}

async function initializeMessageObject () {
// fetch message, create a new one if necessary
console.log(‘Start initializing message object’);
const messages = await writeChannel.fetchMessages();
if (messages.array().length === 0) {
try {
message = await writeChannel.send({embed: new Discord.RichEmbed()});
} catch (err) {
console.log(err);
}
} else {
if (messages.first().embeds.length === 0) {
await messages.first().delete();
message = await writeChannel.send({embed: new Discord.RichEmbed()});
} else {
message = messages.first();
}
}
console.log(‘Message object initialized’);
}

function parseData () {
for (let i in shardData) {
const user = shardData[i];
mates.push({
name: user.Name,
payout: parseInt(user.UTC.substr(0,2)),
flag: user.Flag,
swgoh: user.SWGOH,
utc: user.UTC
});
}
const matesByTime = {};
for (let i in mates) {
const mate = mates[i];
if (!matesByTime[mate.payout]) {
matesByTime[mate.payout] = {
payout: mate.payout,
mates:
}
}
matesByTime[mate.payout].mates.push(mate);
}
mates = Object.values(matesByTime);
}

function calculateSecondsUntilPayout () {
const now = new Date();
for (let i in mates) {
const mate = mates[i];
const p = new Date();
p.setUTCHours(mate.payout, 0, 0, 0);
if (p < now) p.setDate(p.getDate() + 1);
mate.timeUntilPayout = p.getTime() - now.getTime();
let dif = new Date(mate.timeUntilPayout);
const round = dif.getTime() % 60000;
if (round < 30000) {
dif.setTime(dif.getTime() - round);
} else {
dif.setTime(dif.getTime() + 60000 - round);
}
mate.time = ${String(dif.getUTCHours()).padStart(2, '00')}:${String(dif.getUTCMinutes()).padStart(2, '00')};
}
mates.sort((a, b) => {
return a.timeUntilPayout - b.timeUntilPayout;
})
}

async function sendMessage () {
let embed = new Discord.RichEmbed().setThumbnail(‘https://i.imgur.com/OEwutbb.png’);
let desc = ‘Estimated Payout:’;
for (let i in mates) {
let fieldName = String(mates[i].time) + " - (UTC " + mates[i].payout + “:00)”;
let fieldText = ‘’;
for (const mate of mates[i].mates) {
fieldText += ${mate.flag} [${mate.name}](${mate.swgoh})\n; // Discord automatically trims messages
}
embed.addField(fieldName, fieldText, true);
}
embed.setDescription(desc);
embed.setFooter(‘Last refresh’, ‘https://i.imgur.com/OEwutbb.png’);
embed.setTimestamp();
await message.edit({embed});
console.log(‘Message send’);
}

im totally winging it btw. i followed instructions on how to set this up. its not my work. idk what a code snippet is. lol. btw i do appreciate your help

Can you add codeblocks around your code please?

```js
// code here
```

Just copy and paste again

// Init project
const http = require("http");
const express = require("express");
const Discord = require("discord.js");
const fs = require("fs");

const app = express();
const client = new Discord.Client();

const prefix = "!";

var writeChannel;
var message;
var mates = [];

// Keeping the project "alive"
app.get("/", (request, response) => {
  console.log(Date.now() + " Ping Received");
  main(); // Subsequent call
  response.sendStatus(200);
});
app.listen(process.env.PORT);
setInterval(() => {
  http.get(`http://${process.env.PROJECT_DOMAIN}.glitch.me/`);
}, 60000);

// Initialize the bot
client.on("ready", async () => {
  client.user.setPresence({
    game: { name: "live payout countdowns", type: 0 }
  });
  writeChannel = client.channels.get(process.env.writeChannelId);
  initializeMessageObject();
});
client.login(process.env.botToken);

console.log("App restarted");

// Parse a JSON data file
let shardData = JSON.parse(fs.readFileSync("./shard-data.json", "utf8"));
parseData();

// Initial call
main();

// Below are the rest of the functions that make up the bot
async function main() {
  try {
    console.log("Try");
    if (message) {
      calculateSecondsUntilPayout();
      await sendMessage();
    } else if (writeChannel) {
      initializeMessageObject();
    } else {
      console.log("Something is wrong");
    }
  } catch (err) {
    console.log(err);
    initializeMessageObject();
  } finally {
    //  setTimeout(main, 60000 - Date.now() % 60000);
    console.log("Try finally");
  }
}

async function initializeMessageObject() {
  // fetch message, create a new one if necessary
  console.log("Start initializing message object");
  const messages = await writeChannel.fetchMessages();
  if (messages.array().length === 0) {
    try {
      message = await writeChannel.send({ embed: new Discord.RichEmbed() });
    } catch (err) {
      console.log(err);
    }
  } else {
    if (messages.first().embeds.length === 0) {
      await messages.first().delete();
      message = await writeChannel.send({ embed: new Discord.RichEmbed() });
    } else {
      message = messages.first();
    }
  }
  console.log("Message object initialized");
}

function parseData() {
  for (let i in shardData) {
    const user = shardData[i];
    mates.push({
      name: user.Name,
      payout: parseInt(user.UTC.substr(0, 2)),
      flag: user.Flag,
      swgoh: user.SWGOH,
      utc: user.UTC
    });
  }
  const matesByTime = {};
  for (let i in mates) {
    const mate = mates[i];
    if (!matesByTime[mate.payout]) {
      matesByTime[mate.payout] = {
        payout: mate.payout,
        mates: []
      };
    }
    matesByTime[mate.payout].mates.push(mate);
  }
  mates = Object.values(matesByTime);
}

function calculateSecondsUntilPayout() {
  const now = new Date();
  for (let i in mates) {
    const mate = mates[i];
    const p = new Date();
    p.setUTCHours(mate.payout, 0, 0, 0);
    if (p < now) p.setDate(p.getDate() + 1);
    mate.timeUntilPayout = p.getTime() - now.getTime();
    let dif = new Date(mate.timeUntilPayout);
    const round = dif.getTime() % 60000;
    if (round < 30000) {
      dif.setTime(dif.getTime() - round);
    } else {
      dif.setTime(dif.getTime() + 60000 - round);
    }
    mate.time = `${String(dif.getUTCHours()).padStart(2, "00")}:${String(
      dif.getUTCMinutes()
    ).padStart(2, "00")}`;
  }
  mates.sort((a, b) => {
    return a.timeUntilPayout - b.timeUntilPayout;
  });
}

async function sendMessage() {
  let embed = new Discord.RichEmbed().setThumbnail(
    "https://i.imgur.com/OEwutbb.png"
  );
  let desc = "**Estimated Payout**:";
  for (let i in mates) {
    let fieldName =
      String(mates[i].time) + " - (UTC " + mates[i].payout + ":00)";
    let fieldText = "";
    for (const mate of mates[i].mates) {
      fieldText += `${mate.flag} [${mate.name}](${mate.swgoh})\n`; // Discord automatically trims messages
    }
    embed.addField(fieldName, fieldText, true);
  }
  embed.setDescription(desc);
  embed.setFooter("Last refresh", "https://i.imgur.com/OEwutbb.png");
  embed.setTimestamp();
  await message.edit({ embed });
  console.log("Message send");
}

Weird, I cannot seem to reproduce?

this is the source. The only thing i changed was the discord.js version and express version. i did it through the “add package” button at the top.