Skip to content

Commit 1b11523

Browse files
committed
fix #13: Add a function to send the blog link or latest article
1 parent 143af2d commit 1b11523

File tree

5 files changed

+61
-3
lines changed

5 files changed

+61
-3
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
.env
3-
dist
3+
dist
4+
package-lock.json

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"discord.js": "^12.2.0",
6060
"dotenv": "^8.2.0",
6161
"express": "^4.17.1",
62+
"feed-read": "0.0.1",
6263
"lodash": "^4.17.15",
6364
"mongoose": "^5.9.15",
6465
"multer": "^1.4.2",

src/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
"codeBlock": "```",
44
"avatarUrl": "https://cdn.discordapp.com/avatars",
55
"avatarExtension": "png",
6-
"checkUrl": "([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?"
6+
"checkUrl": "([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?",
7+
"siteUrl": "https://blog.fimbacode.org"
78
}

src/functions/messages.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
import { Message } from "discord.js"
22
import replyToUsers from "../helpers/reply-to-users"
3-
import { prefix, avatarUrl, avatarExtension, checkUrl } from "../config.json"
3+
import { prefix, avatarUrl, avatarExtension, checkUrl, siteUrl } from "../config.json"
44
import Members from "../helpers/MembersHelper"
55
import Points from "../constants/points"
6+
import replyWithLink from "./reply-with-link"
67

78
const isCodeBlock = "(`{3})([a-zA-Z0-9_ ])*(`{3})"
89

910
const messages = async (message: Message) => {
1011
{
12+
13+
// ====== Send Blog Link =====
14+
if (message.content === "!blog") {
15+
16+
message.channel.send('Aqui está o link do nosso blog: ' + siteUrl)
17+
18+
}
19+
// ====== Send Last post Link =====
20+
if (message.content === "!blog:last") {
21+
22+
message.channel.send(replyWithLink(message.content));
23+
}
24+
1125
// ===== GAMIFY ======
1226
// URL
1327
if (new RegExp(checkUrl).test(message.content)) {
@@ -31,6 +45,7 @@ const messages = async (message: Message) => {
3145
// ===== BOT REPLIES =====
3246
if (!message.content.startsWith(prefix) || message.author.bot) return
3347
message.reply(replyToUsers(message.content.slice(prefix.length).trim()))
48+
3449
}
3550
}
3651

src/functions/reply-with-link.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { feed } from "feed-read"
2+
3+
const rssFeed = "https://blog.fimbacode.org/feed.xml"
4+
5+
const replyWithLink = (messageContent: string) => {
6+
7+
if (messageContent.includes("!blog:last")) {
8+
9+
const list = []
10+
11+
try {
12+
13+
feed(rssFeed, function(err, items) {
14+
15+
//console.log(items);
16+
17+
items.forEach(item => {
18+
19+
const links = item;
20+
21+
list.push(links.link)
22+
23+
const link = list[0]
24+
25+
return link
26+
27+
})
28+
29+
})
30+
31+
} catch (error) {
32+
//console.log("Something went wrong when fetching the message: ", error)
33+
// Return as `reaction.message.author` may be undefined/null
34+
return
35+
}
36+
37+
}
38+
}
39+
40+
export default replyWithLink

0 commit comments

Comments
 (0)