This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
211 lines (161 loc) · 7.71 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
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
import * as dotenv from 'dotenv';
dotenv.config();
// import { SaveData, CheckIfExists, CheckIfExistsQ } from './moralis.js';
import { sendWelcomeEmail, sendRepeatEmail } from '../functions/email.js';
import { createUser, createOrUpdateUser, getUser, } from '../functions/utils.js';
import { createGig } from '../functions/gig.js';
import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages] });
// Getting "numberOfMsgs" messages from the channel "channelID"
async function getMessages(channelID, numberOfMsgs, channelName) {
const reciever = client.channels.cache.get(channelID);
const sender = client.channels.cache.get("1011267714800693348");
if (channelName === 'intro') {
reciever.messages.fetch({ limit: numberOfMsgs }).then(messages => {
console.log(`Received ${messages.size} messages`);
messages.forEach(async (message) => {
// Parsing JSON
var recievedData = message.content;
try {
var object = JSON.parse(recievedData);
// Getting attributes
const nameOfDeveloper = object.name;
const emailOfDeveloper = object.email;
const languages = object.Languages;
const links = object.Links;
const associations = [];
associations.push(message.guild.name);
// Logging for debugging
console.log(message.content);
console.log(nameOfDeveloper);
console.log(emailOfDeveloper);
console.log(languages);
console.log(links);
// Cloud function calls
const preExisting = await getUser(emailOfDeveloper);
console.log(preExisting+" preexisting");
if (preExisting === null) {
console.log("AfdasfAS");
createUser(emailOfDeveloper, nameOfDeveloper, languages, links, associations);
sendWelcomeEmail(nameOfDeveloper, emailOfDeveloper);
} else {
if (nameOfDeveloper === undefined) {
nameOfDeveloper = null;
}
if (emailOfDeveloper === undefined) {
emailOfDeveloper = null;
}
if (languages === undefined) {
languages = null;
}
if (links === undefined) {
links = null;
}
if (associations.length === 0) {
associations = null;
}
await createOrUpdateUser(emailOfDeveloper, nameOfDeveloper, languages, links, associations);
var user = await getUser(emailOfDeveloper);
var allAssociations = user.get("associations");
sendRepeatEmail(nameOfDeveloper, emailOfDeveloper, allAssociations);
}
} catch (err) {
console.log(err);
message.content = "Invalid JSON";
reciever.send(message);
return;
}
message.content = "<@" + message.author.id + ">"+" You will recieve an email if you have been successfully registered!";
reciever.send(message);
})
});
} else if (channelName === 'listings') {
reciever.messages.fetch({ limit: numberOfMsgs }).then(messages => {
console.log(`Received ${messages.size} messages`);
messages.forEach(message => {
// Parsing JSON
var recievedData = message.content;
try {
var object = JSON.parse(recievedData);
// Getting attributes
const title = object.title;
const description = object.description;
const bounty = object.bounty;
const time = object.time;
const completed = object.completed;
const category = object.category;
const issuedBy = message.guild.name;
// Logging for debugging
console.log(message.content);
console.log(title);
console.log(description);
console.log(bounty);
console.log(time);
console.log(completed);
console.log(category);
var gigMessage = "Title - " + title + "\nDescription - " + description + "\nBounty - " + bounty + "\nTime - " + time + "\nIssued By - " + issuedBy + "\nCategory - " + category + "\nCompleted - " + completed;
message.content = gigMessage;
createGig(title, description, bounty, time, issuedBy, category);
} catch (err) {
console.log(err);
message.content = "Invalid JSON";
reciever.send(message);
}
sender.send(message);
})
});
}
}
// Getting "numberOfMsgs" messages from the channel "channelID"
function checkIntroQueue(introChannels, numberOfMsgs) {
for (var i = 0; i < introChannels.length; i++) {
const reciever = client.channels.cache.get(introChannels[i]);
// const sender = client.channels.cache.get("1008046869030654032");
reciever.messages.fetch({ limit: numberOfMsgs }).then(messages => {
console.log(`Received ${messages.size} messages`);
messages.forEach(message => {
// Parsing JSON
var recievedData = message.content;
try {
var object = JSON.parse(recievedData);
// Getting attributes
const nameOfDeveloper = object.name;
const emailOfDeveloper = object.email;
const languages = object.Languages;
const links = object.Links;
const associations = [];
associations.push(message.guild.name);
// Logging for debugging
console.log(message.content);
console.log(nameOfDeveloper);
console.log(emailOfDeveloper);
console.log(languages);
console.log(links);
const newUser = CheckIfExistsQ(nameOfDeveloper, emailOfDeveloper, languages, links, associations);
} catch (err) {
console.log(err);
message.content = "Invalid JSON";
}
})
});
}
}
// Function triggered when the bot becomes active
client.on('ready', () => {
console.log(client.user.tag);
var introChannels = ["1008046792501366895", "1008046743822286858", "1008046685580181574"];
// checkIntroQueue(introChannels, 30);
// var listingChannels = [];
// checkListingQueue(listingChannels, 30);
});
// Function triggered when bot recieves any message
client.on("messageCreate", async(msg) => {
if (msg.author.bot) return;
var channelName = msg.channel.name;
if (channelName === 'intro' || channelName === 'listings') {
// Method to fetch 1 message and send to our server/channel
getMessages(msg.channel.id, 1, channelName);
}
});
// Starting the bot
client.login(process.env.TOKEN);