-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (67 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
const dotenv = require("dotenv");
const reddit = require("./reddit");
dotenv.config();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
let postReply = async (msg, subreddit) => {
let embedObject = await reddit.getPost(subreddit);
if (embedObject.error) {
msg.reply(
"Sorry, either the subreddit is not accessible or an internal server error."
);
return;
}
if (embedObject.over_18) {
msg.reply("Not allowed here.");
return;
}
const embed = new Discord.MessageEmbed();
embed.setTitle(embedObject.title);
embed.setFooter(embedObject.sub);
embed.setURL(embedObject.link);
embed.setTimestamp();
embed.setColor("#ff4500");
if (
embedObject.thumbnail != "self" &&
embedObject.thumbnail != "" &&
embedObject.thumbnail != null &&
embedObject.thumbnail != "default"
) {
embed.setThumbnail(embedObject.thumbnail);
}
if (embedObject.text != "self" && embedObject.text != "") {
if (embedObject.text.length > 4000) {
embed.setDescription(embedObject.text.slice(0, 4000) + "...");
} else {
embed.setDescription(embedObject.text);
}
}
if (embedObject.image) {
embed.setImage(embedObject.image);
}
if (embedObject.redditVideo) {
msg.reply(embedObject.redditVideo);
return;
}
msg.reply({ embeds: [embed] });
if (embedObject.video) {
msg.reply(embedObject.video);
}
};
client.on("messageCreate", (msg) => {
if (msg.content.toLowerCase().startsWith("red ")) {
let words = msg.content.toLowerCase().split(" ");
let subreddit = words[1];
(async () => {
try {
await postReply(msg, subreddit);
} catch {
msg.reply("Sorry, something went wrong.");
}
})();
}
});
client.login(process.env.DISCORD_TOKEN);