repl.it
@pizzafox/

Trendy-source

Nodejs

No description

fork
loading
Files
  • index.js
  • commands
  • config.js
  • settings.sqlite3
  • Packager files
  • package-lock.json
  • package.json
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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
const Commando = require("discord.js-commando");
const path = require('path');
const mongo = require('mongodb').MongoClient
const sqlite = require('sqlite')
const config = require('./config')
const dblapi = require('dblapi.js')

// Making the Bot
const client = new Commando.CommandoClient({
    owner: '374886124126208000',
    commandPrefix: '=',
    unknownCommandResponse: false
})

const dbl = new dblapi(config.dblApiKey, client)

// The replies for talking

var hellorep = [
    "Hello there ",
    "Hi ",
    "Hello ",
    "Hey ",
    "Yo "
];

var byerep = [
    "Bye ",
    "Goodbye ",
    "R.I.P. ",
    ":wave: "
];

var wasuprep = [
    "I am fine, Thank You!",
    "Good, How about you?",
    "Waaazzzzuuupppppppp!!"
];

// Dealing with the commands
client.registry
    .registerGroups([
        ['fun', '==>> Fun commands'],
        ['moderation', '==>> Moderation commands'],
        ['general', '==>> General Commands'],
        ['role', '==>> Role Management'],
        ['fort', '==>> Fortnite Commands'],
        ['jl', '==>> Join-Leave Logging'],
        ['support','==>> Support']
    ])
    .registerDefaults()
    .commands.filter(command=> command.name.toLowerCase().indexOf('help') > -1).forEach(helpCommand=>{
        client.registry.unregisterCommand(helpCommand)
    })
    client.registry.registerCommandsIn(path.join(__dirname, 'commands'))


// Setting the settings provider
client.setProvider(
    sqlite.open(path.join(__dirname, 'settings.sqlite3')).then(db => new Commando.SQLiteProvider(db))
).catch(console.error);


