Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guildId to rssFeeds collection schema #230

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
67 changes: 67 additions & 0 deletions migrations/1650237364807-add-guildID-to-rssFeeds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const {MongoClient} = require('mongodb');
const config = require('../config');

module.exports.up = async () => {
const mongoClient = new MongoClient(config.mongodb.url, {useUnifiedTopology: true});
await mongoClient.connect();
const db = mongoClient.db(config.mongodb.databaseName);

// Update validator to add guildID property
await db.command({
collMod: 'rssFeeds',
validator: {
$jsonSchema: {
bsonType: 'object',
required: ['name', 'url', 'guildID', 'channelId', 'lastCheck'],
properties: {
name: {
bsonType: 'string',
},
url: {
bsonType: 'string',
},
guildID: {
bsonType: 'string',
},
channelId: {
bsonType: 'string',
},
lastCheck: {
bsonType: 'date',
},
},
},
},
});
};

module.exports.down = async () => {
const mongoClient = new MongoClient(config.mongodb.url, {useUnifiedTopology: true});
await mongoClient.connect();
const db = mongoClient.db(config.mongodb.databaseName);

// Drop guildID from validator
await db.command({
collMod: 'rssFeeds',
validator: {
$jsonSchema: {
bsonType: 'object',
required: ['name', 'url', 'channelId', 'lastCheck'],
properties: {
name: {
bsonType: 'string',
},
url: {
bsonType: 'string',
},
channelId: {
bsonType: 'string',
},
lastCheck: {
bsonType: 'date',
},
},
},
},
});
};
4 changes: 3 additions & 1 deletion src/bot/commands/rss_feeds/createrssfeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const command = new Command('createrssfeed', async (message, args, context) => {

// try to get the channel
// NOTE: Although it reminds the user to make sure the channel is visible, it would still be able to find a channel regardless of its visiblity (but not read its contents)
let channel;
try {
context.client.getChannel(channelId) || await context.client.getRESTChannel(channelId);
channel = context.client.getChannel(channelId) || await context.client.getRESTChannel(channelId);
} catch (error) {
message.channel.createMessage('Couldn\'t find the channel with the specified ID. Please make sure it is visible to me and the ID is correct.').catch(() => {});
log.error(`Failed to locate channel ${escape(channelId)}:`, error);
Expand All @@ -35,6 +36,7 @@ const command = new Command('createrssfeed', async (message, args, context) => {
name: rssFeedName,
url: rssFeedUrl,
channelId,
guildId: channel.guild.id || null,
lastCheck: new Date(),
};

Expand Down
7 changes: 6 additions & 1 deletion src/bot/commands/rss_feeds/deleterssfeed.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ const command = new Command('deleterssfeed', async (message, args, context) => {
const {db} = context;
const collection = db.collection('rssFeeds');
const rssFeedName = args[0];
await collection.deleteOne({name: rssFeedName})
await collection.deleteOne({
guildId: message.guildID,
// in DMs, filtering by guild isn't enough; filter to the specific DM
channelId: message.guildID ? undefined : message.channel.id,
name: rssFeedName,
})
.then(result => {
if (result.deletedCount === 0) {
message.channel.createMessage('Could not find anything to delete. Did you spell the name of the feed correctly?').catch(() => {});
Expand Down
13 changes: 10 additions & 3 deletions src/bot/commands/rss_feeds/rssfeeds.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ import {escape} from '../../util/formatting';

const command = new Command('rssfeeds', async (message, args, {client, db}) => {
const collection = db.collection('rssFeeds');
let str = '';
const feeds = await collection.find().toArray();
if (feeds.length === 0) {

const feeds = await collection.find({
guildId: message.guildID,
// in DMs, filtering by guild isn't enough; filter to the specific DM
channelId: message.guildId ? undefined : message.channel.id,
}).toArray();

if (!feeds.length) {
message.channel.createMessage('No feeds found.').catch(() => { });
return;
}

let str = '';
for (const feed of feeds) {
const channelName = client.getChannel(feed.channelId).name;
str += `Feed: **${escape(feed.name)}**\nChannel: **${escape(channelName)}**\nURL: <${feed.url}> \n \n`;
Expand Down