Skip to content
This repository was archived by the owner on Jun 12, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/
.vscode

./config/dev.json

db/**
5 changes: 4 additions & 1 deletion config/prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,8 @@
"hapijs",
"StrongLoop",
"JSKongress"
]
],
"subscriber": {
Copy link
Member

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.

"dbPath": "db/subscriber.db"
}
}
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
require('dotenv').config()
Copy link
Member

Choose a reason for hiding this comment

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

Pas nécessaire pour la prod, tu peux virer ça et ajouter les variables d'environnement dans ta commande.


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()

Expand Down
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
"dependencies": {
"@sindresorhus/is": "^0.9.0",
"discord.js": "^11.3.2",
"dotenv": "^6.0.0",
"lokijs": "^1.5.5",
"twit": "^2.2.9"
},
"devDependencies": {
Expand Down
75 changes: 75 additions & 0 deletions src/commands/subscriber.js
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}\``)
Copy link
Member

Choose a reason for hiding this comment

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

J'imagine que ce n'est pas la peine d'avertir la planète entière que le mec s'est abonné.

}
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}\``)
Copy link
Member

Choose a reason for hiding this comment

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

J'imagine que ce n'est pas la peine d'avertir la planète entière que le mec s'est abonné.

}
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')
Copy link
Member

Choose a reason for hiding this comment

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

J'imagine que ce n'est pas la peine d'avertir la planète entière que le mec ne s'est pas abonné.

return message.channel.send(`${message.author.username} has subscribed to this channels :\n${subscribedChannels.map(a => `\`#${a.channel}\``).join('\n')}`)
Copy link
Member

Choose a reason for hiding this comment

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

J'imagine que ce n'est pas la peine d'avertir la planète entière que le mec s'est abonné.

}
}

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}\``)
Copy link
Member

Choose a reason for hiding this comment

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

Idem

}
return message.channel.send(`${message.author.username} hasn't subscribed to the channel \`#${message.channel.name}\``)
Copy link
Member

Choose a reason for hiding this comment

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

Idem

}
}

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.`)
Copy link
Member

Choose a reason for hiding this comment

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

Idem

}
return message.channel.send(`${args.join(' ')}
Copy link
Member

Choose a reason for hiding this comment

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

Supprimer le message qui contient la commande avant de faire ça.

${subscribersOnChannel.map(sub => `<@${sub.userId}>`).join(', ')}`)
}