Skip to content

Commit

Permalink
Optimisations and error management improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixLusseau committed Sep 25, 2023
1 parent e7b45ad commit ee810fa
Show file tree
Hide file tree
Showing 14 changed files with 70 additions and 83 deletions.
1 change: 0 additions & 1 deletion OPM-Bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const { ClashRoyaleAPI } = require('@varandas/clash-royale-api')
const Discord = require("discord.js");
require("dotenv").config();

const bot = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
Expand Down
5 changes: 1 addition & 4 deletions commands/ffattacks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ async function ffattacks(bot, api, interaction, pingBool, guildId, channel, clan
try {
RiverRace = await api.getClanCurrentRiverRace(clan)
} catch (error) {
const myError = '[' + error.response.headers.date + ']: Error:' + error.response.status + ' ' + error.response.statusText
console.error(myError);
console.log("CR-API error : ", error)
functions.errorEmbed(bot, interaction, channel)
functions.errorEmbed(bot, interaction, channel, error)
return
}
let points = 0
Expand Down
3 changes: 1 addition & 2 deletions commands/ffgetrotates.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const sqlite3 = require('sqlite3').verbose();

module.exports = {
Expand Down
4 changes: 1 addition & 3 deletions commands/ffhelp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const Discord = require("discord.js");
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
Expand Down
23 changes: 11 additions & 12 deletions commands/ffmembers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const Discord = require("discord.js");
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const fs = require('fs');
const path = require('path');
const puppeteer = require('puppeteer');
Expand Down Expand Up @@ -46,15 +44,15 @@ module.exports = {
}
}
}
const membersEmbed = new EmbedBuilder();
api.getClanMembers(clan) // Get the clans' members from the Supercell API
.then((response) => {
return response
})
.catch((err) => {
console.log("CR-API error : ", err)
})
let response = await api.getClanMembers(clan)

let response = null
try {
response = await api.getClanMembers(clan)
} catch (error) {
functions.errorEmbed(bot, interaction, interaction.channel, error)
return
}

