Merge pull request 'Log errors to a channel and also fix/improve error handling' (#16) from log-errors-to-channel into master
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #16
This commit is contained in:
itzaname 2024-04-26 00:15:00 +00:00
commit e27d6e5b70
7 changed files with 95 additions and 52 deletions

41
bot.js
View File

@ -1,11 +1,12 @@
const fs = require('node:fs'); const fs = require('node:fs');
const { Client, Collection } = require("discord.js"); const { Client, Collection, GatewayIntentBits, ActivityType } = require("discord.js");
const { token } = require("./config/config.json"); const { token } = require("./config/config.json");
const { logChannelId } = require("./config/config.js");
const client = new Client({intents: ["Guilds"]}); const client = new Client({intents: [GatewayIntentBits.Guilds]});
client.commands = new Collection(); client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); const commandFiles = fs.readdirSync("./commands").filter(file => file.endsWith(".js"));
for (const file of commandFiles) { for (const file of commandFiles) {
const command = require(`./commands/${file}`); const command = require(`./commands/${file}`);
@ -14,7 +15,7 @@ for (const file of commandFiles) {
client.commands.set(command.data.name, command); client.commands.set(command.data.name, command);
} }
client.on('interactionCreate', async interaction => { client.on("interactionCreate", async interaction => {
if (!interaction.isCommand()) return; if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName); const command = client.commands.get(interaction.commandName);
@ -22,22 +23,48 @@ client.on('interactionCreate', async interaction => {
if (!command) return; if (!command) return;
try { try {
// Show "Bot is thinking..."
await interaction.deferReply();
await command.execute(interaction); await command.execute(interaction);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); if (interaction.replied) {
await interaction.followUp("An unknown error occured while performing this command!");
}
else {
await interaction.editReply("An unknown error occured while performing this command!");
}
await logError(error, interaction);
} }
}); });
client.on("error", async _error => { /**
* @param {Error} error
* @param {import('discord.js').ChatInputCommandInteraction} interaction
*/
async function logError(error, interaction) {
const logChannel = client.channels.cache.get(logChannelId);
if (!logChannel) {
console.log("Couldn't find log channel with the given id: " + logChannelId);
return;
}
const message = await interaction.fetchReply();
let errMsg = `An error occured when performing \`/${interaction.commandName}\` (${interaction.options.data.map((option) => `${option.name}: \`${option.value}\``).join(", ")}) at ${message.url}\n`;
errMsg += `\`\`\`\n${error.stack ? error.stack : error}\n\`\`\``;
await logChannel.send(errMsg);
}
client.on("error", async error => {
console.error(error);
}); });
client.once("ready", () => { client.once("ready", () => {
console.log("Ready"); console.log("Ready");
client.user.setActivity({ client.user.setActivity({
name: "use /take and /submit", name: "use /take and /submit",
type: 0 type: ActivityType.Custom
}); });
}); });

View File

