-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
233 lines (212 loc) · 7.35 KB
/
index.js
File metadata and controls
233 lines (212 loc) · 7.35 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const fetch = require("node-fetch");
const Discord = require('discord.js');
const client = new Discord.Client();
const Data = require("./data.json");
const version = "v6.0";
const imageChannels = [];
const videoChannels = [];
Data.servers.forEach(s => {
imageChannels.push(s.imageChannel.id);
});
Data.servers.forEach(s => {
videoChannels.push(s.videoChannel.id);
});
client.on('ready', () => { console.log(`Logged in as ${client.user.tag}!`) });
client.on('error', err => { console.log(err) });
client.on('message', msg => {
if (msg.author.bot) return;
if (msg.isMemberMentioned(client.user)) {
checkCmd(msg);
}
if (imageChannels.includes(msg.channel.id)) {
let timeWait = setTimeout(checkMsg, 1000); //wait until discord auto embeds video
function checkMsg() {
if (isImage(msg)) { // if msg is a image in the image submission channel
cleanChannel(msg, 'IMAGE');
}
}
} else if (videoChannels.includes(msg.channel.id)) {
let timeWait = setTimeout(checkMsg, 1000); //wait until discord auto embeds video
function checkMsg() {
if (isVideo(msg)) { // if msg is a video in the video submission channel
cleanChannel(msg, 'VIDEO');
}
}
}
})
function cleanChannel(msg, type, cmd) { // removes all none submission messages from the submission channel
if (type == 'VIDEO') {
msg.channel.fetchMessages({ limit: 100 }).then(messages => {
messages.forEach(message => {
if (!isVideo(message)) { // delete all non-video messages
deleteMsg(message, 0);
} else if (message.id != msg.id && message.author.id == msg.author.id && !cmd) {
deleteMsg(message, 0);
}
});
});
} else if (type == 'IMAGE') {
msg.channel.fetchMessages({ limit: 100 }).then(messages => {
messages.forEach(message => {
if (!isImage(message)) { // delete all non-image messages
deleteMsg(message, 0);
} else if (message.id != msg.id && message.author.id == msg.author.id && !cmd) {
deleteMsg(message, 0);
}
});
});
};
}
function isVideo(msg) {
if (msg.embeds[0]) {
if (msg.embeds[0].type == "video") {
return true;
}
} else if (msg.attachments.size > 0) {
let attachArray = Array.from(msg.attachments.values());
let url = attachArray[0].url.toUpperCase();
if (url.slice(url.length - 3) == "MP4" || url.slice(url.length - 3) == "MOV" || url.slice(url.length - 3) == "MKV") {
return true;
}
}
}
function isImage(msg) {
if (msg.embeds[0]) {
if (msg.embeds[0].type == 'image') {
return true;
}
} else if (msg.attachments.size > 0) {
let attachArray = Array.from(msg.attachments.values());
let url = attachArray[0].url.toUpperCase();
if (url.slice(url.length - 3) == "PNG" || url.slice(url.length - 3) == "JPG") {
return true;
}
}
}
function checkCmd(msg) {
var str = msg.content.toUpperCase();
var hi = str.search('HI');
var reset = str.search('RESET');
var clean = str.search('CLEAN');
var help = str.search('HELP');
var upload = str.search('UPLOAD');
if (hi > -1) {
sendMsg(msg, `Hello, ${msg.author}`, -1);
} else if (help > -1) {
sendMsg(msg, "Current version: " + version, 5000);
} else if (clean > -1) {
if (imageChannels.includes(msg.channel.id)) {
cleanChannel(msg, 'IMAGE', true)
} else if (videoChannels.includes(msg.channel.id)) {
cleanChannel(msg, 'VIDEO', true);
} else {
console.log(`Clean Command does not work in channel with id: ${msg.channel.id}`)
}
} else if (upload > -1 && isAdmin(msg)) {
uploadSubmissions(msg); //uploads submissions to external app
} else if (reset > -1 && isAdmin(msg)) {
if (videoChannels.includes(msg.channel.id) || imageChannels.includes(msg.channel.id)) {
deleteAll(msg);
}
}
}
function isAdmin(msg) {
if (Data.admins.find(a => a.id === msg.author.id)) {
return true
}
}
function uploadSubmissions(msg) {
console.log("attemping to upload data");
if (imageChannels.includes(msg.channel.id)) {
uploadImages(msg);
} else if (videoChannels.includes(msg.channel.id)) {
uploadVideos(msg);
} else {
console.log(`Upload Command does not work in channel with id: ${msg.channel.id}`)
}
function uploadImages(msg) {
let images = [];
msg.channel.fetchMessages({ limit: 100 }).then(messages => {
messages.forEach(message => {
if (isImage(message)) {
let image = getUrl(message);
let name = message.author.username;
images.push({ url: image, text: name });
}
});
uploadData(images);
});
}
function uploadVideos() {
let videos = [];
msg.channel.fetchMessages({ limit: 100 }).then(messages => {
messages.forEach(message => {
if (isVideo(message)) {
let image = getUrl(message);
let name = message.author.username;
images.push({ url: image, text: name });
}
});
uploadData(videos);
});
}
function getUrl(msg) {
if (msg.attachments.size > 0) {
let attachArray = Array.from(msg.attachments.values());
let url = attachArray[0].url;
return url;
}
}
function uploadData(data) {
console.log(data.length);
// uploads images to my multiplayer veiwing app.
fetch('https://bd-socketio.glitch.me/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data),
})
.then(response => response.text())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});
}
}
function deleteAll(msg) {
Data.servers.forEach(server => {
if (server.id == msg.guild.id) {
if (msg.channel.id == server.videoChannel.id || msg.channel.id == server.imageChannel.id) {
msg.channel.fetchMessages({ limit: 100 }).then(messages => {
messages.forEach(message => {
deleteMsg(message, 0);
});
});
return;
};
return;
}
})
}
function deleteMsg(msg, delay) {
Data.servers.forEach(server => {
if (server.id == msg.guild.id) {
msg.delete(delay).catch(err => {
console.log(`Might be Missing MANAGE_MESSAGES permissions to delete ${msg.author.username}'s message`);
console.log(err);
});
};
});
}
function sendMsg(msg, text, delay) {
msg.channel.send(text).then(myMsg => {
if (delay > -1) {
deleteMsg(myMsg, delay);
deleteMsg(msg, delay);
}
}).catch();
}
client.login(process.env.BOT_TOKEN);