Skip to content

Commit

Permalink
Add message regex replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
mini-ninja-64 committed Dec 1, 2023
1 parent 02f2843 commit e565727
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 14 deletions.
30 changes: 30 additions & 0 deletions events/messageCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const db = require('../utils/database.js');
const arrayUtils = require('../utils/array.js');

const fetchFilters = db.prepare(
'SELECT (filter, response, regex) WHERE guild_id = (?);'
);

module.exports = {
name: 'messageCreate',
execute(message) {
const guildId = message.guildId;
const filters = fetchFilters.run(guildId);

const messages = filters
.map(({ message_filter, message_response, regex }) => {
if (regex === 1) {
const match = message_filter.match(/\/(.*)\/(.*)/);
const messageRegex = new RegExp(match[1], match[2]);
if (message.content.match(messageRegex)) {
message.content.replace(messageRegex, message_response);
}
} else {
if (message.content === message_filter) return message_response;
}
})
.filter((response) => response !== undefined);

console.log(arrayUtils.randomElement(messages));
},
};
3 changes: 3 additions & 0 deletions utils/array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
randomElement: (arr) => arr[Math.floor(Math.random() * arr.length)],
};
40 changes: 26 additions & 14 deletions utils/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,36 @@ try {

db.prepare(
`CREATE TABLE IF NOT EXISTS reminders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
guild_id TEXT NOT NULL,
message TEXT NOT NULL,
start_time INTEGER NOT NULL,
end_time INTEGER NOT NULL);`
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id TEXT NOT NULL,
channel_id TEXT NOT NULL,
guild_id TEXT NOT NULL,
message TEXT NOT NULL,
start_time INTEGER NOT NULL,
end_time INTEGER NOT NULL
);`
).run();

db.prepare(
`CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
mod_role TEXT,
role_cmds INTEGER NOT NULL DEFAULT 0,
role_blacklist TEXT,
welcome_msgs INTEGER NOT NULL DEFAULT 0,
welcome_channel_id TEXT);`
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
mod_role TEXT,
role_cmds INTEGER NOT NULL DEFAULT 0,
role_blacklist TEXT,
welcome_msgs INTEGER NOT NULL DEFAULT 0,
welcome_channel_id TEXT
);`
).run();

db.prepare(
`CREATE TABLE IF NOT EXISTS reactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
guild_id TEXT NOT NULL,
regex INTEGER NOT NULL DEFAULT 0,
message_filter TEXT NOT NULL,
message_response TEXT NOT NULL
);`
).run();

const tableInfo = db.pragma('table_info(settings)');
Expand Down

0 comments on commit e565727

Please sign in to comment.