forked from Heath123/discross
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.js
64 lines (51 loc) · 2.32 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
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: [Discord.Partials.Message], shards: "auto", intents: [Discord.GatewayIntentBits.Guilds/*, Discord.GatewayIntentBits.GuildMembers*/, Discord.GatewayIntentBits.GuildMessages, Discord.GatewayIntentBits.MessageContent] })
// 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('messageCreate', 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