let Members = "";
let Members_text = "**" + response.length + " members**\n\n"
// Make the string with the members' names, tags, roles, levels and trophies
Expand Down Expand Up @@ -90,6 +88,7 @@ module.exports = {
await functions.renderCommand(interaction, tmpFile, 0, 100)

if (text != null) {
const membersEmbed = new EmbedBuilder();
const rand = Math.random().toString(36).slice(2); // Generate a random string to avoid the image cache
try {
membersEmbed
Expand Down
37 changes: 17 additions & 20 deletions commands/ffopponents.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const Discord = require("discord.js");
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const functions = require('../utils/functions.js');
const fs = require('fs');

Expand All @@ -26,32 +24,30 @@ module.exports = {
await interaction.deferReply({ ephemeral: false });
const clan = interaction.options.getString('clan');
let text = interaction.options.getBoolean('text_version'); // For text version too
const opponentsEmbed = new EmbedBuilder();
let Opponents = "";
let scores = [];
let charts = "";
api.getClanCurrentRiverRace(clan) // Get info about the River Race
.then((response) => {
return response
})
.catch((err) => {
console.log("CR-API error : ", err)
})
let RiverRace = await api.getClanCurrentRiverRace(clan)

let RiverRace = null
try {
RiverRace = await api.getClanCurrentRiverRace(clan)
} catch (error) {
functions.errorEmbed(bot, interaction, channel, error)
return
}

let opponentsText = "<ul style='font-size: 2em; text-align: left;'>\n";
let Labels = []
let seasons = []

for (let i = 0; i < RiverRace.clans.length; i++) {
api.getClanByTag(RiverRace.clans[i].tag) // Get the clans' info from the Supercell API
.then((clan) => {
return clan
})
.catch((err) => {
console.log("CR-API error : ", err)
})
let clan = await api.getClanByTag(RiverRace.clans[i].tag)
let clan = null
try {
clan = await api.getClanByTag(RiverRace.clans[i].tag)// Get the clans' info from the Supercell API
} catch (error) {
functions.errorEmbed(bot, interaction, channel, error)
return
}
Labels.push(RiverRace.clans[i].name)
// console.log("clan : " + RiverRace.clans[i].name)

Expand Down Expand Up @@ -146,6 +142,7 @@ module.exports = {
await functions.renderCommand(interaction, tmpFile, 0, 300)

if (text != null) {
const opponentsEmbed = new EmbedBuilder();
const rand = Math.random().toString(36).slice(2); // Generate a random string to avoid the image cache
try {
opponentsEmbed
Expand Down
5 changes: 1 addition & 4 deletions commands/ffrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ async function ffrace(bot, api, interaction, guildId, channel, clan, report) {
try {
RiverRace = await api.getClanCurrentRiverRace(clan)
} catch (error) {
const myError = '[' + error.response.headers.date + ']: Error:' + error.response.status + ' ' + error.response.statusText
console.error(myError);
console.log("CR-API error : ", error)
functions.errorEmbed(bot, interaction, channel)
functions.errorEmbed(bot, interaction, channel, error)
return
}

Expand Down
19 changes: 9 additions & 10 deletions commands/ffresults.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const functions = require('../utils/functions.js');

async function ffresults(bot, api, interaction, guildId, clan) {
// Check if the command was run by an interaction or a scheduled message
Expand All @@ -14,14 +14,13 @@ async function ffresults(bot, api, interaction, guildId, clan) {

const resultsEmbed = new EmbedBuilder();
let Players = "";
api.getClanCurrentRiverRace(clan) // Get info about the River Race
.then((response) => {
return response
})
.catch((err) => {
console.log("CR-API error : ", err)
})
let RiverRace = await api.getClanCurrentRiverRace(clan)
let RiverRace = null
try {
RiverRace = await api.getClanCurrentRiverRace(clan)// Get info about the River Race
} catch (error) {
functions.errorEmbed(bot, interaction, channel, error)
return
}

// Sort the players by fame
RiverRace.clan.participants.sort((a, b) => (a.fame < b.fame) ? 1 : -1)
Expand Down
21 changes: 9 additions & 12 deletions commands/ffriver.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const Discord = require("discord.js");
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const functions = require('../utils/functions.js');
const fs = require('fs');

Expand Down Expand Up @@ -44,19 +42,17 @@ module.exports = {
}
}
}
const riverEmbed = new EmbedBuilder();
let Labels = [];
let Datas = [];
let Race = "";

api.getClanCurrentRiverRace(clan) // Get info about the River Race
.then((response) => {
return response
})
.catch((err) => {
console.log("CR-API error : ", err)
})
let RiverRace = await api.getClanCurrentRiverRace(clan)
let RiverRace = null
try {
RiverRace = await api.getClanCurrentRiverRace(clan)// Get info about the River Race
} catch (error) {
functions.errorEmbed(bot, interaction, channel, error)
return
}

// Chart data processing
for (let i = 0; i < RiverRace.clans.length; i++) {
Expand Down Expand Up @@ -137,6 +133,7 @@ module.exports = {
await functions.renderCommand(interaction, tmpFile, 0, 0)

if (text != null) {
const riverEmbed = new EmbedBuilder();
const rand = Math.random().toString(36).slice(2); // Generate a random string to avoid the image cache
try {
riverEmbed
Expand Down
3 changes: 1 addition & 2 deletions commands/ffrmrotate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const sqlite3 = require('sqlite3').verbose();

module.exports = {
Expand Down
3 changes: 1 addition & 2 deletions commands/ffsethour.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const fs = require('fs');
const schedule = require('../utils/schedule.js');

Expand Down
3 changes: 1 addition & 2 deletions commands/ffsetrotate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const { SlashCommandBuilder } = require('discord.js');
const { EmbedBuilder } = require('discord.js');
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const sqlite3 = require('sqlite3').verbose();

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "OPM-Bot",
"version": "3.0.0",
"version": "3.0.1",
"description": "",
"main": "OPM-Bot.js",
"scripts": {
Expand Down
24 changes: 16 additions & 8 deletions utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const puppeteer = require('puppeteer');
const { AttachmentBuilder, EmbedBuilder } = require('discord.js');

// Function to send an error embed
function errorEmbed(bot, interaction, channel) {
function errorEmbed(bot, interaction, channel, error) {
const myError = '[' + error.response.headers.date + ']: Error:' + error.response.status + ' ' + error.response.statusText
console.error(myError);
console.log("CR-API error : ", error)
const errorEmbed = new EmbedBuilder()
errorEmbed
.setColor(0x0099FF)
Expand Down Expand Up @@ -55,13 +58,18 @@ function ratio(RiverRace, decksRemaining, i) {

// Function to fetch the clan's war history directly from the Supercell API not using the @varandas/clash-royale-api package (not available in it)
async function fetchHist(tag) {
const response = await fetch("https://api.clashroyale.com/v1/clans/%23" + tag + "/riverracelog", {
headers: {
authorization: `Bearer ${process.env.CR_TOKEN}`,
},
});
const jsonData = await response.json();
return jsonData;
try {
const response = await fetch("https://api.clashroyale.com/v1/clans/%23" + tag + "/riverracelog", {
headers: {
authorization: `Bearer ${process.env.CR_TOKEN}`,
},
});
const jsonData = await response.json();
return jsonData;
} catch (error) {
console.log("CR-API error : ", error)
return error;
}
}

function generateHtmlTableFromWorksheet(worksheet) {
Expand Down

0 comments on commit ee810fa

Please sign in to comment.