// Connecting to database
mongo.connect(`mongodb://${config.dbUser}:${config.dbPass}@ds026658.mlab.com:26658/trendy`, {
    useNewUrlParser: true
}, function (err, db) {
    if (err) {
        console.error
    } else {
        let dbo = db.db('trendy')
        let servcol = dbo.collection('servdata')
        let usercol = dbo.collection('userdata')
        let coolcol = dbo.collection('cooldown')
        let mutecol = dbo.collection('mutes')
        let userdata;
        let servdata;
        let cooldata;
        usercol.find().toArray().then(o => {
            userdata = o;
            console.log("Userdata set!")
        })
        servcol.find().toArray().then(o => {
            servrdata = o;
            console.log("Serverdata set!")
        })
        coolcol.find().toArray().then(o => {
            cooldata = o;
            console.log("Cooldowns set!")
        })
        // After Logging in
        client.on('ready', () => {
            client.user.setActivity("=help")

            console.log(client.user.avatarURL)

            client.setInterval(() => {
                usercol.find().toArray().then(o => {
                    userdata = o;
                    console.log("Userdata set!")
                })
                servcol.find().toArray().then(o => {
                    servrdata = o;
                    console.log("Serverdata set!")
                })
                coolcol.find().toArray().then(o => {
                    cooldata = o;
                    console.log("Cooldowns set!")
                })
            }, 120000)

            // Notify the owners if u don't have the perms to send messages
            client.guilds.forEach(guild => {

                var role = guild.me.hasPermission('SEND_MESSAGES')
                if (!role) {
                    try {
                        guild.owner.send('I do not have permissions to send messages in your guild, please fix the permissions.\nThank You. :smile:')
                        console.log(`Sent request to ${guild.name}'s owner.`)
                    } catch (err) {
                        console.log(`Unable to send the request to ${guild.name}'s owner.`)
                    }
                }
            })

            client.setInterval(() => {
                mutecol.find().toArray().then(obj => {
                    obj.forEach(mute => {
                        if (Date.now() > mute.duration) {
                            let muterole = client.guilds.get(mute.server).roles.filter(rol => rol.name.toLowerCase().indexOf("muted") > -1).array()[0]
                            mutecol.deleteOne(mute, (error, result) => {
                                if (error) {
                                    console.error(error)
                                    console.log("error")
                                } else {
                                    if (muterole) {
                                        console.log(muterole.name)
                                        client.guilds.get(mute.server).members.get(mute.user).removeRole(muterole).then(mem => {
                                            mem.send(`You have been unmuted in **${mem.guild.name}**.`).catch(console.error).then(console.log("Unmuted."))
                                        })
                                    }
                                }
                            })
                        }
                    })
                })
            }, 10000)

            // Update guilds and users info
            client.setInterval(() => {

                client.guilds.forEach(guild => {

                    let coolDoc = {
                        '_id': guild.id,
                        'hicool': Date.now(),
                        'bicool': Date.now(),
                        'spcool': Date.now()
                    }
                    coolcol.insertOne(coolDoc, (err, res) => {
                        if (!err) {
                            console.log(`Registered ${guild.name}'s cooldown.`)
                        }
                    })

                    let guildDoc = {
                        '_id': guild.id,
                        'name': guild.name,
                        'talk': 'disable',
                        'defrole': 'disabled',
                        'welcome': 'Hello {user} and welcome to {server}.\nPlease enjoy your stay! :smile:',
                        'leave': 'Goodbye {user} :wave:! :cry:',
                        'channel': 'disabled'
                    }
                    servcol.insertOne(guildDoc, (err, res) => {
                        if (!err) {
                            console.log(`Registered ${guild.name}.`)
                        }
                    })
                })

                dbl.postStats(client.guilds.size)

            }, 300000)
            console.log("Logged in...")
        })

        // After receiving a message
        client.on('message', message => {
            let author = message.author.id
            if (message.author.bot) return

            // Check if the sender was afk, bring him online if yes
            if (message.guild) {
                if (userdata) {
                    let user = userdata.find(obj => obj._id == author)
                    if (user) {
                        if (user.status) {
                            if (user.status.toLowerCase() == 'afk') {
                                usercol.updateOne({
                                    '_id': author
                                }, {
                                    $set: {
                                        'status': 'online',
                                        'reas': ''
                                    }
                                }, (er, data) => {
                                    if (er)
                                        return message.reply(" I was unable to set your status to online.")
                                    else {
                                        return message.reply(" I see you've come back online")
                                    }
                                })
                                userdata[userdata.indexOf(user)].status = 'online'
                                userdata[userdata.indexOf(user)].reas = ''
                            }
                        }
                    }
                }

                // Check if any afk members were @mentioned
                if (message.mentions.members.array().length > 0) {
                    message.mentions.members.forEach(mem => {
                        usercol.findOne({
                            '_id': mem.id
                        }, (err, res) => {
                            if (err)
                                console.error
                            else {
                                if (res) {
                                    if (res.status) {
                                        if (res.status == 'afk')
                                            message.reply(` **${mem.displayName}** is afk for **${res.reas}**.`)
                                    }
                                } else {
                                    return null
                                }
                            }
                        })
                    })
                }

                // Talking

                let msg = message.content.toLowerCase()

                servcol.findOne({
                    '_id': message.guild.id
                }, (err, data) => {

                    if (!err) {
                        if (data) {
                            if (data.talk == 'enable') {
                                if (cooldata) {
                                    let servercool = cooldata.find(obj => obj._id == message.guild.id)
                                    if (servercool.hicool < Date.now())
                                        if (msg == "hi" || msg == "hello" || msg == "yo" || msg.startsWith("hello ") || msg.startsWith("hi ") || msg == client.user || msg.startsWith("hey") || msg.startsWith("helo") || msg.startsWith("yo ") && message.author != client.user)
                                            message.channel.send(hellorep[Math.floor(Math.random() * hellorep.length)] + message.member.user.username + "!").then(() => {
                                                cooldata[cooldata.indexOf(servercool)].hicool = Date.now() + 300000
                                                coolcol.updateOne({
                                                    '_id': message.guild.id
                                                }, {
                                                    $set: {
                                                        'hicool': Date.now() + 300000
                                                    }
                                                })
                                            })
                                    if (servercool.spcool < Date.now())
                                        if (msg.indexOf("wassup") > -1 || msg.indexOf("wasup") > -1 || msg.indexOf("what's up") > -1)
                                            message.channel.send(wasuprep[Math.floor(Math.random() * wasuprep.length)]).then(() => {
                                                cooldata[cooldata.indexOf(servercool)].spcool = Date.now() + 300000
                                                coolcol.updateOne({
                                                    '_id': message.guild.id
                                                }, {
                                                    $set: {
                                                        'spcool': Date.now() + 300000
                                                    }
                                                })
                                            })
                                    if (servercool.bicool < Date.now())
                                        if (msg.indexOf("bye") > -1 || msg.indexOf("goodbye") > -1 || msg.startsWith(":wave:") || msg.indexOf("gtg") > -1 || msg.indexOf("cya") > -1)
                                            message.channel.send(byerep[Math.floor(Math.random() * byerep.length)] + message.member.user.username + "!").then(() => {
                                                cooldata[cooldata.indexOf(servercool)].bicool = Date.now() + 300000
                                                coolcol.updateOne({
                                                    '_id': message.guild.id
                                                }, {
                                                    $set: {
                                                        'bicool': Date.now() + 300000
                                                    }
                                                })
                                            })
                                }
                            }
                        }
                    }

                })
            }

            // End of talking

            if(message.content === 'sc' && client.isOwner(message.author) && message.channel.type === 'dm'){
                message.author.send(`${client.guilds.size} servers!`)
            }

            //End of on Message
        })

        client.on('guildMemberAdd', (mem) => {

            let memDoc = {
                '_id': mem.id,
                'status': 'online',
                'reas': ''
            }
            usercol.insertOne(memDoc, function (err, res) {
                if (!err) {
                    console.log(`User registered.`)
                }
            })

            servcol.findOne({
                '_id': mem.guild.id
            }, (err, res) => {

                if (!err) {
                    if (res) {
                        if (res.defrole != 'disabled') {
                            try {
                                mem.addRole(mem.guild.roles.get(res.defrole))
                            } catch (err) {
                                console.error
                            }
                        }
                        if (res.channel != 'disabled') {
                            try {
                                let msgToSend = res.welcome.replace('{server}', mem.guild.name).replace('{user}', mem)
                                client.channels.get(res.channel).send(msgToSend)
                            } catch (err) {
                                console.error
                            }
                        }
                    }
                }

            })


        })

        client.on('guildMemberRemove', (mem) => {
            let saveUser = false
            client.guilds.forEach(server => {
                server.members.forEach(member => {
                    if (member && mem)
                        if (mem.id == member.id)
                            saveUser = true

                })
            })

            if (!saveUser) {
                if (mem) {
                    usercol.findOneAndDelete({
                        '_id': mem
                    }, (err, res) => {
                        if (err) console.error
                        else {
                            console.log("Member deleted.")
                        }
                    })
                }
            }

            servcol.findOne({
                '_id': mem.guild.id
            }, (err, res) => {

                if (!err) {
                    if (res) {
                        if (res.channel != 'disabled') {
                            try {
                                let msgToSend = res.leave.replace('{server}', mem.guild.name).replace('{user}', mem.displayName)
                                client.channels.get(res.channel).send(msgToSend)
                            } catch (err) {
                                console.error
                            }
                        }
                    }
                }
            })
        })
    }
})

client.login(config.token)