This repository has been archived by the owner on Feb 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (149 loc) · 6.52 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
const fs = require('node:fs');
const path = require('node:path');
const { Client, GatewayIntentBits, Partials, ActivityType } = require('discord.js');
const { clientId, token, allowedChannels, readChannels } = require('./config.json');
const { system_message } = require('./system_messages.js')
const Markov = require('js-markov');
const bannedList = require('./banned.json')
const cleverbot = require("cleverbot-free");
let cooldownPeeps = new Array();
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.DirectMessages],
partials: [Partials.Message, Partials.Channel],
});
client.once('ready', async() => {
//check if needed files exist. if not, create them
if(!fs.existsSync("./markov.txt")) {
fs.writeFileSync("./markov.txt", "Hello, World!\n")
}
if(!fs.existsSync("./canUseFullSentience.json")) {
fs.writeFileSync("./canUseFullSentience.json", "[]")
}
if(!fs.existsSync("./convos")) {
fs.mkdirSync("./convos")
fs.mkdirSync("./convos/threads")
fs.writeFileSync("./convos/threads/list.json", "[]")
}
console.log(`✅ Signed in as ${client.user.tag}! \n`);
client.user.setActivity('for your messages!', { type: ActivityType.Watching });
});
//Message Commands
/***
* TODO: MOVE THESE TO SEPARATE FILES!!!
*/
client.on('messageCreate', async message => {
if (message.author.bot) return;
if (bannedList.includes(message.author.id) || cooldownPeeps.includes(message.author.id)) return;
//check if sentence contains @everyone or @here
if (message.content.includes('@everyone') || message.content.includes('@here')) return;
//DM function
if(message.channel.type === 1){
if(!fs.existsSync(`./convos/${message.author.id}.json`)){
//user does not have DM history with the bot
if (message.content == "--understood-and-agreed") { //user agrees to cleverbot rules
fs.writeFileSync(`./convos/${message.author.id}.json`, "[]")
message.reply(system_message('dm_can_use'))
} else {
message.reply(system_message('welcome_dm')) //tell user about cleverbot rules
}
} else {
//user DOES have history
if (message.content == "--reset-convo") {
fs.writeFileSync(`./convos/${message.author.id}.json`, "[]")
message.reply(system_message('dm_reset'))
} else if (message.content == "--export-convo") {
message.reply(system_message('dm_export'))
message.author.send({files: [`./convos/${message.author.id}.json`]})
} else {
let jsondata = fs.readFileSync(`./convos/${message.author.id}.json`)
jsondata = JSON.parse(jsondata)
let mymessage = message.content
let resp;
await cleverbot(mymessage, jsondata).then(response =>
resp = response
);
await message.author.send(resp)
await jsondata.push(mymessage)
await jsondata.push(resp)
jsondata = JSON.stringify(jsondata)
fs.writeFileSync(`./convos/${message.author.id}.json`, jsondata)
}
}
}
//Threads
let threadlist = JSON.parse(fs.readFileSync("./convos/threads/list.json"))
if(threadlist.includes(message.channel.id)){
return await require('./src/thread.js').threads(cleverbot, message)
}
//Text Channel Functions
if(message.content.startsWith(`<@${clientId}>`) && allowedChannels.includes(message.channel.id)) {
if (message.content.includes("--help")) {
message.reply(system_message("gen_help"))
} else if (message.content.includes("--thread")) { //Threads creation
await require('./src/thread.js').createThread(message, clientId)
} else if (message.content.includes("--understood-and-agreed")) { //Agree to Cleverbot's rules
//load agreed users array
let jsondata = fs.readFileSync("./canUseFullSentience.json")
jsondata = JSON.parse(jsondata)
if(!jsondata.includes(message.author.id)) { //if the user isn't in the array
message.reply(system_message(`txt_agreed`)) //tell them that they can use it now
//add them to the array and write it.
jsondata.push(message.author.id)
jsondata = JSON.stringify(jsondata)
fs.writeFileSync("./canUseFullSentience.json", jsondata)
} else { //if the user is in the array
message.reply(system_message(`txt_already_agreed`))
}
} else if (message.content.includes("--full-sentience")) { //Cleverbot
//load the array of people who can use full-sentience
let jsondata = fs.readFileSync("./canUseFullSentience.json")
jsondata = JSON.parse(jsondata)
if(jsondata.includes(message.author.id)) { //if they can use it
let mymessage = message.content.replace(`<@${clientId}>`, "") //remove the mention
mymessage = mymessage.replace("--full-sentience", "") //remove the --full-sentience
//get cleverbot reply
cleverbot(mymessage).then(response => message.reply(response));
} else {
//tell the user they need to register
message.reply(system_message('welcome_txt'))
}
} else { //Markov function
fs.readFile('markov.txt', function(err, data) {
var markov = new Markov();
let arr = new Array();
if(err) throw err;
const parr = data.toString().replace(/\r\n/g,'\n').split('\n');
for(let i of parr) {if(i.length > 0) arr.push(i);}
markov.addStates(arr);
markov.train();
var txt = markov.generateRandom(1500);
if(txt.length < 1) {message.channel.send("_ _");} else {message.reply(txt)}
});
}
}
//Message logging from the server
if (readChannels.includes(message.channel.id)) {
let msg = message.content
const allMessage = fs.readFileSync('markov.txt', 'utf8');
if (allMessage.includes(msg)) {
return;
} else {
fs.appendFileSync('./markov.txt', `${msg}\n`)
}
}
})
//login to discord
client.login(token);
//error handlrz
process.on('uncaughtException', (error, origin) => {
console.log('----- Uncaught exception -----')
console.log(error)
console.log('----- Exception origin -----')
console.log(origin)
})
process.on('unhandledRejection', (reason, promise) => {
console.log('----- Unhandled Rejection at -----')
console.log(promise)
console.log('----- Reason -----')
console.log(reason)
})