-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
220 lines (194 loc) · 10.5 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const dotenv = require('dotenv').config();
const Discord = require('discord.js');
const Mangadex = require('mangadex-full-api');
const Database = require("@replit/database");
const fetch = require("node-fetch");
const db = new Database();
//http server to keep the replit running
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end('ok');
});
server.listen(3000);
//Create the instance of the bot
const client = new Discord.Client();
//Log message on successful Discord login
client.on('ready', () => {
console.log(`${new Date().toLocaleString()}: Logged in as ${client.user.tag}`);
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: Logged in as ${client.user.tag}`);
//client.channels.cache.get('755833627761180742').send(`No don't do that`);
});
//Handle the manual prefix.checkChapter command
client.on('message', msg => {
if (msg.content === `${process.env.PREFIX}.check`) {
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: Manual chapter update requested`);
checkChapter();
}
if (msg.content === (`${process.env.PREFIX}.extra`)) {
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: Manual chapter update requested`);
var num = msg.content.replace(/[^0-9]/g, '');
extraChapter(num);
}
if (msg.content === (`${process.env.PREFIX}.anime`)) {
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()} Manual Anime Update requested`);
checkEpisode();
}
//FAQ and QOL commands for the Horimiya discord
if(msg.content === (`${process.env.PREFIX}.art`))
{
msg.channel.send("Media channels were removed because\n1) People didn't care about most of the posted media\n2) The media people actually talked about created discussions which should have been in #spoilers or #no-spoilers");
}
if(msg.content === (`${process.env.PREFIX}.noRaws`)){
msg.channel.send('While we do not have an official rule that states you cannot post or discuss raws, leaks, or speedscans, We would appreciate if you did not post them. This "request" applies to all series, but epsecially the following: *Attack on Titan/Shingeki no Kyojin*, *Kaguya-Sama: Love is war*, *Oshi no Ko*, and *Kanojo, Okarishimasu*. We hope you understand.');
}
if(msg.content === (`${process.env.PREFIX}.epRelease`)){
msg.channel.send("Horimiya's anime is released every Saturday at 5:00PM (17:00) GMT.");
}
if (msg.content === (`${process.env.PREFIX}.when`)) {
msg.channel.send("Horimiya releases on a monthly schedule and is translated by a volunteer group.We know about the next chapter release as much as you do (nothing). The final chapter is being released ***in Japan*** on March 18th. If you want to be first to be notified, join the scanlation group and help scanlate the manga: https://tsundere.services/");
}
if(msg.content === (`${process.env.PREFIX}.credit`)){
msg.channel.send("Please say thank you to Yuki's wonderful, incredible developer, Epsev, and the writer of some of the FAQ commands, Alternis, for their work on Yuki's code. If you enjoy this bot or any of Epsev's other projects like Seiyuusauce, please make sure to buy him a coffee at https://www.buymeacoffee.com/doongs .");
}
if(msg.content === (`${process.env.PREFIX}.commonSense`)){
msg.channel.send("Please remember to use common sense when asking questions. Please follow the following logical process if you need assistance: \n 1. Am I in the right channel \n 2. Can I google my question? \n If you answered 'Yes' to the first question, and 'No' to the second, go ahead, ask your question. Otherwise please either move to the correct channel, or google your question. Thank you.");
}
if(msg.content === (`${process.env.PREFIX}.s2`)){
msg.channel.send("No, we don't know if there is a season two of Horimiya's anime. In an interview, the director implied that the ending of the Horimiya anime would be where he'd like us to think Horimiya ends. Please, if you have complaints about this, direct them to Clover Works on Twitter, not to us or our staff team.");
}
if(msg.content === (`${process.env.PREFIX}.rules`)){
msg.channel.send("You have obviously not read the rules of this server. Please check the rules channel, and while you are at it, please check the other channels in the catagory marked 'Important', Thank you!");
}
});
//uncomment this to reset the lastChapter key in the database to 0, uncomment the following line to reset the lastEpisode key in the database to 0
//db.set("lastChapter", 0).then(() => { console.log(`lastChapter set to 0`) });
//db.set("lastEpisode", 0).then(() => { console.log(`lastEpisode set to 0`) });
//Logs the bot into Mangadex and determines if a new chapter has been uploaded
function checkChapter() {
//Mangadex Login
Mangadex.agent.login(process.env.MANGADEX_USERNAME, process.env.MANGADEX_PASSWORD, false).then(async () => {
var manga = new Mangadex.Manga();
await manga.fill(process.env.MANGADEX_ID);
let chapterId = manga.chapters[0].id;
let chapter = await Mangadex.Chapter.get(chapterId);
console.log(chapter);
db.get("lastChapter").then(value => {
if (value != chapter.chapter && (chapter.language == 'GB' || chapter.language == 'en_EN' || chapter.language == 'en_US')) {
db.set("lastChapter", chapter.chapter).then(() => { console.log(`${new Date().toLocaleString()}: New chapter is Ch. ${chapter.chapter}`) });
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: New chapter is Ch. ${chapter.chapter}`);
sendMessage(chapter);
return true;
} else {
console.log(`${new Date().toLocaleString()}: Current chapter is still Ch. ${chapter.chapter}`);
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: Current chapter is still Ch. ${chapter.chapter}`);
return false;
}
});
});
}
function checkEpisode() {
//Query the Anilist API for episodes of the Horimiya anime
var query = `
query ($id: Int) {
Media (id: $id, type: ANIME) {
id
title {
romaji
english
native
}
streamingEpisodes {
title
thumbnail
url
site
}
}
}
`;
// Define our query variables and values that will be used in the query request
var variables = {
id: process.env.ANILIST_ID
};
// Define the config we'll need for our Api request
var url = 'https://graphql.anilist.co',
options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
body: JSON.stringify({
query: query,
variables: variables
})
};
// Make the HTTP Api request
fetch(url, options).then(handleResponse).then(handleData).catch(handleError);
function handleResponse(response) {
return response.json().then(function(json) {
return response.ok ? json : Promise.reject(json);
});
}
function handleData(data) {
console.log(data.data.Media.streamingEpisodes[(data.data.Media.streamingEpisodes.length - 1)])
var newestEpisode = (data.data.Media.streamingEpisodes[(data.data.Media.streamingEpisodes.length - 1)])
db.get("lastEpisode").then(value => {
if (value != newestEpisode.title) {
db.set("lastEpisode", newestEpisode.title).then(() => { console.log(`${new Date().toLocaleString()}: New episode is Ep. ${newestEpisode.title}`) });
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()} New episode is Ep. ${newestEpisode.title}`);
sendAnimeMessage(newestEpisode);
return true;
} else {
console.log(`${new Date().toLocaleString()} Current episode is still Ep. ${newestEpisode.title}`);
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()} Current episode is still Ep. ${newestEpisode.title}`);
return false;
}
});
}
function handleError(error) {
alert('Error, check console');
console.error(error);
}
}
//Check for a new chapter once on init and again every UPDATE_INTERVAL milleseconds
client.login(process.env.TOKEN).then(() => {
checkChapter();
//checkEpisode();
});
// setInterval(() => {
// client.login(process.env.TOKEN).then(() => {
// checkChapter();
// //checkEpisode();
// });
// },
// process.env.UPDATE_INTERVAL);
//Function to handle message sending on a manga update
function sendMessage(chapter) {
//Console log for
console.log(`${new Date().toLocaleString()}: Sending message for Ch. ${chapter.chapter}`);
//Sends a message to the admin bot logging channel
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: Sending message for Ch. ${chapter.chapter}`);
//Sends a message to the update channel with the chapter number, translation group, and Mangadex URL
client.channels.cache.get(process.env.DISCORD_MANGA).send(`<@&${process.env.DISCORD_MANGA_ROLE}>\nHey everyone, chapter ${chapter.chapter} is out now from ${chapter.groups[0].title}!\n${chapter.url}`);
}
function sendAnimeMessage(newestEpisode) {
//This is effectively a copy of sendMessage, just written in terms of the checkEpisode function
console.log(`${new Date().toLocaleString()}: Sending message for Ep. ${newestEpisode.title}`);
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()} Sending message for episode ${newestEpisode.title}`);
client.channels.cache.get(process.env.DISCORD_ANIME).send(`<@&${process.env.DISCORD_ANIME_ROLE}>\n${newestEpisode.title} is out now!\nGo watch it here:\n${newestEpisode.url}`);
client.channels.cache.get(process.env.DISCORD_ANIME).send(`${newestEpisode.thumbnail}`);
}
//If an extra chapter is released and it's not the latest chapter number wise, manually update for it
function extraChapter(chapterId) {
client.login()
Mangadex.agent.login(process.env.MANGADEX_USERNAME, process.env.MANGADEX_PASSWORD, false).then(async () => {
let chapter = await Mangadex.Chapter.get(chapterId);
//Console log for
console.log(`${new Date().toLocaleString()}: Sending message for Ch. ${chapter.chapter}`);
//Sends a message to the admin bot logging channel
client.channels.cache.get(process.env.DISCORD_LOG).send(`${new Date().toLocaleString()}: Sending message for Ch. ${chapter.chapter}`);
client.channels.cache.get(process.env.DISCORD_MANGA).send(`<@&${process.env.DISCORD_MANGA_ROLE}>\nHey everyone, extra chapter ${chapter.chapter} is out now from ${chapter.groups[0].title}!\n${chapter.url}`);
});
}