-
Notifications
You must be signed in to change notification settings - Fork 2
Feature channel subscribe #7
Changes from 8 commits
79ec314
d9a209b
e63c3f5
1460f0d
9771784
1affea8
4cd7b66
31cdd7f
45d1c48
abf09eb
4fc5c18
6c42cc7
66dd7ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,3 +59,5 @@ typings/ | |
| .vscode | ||
|
|
||
| ./config/dev.json | ||
|
|
||
| db/** | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,5 +117,8 @@ | |
| "hapijs", | ||
| "StrongLoop", | ||
| "JSKongress" | ||
| ] | ||
| ], | ||
| "subscriber": { | ||
| "dbPath": "db/subscriber.db" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,24 @@ | ||
| require('dotenv').config() | ||
|
||
|
|
||
| const Event = require('events') | ||
| const Discord = require('discord.js') | ||
|
|
||
| const CommandManager = require('./src/command') | ||
| const TwitterPlugin = require('./src/plugin/twitter') | ||
| const docCmd = require('./src/commands/doc') | ||
| const helpCmd = require('./src/commands/help') | ||
| const subscriberCmd = require('./src/commands/subscriber') | ||
| const welcomeEmbed = require('./src/embeds/welcome') | ||
| const joinLeftEmbed = require('./src/embeds/join-left') | ||
|
|
||
| const CM = CommandManager.init() | ||
| CM.addCommand('doc', docCmd) | ||
| CM.addCommand('help', helpCmd) | ||
| CM.addCommand('subscribe', subscriberCmd.subscribe) | ||
| CM.addCommand('sub', subscriberCmd.subscribe) | ||
| CM.addCommand('unsubscribe', subscriberCmd.unsubscribe) | ||
| CM.addCommand('unsub', subscriberCmd.unsubscribe) | ||
| CM.addCommand('alert', subscriberCmd.alert) | ||
|
|
||
| const RM = new Event() | ||
|
|
||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| const Loki = require('lokijs') | ||
| const path = require('path') | ||
| const config = require('../../config/prod.json') | ||
|
|
||
| const dbPath = path.join(path.dirname(require.main.filename), config.subscriber.dbPath) | ||
|
|
||
| const db = new Loki(dbPath, { | ||
| autoload: true, | ||
| autosave: true, | ||
| autoloadCallback: () => { | ||
| subs = db.getCollection('subscribers') | ||
| if (subs === null) subs = db.addCollection('subscribers') | ||
| } | ||
| }) | ||
|
|
||
| let subs = null | ||
|
|
||
| module.exports.subscribe = ({ | ||
| message, | ||
| args | ||
| }) => { | ||
| if (args.length === 0) { | ||
| const subscribedChannel = subs.findOne({ | ||
| channel: message.channel.name | ||
| }) | ||
| if (subscribedChannel !== null) { | ||
| return message.channel.send(`${message.author.username} already have subscribed to the channel \`#${message.channel.name}\``) | ||
|
||
| } | ||
| subs.insert({ | ||
| userId: message.author.id, | ||
| channel: message.channel.name | ||
| }) | ||
| return message.channel.send(`${message.author.username} has subscribed to the channel \`#${message.channel.name}\``) | ||
|
||
| } | ||
| if (args[0] === 'list') { | ||
| const subscribedChannels = subs.find({ | ||
| userId: message.author.id | ||
| }) | ||
| if (subscribedChannels.length === 0) return message.channel.send('You haven\'t subscribed to a channel') | ||
|
||
| return message.channel.send(`${message.author.username} has subscribed to this channels :\n${subscribedChannels.map(a => `\`#${a.channel}\``).join('\n')}`) | ||
|
||
| } | ||
| } | ||
|
|
||
| module.exports.unsubscribe = ({ | ||
| message, | ||
| args | ||
| }) => { | ||
| if (args.length === 0) { | ||
| const subscribedChannel = subs.findOne({ | ||
| channel: message.channel.name | ||
| }) | ||
| if (subscribedChannel !== null) { | ||
| subs.remove(subscribedChannel) | ||
| return message.channel.send(`${message.author.username} has unsubscribed to the channel \`#${message.channel.name}\``) | ||
|
||
| } | ||
| return message.channel.send(`${message.author.username} hasn't subscribed to the channel \`#${message.channel.name}\``) | ||
|
||
| } | ||
| } | ||
|
|
||
| module.exports.alert = ({ | ||
| message, | ||
| args | ||
| }) => { | ||
| if (args.length === 0) { | ||
| return message.channel.send(`Please, add a message.`) | ||
| } | ||
| const subscribersOnChannel = subs.find({ | ||
| channel: message.channel.name | ||
| }) | ||
| if (subscribersOnChannel.length === 0) { | ||
| return message.channel.send(`There is no subscribers for this channel.`) | ||
|
||
| } | ||
| return message.channel.send(`${args.join(' ')} | ||
|
||
| ${subscribersOnChannel.map(sub => `<@${sub.userId}>`).join(', ')}`) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cette config devrait être en variable d'environnement.