-
Notifications
You must be signed in to change notification settings - Fork 15
/
bot.js
68 lines (54 loc) · 2.35 KB
/
bot.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
const fs = require('fs')
const Discord = require('discord.js')
const auth = require('./authentication.js')
const connectionHandler = require('./connectionHandler.js')
const cachelength = 100 // Length of message history
const msghistory = {}
const client = new Discord.Client({ partials: ['MESSAGE'] }) // Allows me to recieve "uncached" (actually manually cached by me) message events
setInterval(function () { // TODO: See if this is needed
client.user.setActivity('for people at https://discross.cloud', { type: 'WATCHING' })
}, 20000)
// https://stackoverflow.com/questions/1967119/why-does-javascript-replace-only-first-instance-when-using-replace
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
// console.log(client.channels.array());
})
client.on('message', async function (msg) {
if (msghistory[msg.channel.id] && !(msghistory[msg.channel.id].get(msg.id))) {
msghistory[msg.channel.id].set(msg.id, msg)
if (msghistory[msg.channel.id].length > cachelength) {
msghistory[msg.channel.id] = msghistory[msg.channel.id].slice(msghistory[msg.channel.id].length - (cachelength + 1), msghistory[msg.channel.id].length) // Limit the length of the cache to 50 messages
}
}
// console.log(msghistory[msg.channel.id.toString()].length);
if (msg.content === '^connect') {
if (msg.webhookID) {
msg.reply("you're already using Discross!")
} else {
msg.author.send('Verification code:\n`' + (await auth.createVerificationCode(msg.author.id)) + '`')
msg.reply('you have been sent a direct message with your verification code.')
}
}
// TODO: Do properly
connectionHandler.sendToAll(msg.content, msg.channel.id)
})
// client.on('messageDelete
exports.startBot = async function () {
client.login(fs.readFileSync('secrets/token.txt', 'utf-8').replace('\n', ''))
}
exports.addToCache = function (msg) {
if (msghistory[msg.channel.id]) {
msghistory[msg.channel.id].set(msg.id, msg)
}
}
exports.getHistoryCached = async function (chnl) {
if (!chnl.id) {
chnl = client.channels.get(chnl)
}
if (!msghistory[chnl.id]) {
const messagearray = await chnl.messages.fetch({ limit: cachelength })
msghistory[chnl.id] = messagearray.sort((messageA, messageB) => messageA.createdTimestamp - messageB.createdTimestamp)
}
return Array.from(msghistory[chnl.id].values())
}
exports.client = client