Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
feat(discord): add new feed-list command
Browse files Browse the repository at this point in the history
  • Loading branch information
adrcav committed Jul 24, 2021
1 parent 8a205af commit 0891f6f
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 30 deletions.
12 changes: 12 additions & 0 deletions src/core/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { Feed, FeedModel } from './models/feed';
import { connectDb } from './db';

type FeedFilter = { serverId?: string };

export default class Controller {
constructor() {
connectDb();
Expand All @@ -27,6 +29,16 @@ export default class Controller {
return result;
}

async getFilteredFeeds({ serverId = '' }: FeedFilter): Promise<Feed[]> {
let result: Feed[] = [];
try {
result = await FeedModel.find({ deleted: false, serverId });
} catch (err) {
console.log(err);
}
return result;
}

async updateChecksum(id: string, latestChecksum: string) {
let result = null;
try {
Expand Down
1 change: 1 addition & 0 deletions src/core/models/feed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Schema, model, Document, Model } from 'mongoose';

export interface Feed {
_id?: string;
channelId: string;
serverId: string;
name: string;
Expand Down
54 changes: 54 additions & 0 deletions src/discord/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Feed } from '../core/models/feed';
import { ACTIONS } from './constants';
import { ActionArgs } from './types';

const renderFeedItem = (item: Feed): string => `
:newspaper2: **${item.name}**
**ID:** ${item._id}
**Channel ID:** ${item.channelId}
**Link:** ${item.link}
`;

const addFeed = async ({
controller,
args,
message,
}: ActionArgs): Promise<string> => {
const name = args[1];
const link = args[2];

await controller.addFeed({
channelId: message.channel.id,
serverId: message.guild?.id || '',
name,
createdBy: message.author.id,
link,
activated: true,
deleted: false,
latestChecksum: '',
});

return 'Feed added :partying_face:';
};

const listFeed = async ({
controller,
message,
}: ActionArgs): Promise<string> => {
const items = await controller.getFilteredFeeds({
serverId: message.guild?.id,
});

if (!items.length) {
return `:broken_heart: List feed has empty! =(`;
}

const renders = items.map((item) => renderFeedItem(item)).join('');

return `:notepad_spiral: (${items.length}) Feeds: \n ${renders}`;
};

export const actions = {
[ACTIONS.ADD]: addFeed,
[ACTIONS.LIST]: listFeed,
};
4 changes: 4 additions & 0 deletions src/discord/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ACTIONS = {
ADD: 'feed-add',
LIST: 'feed-list',
};
44 changes: 14 additions & 30 deletions src/discord/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
import Discord from 'discord.js';
import { CoreController } from '../core/controller';

type FeedMessage = {
channelId: string;
title: string;
description: string;
timestamp: string;
url: string;
author: {
name: string;
url: string;
};
};
import { CoreController } from '../core/controller';
import { FeedMessage } from './types';
import { actions } from './actions';

export class DiscordBot {
client: Discord.Client;
Expand Down Expand Up @@ -68,24 +59,17 @@ export class DiscordBot {

async handleMessage(args: string[], message: Discord.Message) {
try {
switch (args[0]) {
case 'feed-add':
await this.controller.addFeed({
channelId: message.channel.id,
serverId: message.guild?.id || '',
name: args[1],
createdBy: message.author.id,
link: args[2],
activated: true,
deleted: false,
latestChecksum: '',
});
await this.sendSimpleMessage(
'Feed added :partying_face:',
message.channel.id,
);
default:
break;
const command = args[0] || '';

if (actions.hasOwnProperty(command)) {
const response = await actions[command]({
controller: this.controller,
args,
message,
handleSendMessage: this.sendSimpleMessage,
});

await this.sendSimpleMessage(response, message.channel.id);
}
} catch (err) {
console.log(err);
Expand Down
22 changes: 22 additions & 0 deletions src/discord/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Message } from 'discord.js';

import { CoreController } from '../core/controller';

export type ActionArgs = {
controller: CoreController;
args: string[];
message: Message;
handleSendMessage: (content: string, channelId: string) => void;
};

export type FeedMessage = {
channelId: string;
title: string;
description: string;
timestamp: string;
url: string;
author: {
name: string;
url: string;
};
};

0 comments on commit 0891f6f

Please sign in to comment.