@ -15,12 +15,12 @@ async function execute(interaction) {
const fname = submissions[game]; const fname = submissions[game];
if (fname === undefined) { if (fname === undefined) {
await interaction.reply({content: "Invalid game specified!", ephemeral: true}); await interaction.editReply("🚫 Invalid game specified!");
return; return;
} }
if (!fs.existsSync(fname)) { if (!fs.existsSync(fname)) {
await interaction.reply(`No submissions exist yet for ${game}.`); await interaction.editReply(`No submissions exist yet for ${game}.`);
return; return;
} }
@ -33,7 +33,7 @@ async function execute(interaction) {
if (afterDateStr) { if (afterDateStr) {
const afterDate = Sugar.Date.create(afterDateStr); const afterDate = Sugar.Date.create(afterDateStr);
if (isNaN(afterDate)) { if (isNaN(afterDate)) {
await interaction.reply({content: `Could not convert '${afterDateStr}' to a valid date.`, ephemeral: true}); await interaction.editReply(`🚫 Could not convert '${afterDateStr}' to a valid date.`);
return; return;
} }
afterTimestamp = Math.round(afterDate / 1000); afterTimestamp = Math.round(afterDate / 1000);
@ -42,14 +42,14 @@ async function execute(interaction) {
if (beforeDateStr) { if (beforeDateStr) {
const beforeDate = Sugar.Date.create(beforeDateStr); const beforeDate = Sugar.Date.create(beforeDateStr);
if (isNaN(beforeDate)) { if (isNaN(beforeDate)) {
await interaction.reply({content: `Could not convert '${beforeDateStr}' to a valid date.`, ephemeral: true}); await interaction.editReply(`🚫 Could not convert '${beforeDateStr}' to a valid date.`);
return; return;
} }
beforeTimestamp = Math.round(beforeDate / 1000); beforeTimestamp = Math.round(beforeDate / 1000);
} }
if (!isNaN(afterTimestamp) && !isNaN(beforeTimestamp) && beforeTimestamp < afterTimestamp) { if (!isNaN(afterTimestamp) && !isNaN(beforeTimestamp) && beforeTimestamp < afterTimestamp) {
await interaction.reply({content: `Your date range is invalid: ${getDateRangeString(afterTimestamp, beforeTimestamp)}.`, ephemeral: true}); await interaction.editReply(`🚫 Your date range is invalid: ${getDateRangeString(afterTimestamp, beforeTimestamp)}.`);
return; return;
} }
@ -82,17 +82,17 @@ async function execute(interaction) {
else { else {
msg = "Could not find any submissions."; msg = "Could not find any submissions.";
} }
await interaction.reply({content: msg, ephemeral: true}); await interaction.editReply(msg);
return; return;
} }
const file = new AttachmentBuilder(Buffer.from(csvString), { name: fname }); const file = new AttachmentBuilder(Buffer.from(csvString), { name: fname });
const dateRangeStr = getDateRangeString(afterTimestamp, beforeTimestamp); const dateRangeStr = getDateRangeString(afterTimestamp, beforeTimestamp);
if (dateRangeStr) { if (dateRangeStr) {
await interaction.reply({content: `Using date range ${dateRangeStr}:`, files: [file]}); await interaction.editReply({content: `Using date range ${dateRangeStr}:`, files: [file]});
} }
else { else {
await interaction.reply({files: [file]}); await interaction.editReply({files: [file]});
} }
} }

View File

@ -32,15 +32,14 @@ async function robloxUsernameFromId(id) {
async function execute(interaction) { async function execute(interaction) {
const userId = await robloxUserFromDiscord(interaction.user.id); const userId = await robloxUserFromDiscord(interaction.user.id);
if (!userId) { if (!userId) {
const msg = "You don't have a Roblox account linked with your Discord account. Use !link with the rbhop dog bot to link your account."; await interaction.editReply("🚫 You don't have a Roblox account linked with your Discord account. Use !link with the rbhop dog bot to link your account.");
await interaction.reply({content: msg, ephemeral: true});
return; return;
} }
const game = interaction.options.getString("game", true);
const game = interaction.options.getString("game", true);
const fname = submissions[game]; const fname = submissions[game];
if (fname === undefined) { if (fname === undefined) {
await interaction.reply({content: "Invalid game specified!", ephemeral: true}); await interaction.editReply("🚫 Invalid game specified!");
return; return;
} }
@ -51,43 +50,51 @@ async function execute(interaction) {
const id = interaction.options.getInteger("asset_id", true); const id = interaction.options.getInteger("asset_id", true);
await noblox.setCookie(cookies[game]); await noblox.setCookie(cookies[game]);
try {
// Check that the bot owns this model // Check that the bot owns this model
if (!(await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset"))) { if (!(await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset"))) {
const msg = `The ${game} maptest bot's inventory does not contain this asset (id: ${id}). You must use the /take command first.`; const msg = `🚫 The ${game} maptest bot's inventory does not contain this asset (id: ${id}). You must use the /take command first.`;
await interaction.reply({content: msg, ephemeral: true}); await interaction.editReply(msg);
return;
}
} catch (error) {
if (error.message !== "400 The specified Asset does not exist!") {
throw error;
}
await interaction.editReply(`🚫 This asset does not exist (id: ${id}).`);
return; return;
} }
const assetInfo = await getAssetInfo(id); const assetInfo = await getAssetInfo(id);
if (assetInfo.typeId !== AssetType.Model) {
await interaction.editReply(`🚫 This asset (id: ${id}) is not a model. Your map must be a model.`);
return;
}
if (assetInfo.creatorId !== userId) { if (assetInfo.creatorId !== userId) {
const assetUsernamePromise = robloxUsernameFromId(assetInfo.creatorId); const assetUsernamePromise = robloxUsernameFromId(assetInfo.creatorId);
const interactionUsernamePromise = robloxUsernameFromId(userId); const interactionUsernamePromise = robloxUsernameFromId(userId);
const assetUsername = await assetUsernamePromise; const assetUsername = await assetUsernamePromise;
const interactionUsername = await interactionUsernamePromise; const interactionUsername = await interactionUsernamePromise;
const msg = `The account linked to your Discord (${interactionUsername}) is not the owner of this model (${assetUsername}), so you cannot submit it.`; const msg = `🚫 The account linked to your Discord (${interactionUsername}) is not the owner of this model (${assetUsername}), so you cannot submit it.`;
await interaction.reply({content: msg, ephemeral: true}); await interaction.editReply(msg);
return;
}
// Shouldn't really be possible but who knows...
if (assetInfo.typeId !== AssetType.Model) {
await interaction.reply({content: `This asset (id: ${id}) is not a model. Your map must be a model.`, ephemeral: true});
return; return;
} }
const csvFile = fs.readFileSync(fname); const csvFile = fs.readFileSync(fname);
const lines = parse(csvFile, {delimiter: ',', fromLine: 2}); const lines = parse(csvFile, {delimiter: ',', fromLine: 2});
for (let lineStr of lines) { for (let lineStr of lines) {
const line = getSubmissionLine(lineStr); const line = getSubmissionLine(lineStr);
if (id === line.modelId) { if (id === line.modelId) {
await interaction.reply({content: `This map (id: ${id}) was already submitted on <t:${line.timestamp}:d>.`, ephemeral: true}); await interaction.editReply(`🚫 This map (id: ${id}) was already submitted on <t:${line.timestamp}:d>.`);
return; return;
} }
} }
// Show "Bot is thinking..."
await interaction.deferReply();
// Validate and send the validation result // Validate and send the validation result
const validation = await validateMapAsset(id, game); const validation = await validateMapAsset(id, game);
const msg = getValidationMessage(validation, game, true); const msg = getValidationMessage(validation, game, true);

View File

@ -10,35 +10,45 @@ async function execute(interaction) {
const game = interaction.options.getString("game", true); const game = interaction.options.getString("game", true);
const cookie = cookies[game]; const cookie = cookies[game];
if (cookie === undefined) { if (cookie === undefined) {
await interaction.reply({content: "Invalid game specified!", ephemeral: true}); await interaction.editReply("🚫 Invalid game specified!");
return; return;
} }
const id = interaction.options.getInteger("asset_id", true); const id = interaction.options.getInteger("asset_id", true);
await noblox.setCookie(cookie); await noblox.setCookie(cookie);
try {
// Check that the bot doesn't already own this asset // Check that the bot doesn't already own this asset
if (await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset")) { if (await noblox.getOwnership(await noblox.getCurrentUser("UserID"), id, "Asset")) {
const msg = `The ${game} maptest bot already has this model (id: ${id})`; const msg = `🚫 The ${game} maptest bot already has this model (id: ${id})`;
await interaction.reply({content: msg, ephemeral: true}); await interaction.editReply(msg);
return; return;
} }
} catch (error) {
if (error.message !== "400 The specified Asset does not exist!") {
throw error;
}
await interaction.editReply(`🚫 This asset does not exist (id: ${id}).`);
return;
}
// Validate that this is a model // Validate that this is a model
const assetInfo = await getAssetInfo(id); const assetInfo = await getAssetInfo(id);
if (assetInfo.status === 404) { if (assetInfo.status !== 403 && (assetInfo.status < 200 || assetInfo.status > 300)) {
await interaction.reply({content: `This asset may not exist or is not a model (id: ${id}). Your map must be a model.`, ephemeral: true}); await interaction.editReply(`🚫 This asset may not exist or is not a model (id: ${id}). Your map must be a model.`);
return; return;
} }
// 403 (Forbidden) means the asset isn't distributed // 403 (Forbidden) means the asset isn't distributed
if (assetInfo.status === 403 || !assetInfo.forSale) { if (assetInfo.status === 403 || !assetInfo.forSale) {
await interaction.reply({content: `This model (id: ${id}) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store).`, ephemeral: true}); await interaction.editReply(`🚫 This model (id: ${id}) is off sale. Please configure it to be on sale (Configure -> Distribute on Creator Store). It is also possible that this is not a valid model.`);
return; return;
} }
if (assetInfo.typeId !== AssetType.Model) { if (assetInfo.typeId !== AssetType.Model) {
await interaction.reply({content: `This asset (id: ${id}) is not a model. Your map must be a model.`, ephemeral: true}); await interaction.editReply(`🚫 This asset (id: ${id}) is not a model. Your map must be a model.`);
return; return;
} }
if (assetInfo.price !== 0) { if (assetInfo.price !== 0) {
await interaction.reply({content: `This model (id: ${id}) is not free. Please change the price to be free.`, ephemeral: true}); await interaction.editReply(`🚫 This model (id: ${id}) is not free. Please change the price to be free.`);
return; return;
} }
@ -51,9 +61,6 @@ async function execute(interaction) {
} }
}; };
// Show "Bot is thinking..."
await interaction.deferReply();
// Kick off the buy request // Kick off the buy request
const buyPromise = noblox.buy({product: productInfo, price: 0}); const buyPromise = noblox.buy({product: productInfo, price: 0});

View File

@ -29,7 +29,7 @@ async function getAssetInfo(assetId) {
params: { params: {
assetIds: assetId assetIds: assetId
}, },
validateStatus: (status) => status === 403 || status === 404 || (status >= 200 && status < 300) // Allow 403/404 as a valid status (don't throw an error) validateStatus: (_status) => true
}); });
if (res.status < 200 || res.status > 300) { if (res.status < 200 || res.status > 300) {

View File

@ -1,4 +1,4 @@
const { bhopCookie, surfCookie, deathrunCookie, flytrialsCookie } = require("./config.json"); const { bhopCookie, surfCookie, deathrunCookie, flytrialsCookie, devLogChannelId } = require("./config.json");
const cookies = { const cookies = {
bhop: bhopCookie, bhop: bhopCookie,
@ -21,4 +21,6 @@ const gamePlaces = {
flytrials: "https://www.roblox.com/games/12724901535/" flytrials: "https://www.roblox.com/games/12724901535/"
}; };
module.exports = { cookies, submissions, commands, gamePlaces }; const logChannelId = devLogChannelId ?? "1233066743023538257"; // Default is the error-logs channel in rbhop
module.exports = { cookies, submissions, commands, gamePlaces, logChannelId };

View File

@ -8,7 +8,7 @@
"discord.js": "^14.14.1", "discord.js": "^14.14.1",
"noblox.js": "^4.15.1", "noblox.js": "^4.15.1",
"node-csv": "^0.1.2", "node-csv": "^0.1.2",
"rbxm-parser": "^1.0.4", "rbxm-parser": "^1.0.6",
"sugar-date": "^2.0.6" "sugar-date": "^2.0.6"
}, },
"devDependencies": { "devDependencies": {