Skip to content
This repository was archived by the owner on Jun 12, 2020. It is now read-only.

Commit 81a3e75

Browse files
authored
Merge pull request #7 from Westixy-FORK/feature_channel-subscribe
Feature channel subscribe
2 parents ca0de78 + 66dd7ff commit 81a3e75

File tree

7 files changed

+104
-4
lines changed

7 files changed

+104
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ typings/
5959
.vscode
6060

6161
./config/dev.json
62+
63+
db/**

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ TWITTER_CONSUMER_KEY=
1212
TWITTER_CONSUMER_SECRET=
1313
TWITTER_ACCESS_TOKEN=
1414
TWITTER_ACCESS_TOKEN_SECRET=
15+
LOKI_DB_PATH=
1516
```
1617

1718
## Commands available

config/prod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,4 @@
118118
"StrongLoop",
119119
"JSKongress"
120120
]
121-
}
121+
}

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ const CommandManager = require('./src/command')
55
const TwitterPlugin = require('./src/plugin/twitter')
66
const docCmd = require('./src/commands/doc')
77
const helpCmd = require('./src/commands/help')
8+
const subscriberCmd = require('./src/commands/subscriber')
89
const welcomeEmbed = require('./src/embeds/welcome')
910
const joinLeftEmbed = require('./src/embeds/join-left')
1011

1112
const CM = CommandManager.init()
1213
CM.addCommand('doc', docCmd)
1314
CM.addCommand('help', helpCmd)
15+
CM.addCommand('subscribe', subscriberCmd.subscribe)
16+
CM.addCommand('sub', subscriberCmd.subscribe)
17+
CM.addCommand('unsubscribe', subscriberCmd.unsubscribe)
18+
CM.addCommand('unsub', subscriberCmd.unsubscribe)
19+
CM.addCommand('alert', subscriberCmd.alert)
1420

1521
const RM = new Event()
1622

package-lock.json

Lines changed: 8 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"dependencies": {
1313
"@sindresorhus/is": "^0.9.0",
1414
"discord.js": "^11.3.2",
15+
"lokijs": "^1.5.5",
1516
"twit": "^2.2.9"
1617
},
1718
"devDependencies": {

src/commands/subscriber.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const Loki = require('lokijs')
2+
3+
const dbPath = process.env.LOKI_DB_PATH
4+
5+
const db = new Loki(dbPath, {
6+
autoload: true,
7+
autosave: true,
8+
autoloadCallback: () => {
9+
subs = db.getCollection('subscribers')
10+
if (subs === null) subs = db.addCollection('subscribers')
11+
}
12+
})
13+
14+
let subs = null
15+
16+
module.exports.subscribe = ({
17+
message,
18+
args
19+
}) => {
20+
if (args.length === 0) {
21+
const subscribedChannel = subs.findOne({
22+
userId: message.author.id,
23+
channel: message.channel.name
24+
})
25+
if (subscribedChannel !== null) {
26+
message.author.send(`Vous êtes déjà abonné au channel \`#${message.channel.name}\``)
27+
return message.delete()
28+
}
29+
subs.insert({
30+
userId: message.author.id,
31+
channel: message.channel.name
32+
})
33+
message.author.send(`Vous vous êtes abonné au channel \`#${message.channel.name}\``)
34+
return message.delete()
35+
}
36+
if (args[0] === 'list') {
37+
const subscribedChannels = subs.find({
38+
userId: message.author.id
39+
})
40+
if (subscribedChannels.length === 0) {
41+
message.author.send(`Vous n'êtes abonné à aucun channel`)
42+
return message.delete()
43+
}
44+
message.author.send(`Vous êtes abonnés aux channels suivants :\n${subscribedChannels.map(a => `\`#${a.channel}\``).join('\n')}`)
45+
return message.delete()
46+
}
47+
}
48+
49+
module.exports.unsubscribe = ({
50+
message,
51+
args
52+
}) => {
53+
if (args.length === 0) {
54+
const subscribedChannel = subs.findOne({
55+
userId: message.author.id,
56+
channel: message.channel.name
57+
})
58+
if (subscribedChannel !== null) {
59+
subs.remove(subscribedChannel)
60+
message.author.send(`Vous vous êtes désabonné au channel \`#${message.channel.name}\``)
61+
return message.delete()
62+
}
63+
message.author.send(`Vous n'êtes pas abonné au channel \`#${message.channel.name}\``)
64+
return message.delete()
65+
}
66+
}
67+
68+
module.exports.alert = ({
69+
message,
70+
args
71+
}) => {
72+
if (args.length === 0) {
73+
message.author.send(`Veuillez mettre un message à votre alert : \`!alert <message>\` `)
74+
return message.delete()
75+
}
76+
const subscribersOnChannel = subs.find({
77+
channel: message.channel.name
78+
})
79+
if (subscribersOnChannel.length === 0) {
80+
message.channel.send(`${args.join(' ')}\nIl n'y a pas d'abonné à ce channel...`)
81+
return message.delete()
82+
}
83+
message.channel.send(`${args.join(' ')}\n${subscribersOnChannel.map(sub => `<@${sub.userId}>`).join(', ')}`)
84+
return message.delete()
85+
}

0 commit comments

Comments
 (0)