Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

feat: init mongo #58

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env-sample
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
BOT_TOKEN=XXXXXXXXXXXXXXXX
BOT_TOKEN=XXXXXXXXXXXXXXXX
MONGO_CONNECTION_STRING=XXXXXXX
23 changes: 23 additions & 0 deletions Commands/add-blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { DatabaseService } = require('../services/database.service');
const { prefix, adminRole } = require('../config.json');

module.exports = async function (message) {
const dbService = new DatabaseService();
if (!message.member.roles.cache.some((role) => role.name === adminRole)) {
message.reply('You are not authorized to do that');
}
let args = message.content.slice(prefix.length).trim().split(' ');
args.shift();
args = args.map((el) => el.toLowerCase());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

every argument is converted to lowercase, so the bot cant be joked by typing blacklisted words in caps or somehow other

const createSuccess = await dbService.addArrayToBlacklist(
message.channel.id,
args
);

if (!createSuccess) {
message.reply('Something went wrong when Creating the blacklist');
return;
}

message.reply('Added Words to blacklist successfully');
};
30 changes: 14 additions & 16 deletions Commands/command.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
const {prefix} = require('../config.json');
const { prefix } = require('../config.json');

module.exports = (client, aliases, callback) => {
if (typeof aliases === 'string') {
aliases = [aliases];
}

if(typeof aliases === 'string'){
aliases = [aliases];
}
client.on('message', (message) => {
const { content } = message;

client.on('message', message => {
const {content} = message;
aliases.forEach((alias) => {
const command = `${prefix}${alias}`;

aliases.forEach(alias => {
const command = `${prefix}${alias}`

if(content.startsWith(`${command}`) || content === command){
console.log(`Running the command ${command}`)
callback(message);
}
});

});
if (content.startsWith(`${command}`) || content === command) {
console.log(`Running the command ${command}`);
callback(message);
}
});
});
};
30 changes: 30 additions & 0 deletions Commands/config.handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { DatabaseService } = require('../services/database.service');
const { prefix, adminRole } = require('../config.json');

function handleConfiguration(client) {
const dbService = new DatabaseService();

client.on('message', async (message) => {
if (message.member.roles.cache.some((role) => role.name === adminRole))
return;
if (message.content.includes(prefix)) return;
if (message.author.bot) return;
const id = message.channel.id;
const config = await dbService.getConfiguration(id);
if (!config) return;
const blackList = config.blacklist;
if (
blackList.some((substring) =>
message.content.toLowerCase().includes(substring)
)
) {
message.delete();
const msg = await message.reply(
'You used a blacklisted word in this channel. Your message got deleted'
);
msg.delete({ timeout: 10000 });
}
});
}

module.exports = { handleConfiguration };
26 changes: 26 additions & 0 deletions Commands/get-blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { DatabaseService } = require('../services/database.service');
const Discord = require('discord.js');
const { adminRole } = require('../config.json');

module.exports = async function (message) {
const dbService = new DatabaseService();
let config = null;
if (!message.member.roles.cache.some((role) => role.name === adminRole)) {
message.reply('You are not authorized to do that');
}
try {
config = await dbService.getConfiguration(message.channel.id);
} catch (error) {
message.reply("Couldn't retrieve configuration from database");
}

if (!config) {
message.reply('No Configuration found');
}
console.log(config.blacklist);
const embed = new Discord.MessageEmbed()
.setTitle('Blacklist')
.setDescription('Blacklisted Words for this channel')
.addField('Blacklisted word', `\`${config.blacklist}\``);
message.channel.send(embed);
};
24 changes: 24 additions & 0 deletions Commands/remove-blacklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { DatabaseService } = require('../services/database.service');
const { prefix, adminRole } = require('../config.json');

module.exports = async function (message) {
const dbService = new DatabaseService();
if (!message.member.roles.cache.some((role) => role.name === adminRole)) {
message.reply('You are not authorized to do that');
}
let args = message.content.slice(prefix.length).trim().split(' ');
args.shift();
args = args.map((el) => el.toLowerCase());

const deleteSuccess = await dbService.removeArrayFromBlacklist(
message.channel.id,
args
);

if (!deleteSuccess) {
message.reply('Something went wrong when updating the blacklist');
return;
}

message.reply('Removed Words from blacklist successfully');
};
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"prefix": "cm!"
"prefix": "cm!",
"adminRole": "Moderators"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I introduced a Admin-Role to make some commands only accessible to users with a specific role. This can be adjusted

}
86 changes: 57 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,71 @@ const version = require('./Commands/version.js');
const help = require('./Commands/help.js');
const links = require('./Commands/links.js');
const restrict = require('./Commands/restrictedWords.js');
const mongoose = require('mongoose');
const { handleConfiguration } = require('./Commands/config.handler');
const addBlacklist = require('./Commands/add-blacklist');
const removeBlacklist = require('./Commands/remove-blacklist');
const getBlacklist = require('./Commands/get-blacklist');

// Up commands
async function init() {
client.on('ready', () => {
console.log(
`Classroom Monitor is currently running on version v${
require('./package.json').version
}`
);

client.on('ready', () => {
//Bot Status
presence(client);

console.log(
`Classroom Monitor is currently running on version v${
require('./package.json').version
}`
);

//Bot Status
presence(client);
//Hey Command
command(client, 'hey', (message) => {
hey(message);
});

//Hey Command
command(client,'hey', message => {
hey(message);
});
//Version Command
command(client, 'version', (message) => {
version(message);
});

//Version Command
command(client,'version', message => {
version(message);
});
//Help Command
command(client, 'help', (message) => {
message.channel.send(help);
});

//Help Command
command(client, 'help', message => {
message.channel.send(help);
});
//Links Command
command(client, 'links', (message) => {
message.channel.send(links);
});

//Links Command
command(client, 'links', message => {
message.channel.send(links);
});
command(client, 'add-blacklist', (message) => {
addBlacklist(message);
});

restrict(client, message => {});
});
command(client, 'remove-blacklist', (message) => {
removeBlacklist(message);
});

// Authentications
command(client, 'get-blacklist', (message) => {
getBlacklist(message);
});

client.login(process.env.BOT_TOKEN);
handleConfiguration(client);

restrict(client, (message) => {});
});

try {
await mongoose.connect(process.env.MONGO_CONNECTION_STRING, {
useNewUrlParser: true,
});
console.log('Connected to MongoDB');
} catch (error) {
throw new Error('Couldnt connect to database');
}

await client.login(process.env.BOT_TOKEN);
}

init();
Loading