Skip to content

Commit

Permalink
Re-add the file scanner for ROM files
Browse files Browse the repository at this point in the history
  • Loading branch information
LegendaryLinux committed Feb 16, 2023
1 parent e1f707b commit d400619
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
27 changes: 24 additions & 3 deletions bot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { Client, Events, GatewayIntentBits, Partials } = require('discord.js');
const config = require('./config.json');
const { cachePartial } = require('./lib');
const { generalErrorHandler } = require('./errorHandlers');
const fs = require('fs');

Expand All @@ -8,11 +9,11 @@ process.on('uncaughtException', (err) => generalErrorHandler(err));
process.on('unhandledRejection', (err) => generalErrorHandler(err));

const client = new Client({
partials: [ Partials.GuildMember, Partials.Message, Partials.Reaction ],
partials: [ Partials.GuildMember, Partials.Message ],
intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages,
GatewayIntentBits.GuildMembers ],
GatewayIntentBits.GuildMembers, GatewayIntentBits.MessageContent],
});
client.devMode = process.argv[2] && process.argv[2] === 'dev';
client.messageListeners = [];
client.slashCommandCategories = [];

client.tempData = {
Expand All @@ -25,8 +26,28 @@ fs.readdirSync('./slashCommandCategories').filter((file) => file.endsWith('.js')
client.slashCommandCategories.push(slashCommandCategory);
});

// Run messages through the listeners
client.on(Events.MessageCreate, async (msg) => {
// Fetch message if partial
const message = await cachePartial(msg);
if (message.member) { message.member = await cachePartial(message.member); }
if (message.author) { message.author = await cachePartial(message.author); }

// Ignore all bot messages
if (message.author.bot) { return; }

// Run the message through the message listeners
return client.messageListeners.forEach((listener) => listener(client, message));
});

// Run the interactions through the interactionListeners
client.on(Events.InteractionCreate, async(interaction) => {
// Load message listener files
fs.readdirSync('./messageListeners').filter((file) => file.endsWith('.js')).forEach((listenerFile) => {
const listener = require(`./messageListeners/${listenerFile}`);
client.messageListeners.push(listener);
});

// Handle slash command interactions independently of other interactions
if (interaction.isChatInputCommand()) {
for (const listener of client.slashCommandCategories.commands) {
Expand Down
30 changes: 30 additions & 0 deletions messageListeners/fileScanner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {generalErrorHandler} = require('../errorHandlers');

const romExtensions = ['sfc', 'smc', 'rom', 'nes', 'z64', 'n64', 'gb', 'gbc', 'gba'];

const isRomFile = (filename) => {
const parts = filename.split('.');
for (const part of parts) {
// Rom extension is present in filename
if (romExtensions.indexOf(part) !== -1) { return true; }
}
// Doesn't look like a ROM file
return false;
};

module.exports = (client, message) => {
try{
return message.attachments.each((attachment) => {
// Disallow direct posting of ROM files
if (isRomFile(attachment.name)) {
message.channel.send(`${message.author}: Do not post links to ROMs or other copyrighted content.`);
return message.delete();
}
});
} catch (error) {
message.channel.send('Something went wrong while trying to analyze your file. It has been deleted ' +
'for safety purposes.');
message.delete();
generalErrorHandler(error);
}
};

0 comments on commit d400619

Please sign in to comment.