-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (132 loc) · 5.46 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
const Discord = require('discord.js');
const client = new Discord.Client();
var fs = require('fs');
const PERSISTENT_DAILY_CHENGYU_INDEX_FILEPATH = './index.txt';
const FALLBACK_DAILY_CHENGYU_INDEX = 0;
const ONLINE_DICT_LINK_FORMAT = "https://baike.baidu.com/item/{chengyu}";
//GLOBAL VARS
const _config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
const _chengyuDict = JSON.parse(fs.readFileSync(_config.chengyuDictFilePath, 'utf8'));
var _currentDailyChengyuIndex = -1;
client.login(_config.loginToken);
//executes once client is logged in
client.once('ready', () => {
console.log(`Ready! Logged in as ${client.user.tag}`);
initDailyChengyuIndexValue();
const millisecondsInADay = 86400000;
const timeNow = new Date();
var timeToNewDailyChengyu = new Date(timeNow.getFullYear(), timeNow.getMonth(), timeNow.getDate(), _config.hourToSendDailyChengyuMessageUTC, 0, 0, 0) - timeNow;
if (timeToNewDailyChengyu < 0) {
timeToNewDailyChengyu += millisecondsInADay; // it's after 10am, try 10am tomorrow.
}
const timeout = setTimeout(function() { startDailyChengyuLoop(); }, timeToNewDailyChengyu);
});
//starts the daily chengyu loop
function startDailyChengyuLoop() {
const millisecondsInADay = 86400000;
const interval = setInterval(function() { newDailyChengyu(); }, millisecondsInADay);
}
function initDailyChengyuIndexValue() {
if (fs.existsSync(PERSISTENT_DAILY_CHENGYU_INDEX_FILEPATH)) {
_currentDailyChengyuIndex = fs.readFileSync(PERSISTENT_DAILY_CHENGYU_INDEX_FILEPATH, 'utf8');
}
if (_currentDailyChengyuIndex < 0) {
_currentDailyChengyuIndex = FALLBACK_DAILY_CHENGYU_INDEX;
}
if (_currentDailyChengyuIndex >= _chengyuDict.entries.length) {
_currentDailyChengyuIndex = FALLBACK_DAILY_CHENGYU_INDEX;
}
}
//main function that gets looped each interval
function newDailyChengyu() {
const newDailyChengyuIndex = getRandomChengyuIndex(_chengyuDict);
sendDailyChengyuMessage(buildChengyuEmbed(newDailyChengyuIndex));
_currentDailyChengyuIndex = newDailyChengyuIndex;
}
function getRandomChengyuIndex(chengyuEntries) {
return Math.floor(Math.random() * (chengyuEntries.entries.length - 1));
}
//sends the daily chengyu embed to the designated channel
function sendDailyChengyuMessage(embed) {
var messageText;
var consoleMessage;
if ('chengyuRoleId' in _config) {
//send embed with role ping
messageText = '<@&' + _config.dailyChengyuPingRoleId + '> 今天的成语来啦';
consoleMessage = 'Chengyu sent to channel with role ping';
}
else {
//send embed without role ping
messageText = '今天的成语来啦';
consoleMessage = 'Chengyu sent to channel without role ping';
}
client.channels.cache.get(_config.dailyChengyuMessageChannelId).send(messageText, { embed: embed })
.then(logInConsoleWithTime(consoleMessage))
.catch(console.error);
}
function buildChengyuEmbed(chengyuIndex) {
const currentChengyu = _chengyuDict.entries[chengyuIndex]
let chengyuEmbed = new Discord.MessageEmbed()
.setTitle(`${currentChengyu["phrase"]["zhCN"]}(${currentChengyu["phrase"]["zhHK"]})`)
.setURL(ONLINE_DICT_LINK_FORMAT.replace('{chengyu}', currentChengyu["phrase"]["zhCN"]))
.setDescription(`${currentChengyu["phrase"]["zhPY"]}\n${currentChengyu["translations"]["enGB"]}`)
.setFooter(`Source: ${_chengyuDict['citation']['apa']} ${_chengyuDict['citation']['link']}`)
.setTimestamp(new Date());
const examples = currentChengyu['examples'];
for (let i = 0; i < examples.length; i++) {
chengyuEmbed.addField(`例句 ${i+1}`, `${examples[i]['zhCN']}\n${examples[i]['enGB']}`);
}
if ('primaryColour' in _config) {
chengyuEmbed.setColor(_config.primaryColour)
} else {
chengyuEmbed.setColor(0xfd9854)
}
if ('chengyuEmoji' in _config) {
chengyuEmbed.setThumbnail('https://cdn.discordapp.com/emojis/' + _config.chengyuEmoji.id + '.png')
}
return chengyuEmbed;
}
function findIndexOfChengyu(searchTerm, chengyuDict) {
var found = -1;
for (let i = 0; i < chengyuDict['entries'].length; i++) {
const entry = chengyuDict['entries'][i];
if (searchTerm == entry['phrase']['zhCN'] || searchTerm == entry['phrase']['zhHK']) {
found = i;
break;
}
}
return found
}
function logInConsoleWithTime(string, error=false) {
const out = `${new Date().toISOString()} - ${string}`;
if (error) {
console.error(out);
} else {
console.log(out);
}
}
client.on('message', message => {
//searching for a chengyu
if (message.content.slice(0, 3) === '!cy' || message.content.slice(0, 3) === '!cy') {
const searchTerm = message.content.slice(4).trim();
const index = findIndexOfChengyu(searchTerm, _chengyuDict);
if (index != -1) {
message.channel.send("Here's that 成语 you were looking for...", { embed: buildChengyuEmbed(index) })
.then(logInConsoleWithTime(`${message.author.username} searched for ${searchTerm} and I found it!`))
.catch(console.error);
} else {
message.channel.send(`I couldn't find ${searchTerm} in my records, sorry :(`)
.then(logInConsoleWithTime(`${message.author.username} searched for ${searchTerm} but I couldn't find it :(`))
.catch(console.error);
}
}
//if someone sends a message that contains the daily chengyu
else if (
message.content.includes(_chengyuDict['entries'][_currentDailyChengyuIndex]['phrase']['zhCN'])
|| message.content.includes(_chengyuDict['entries'][_currentDailyChengyuIndex]['phrase']['zhHK'])
) {
message.react(message.guild.emojis.cache.get(_config.chengyuEmoji.key))
.then(logInConsoleWithTime(message.author.username + ' used the daily chengyu'))
.catch(console.error);
}
});