aboutsummaryrefslogtreecommitdiff
path: root/events/member-update.js
blob: 2074d9b8613cc52ac04ac750116352115fcf2a0c (plain)
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
const { ActivityType , PermissionsBitField, ActionRowBuilder,ButtonBuilder,ButtonStyle  } = require("discord.js");
const fs = require('fs')
const path = require("path");
const { EmbedBuilder } = require("discord.js");
let db = require("../src/db")
let settings = require("../src/settings")
let llog = require("../src/logg")
let config_loc = __filename+".json"
module.exports = {
    name : "guildMemberUpdate",
    config_loc : config_loc,
    _build_message(p, t){
        global.channels["general"].messages.fetch({limit : 1}).then(async messages => {
            let message = messages.first();
            if(message.id == global.notif.id){
                //edit
                let mess = "";
                //TODO: this can be a single loop
                if(global.notif.voted.length != 0){
                    for(let i = 0; i != global.notif.voted.length - 1; i++){
                        mess += "<@"+global.notif.voted[i]+">, ";
                    }
                    if(global.notif.voted.length > 1) mess += "and "
                    mess += "<@"+global.notif.voted[global.notif.voted.length - 1]+"> just voted! You can too by clicking [here](https://discords.com/servers/486957006628847626/upvote)"
                }

                if(global.notif.joined.length != 0){
                    if(global.notif.voted.length != 0) mess += " and\n"
                    mess += "Welcome to the server "
                    for(let i = 0; i != global.notif.joined.length - 1; i++){
                        mess += "<@"+global.notif.joined[i]+">, ";
                    }
                    if(global.notif.joined.length > 1) mess += "and "
                    mess += "<@"+global.notif.joined[global.notif.joined.length - 1]+">!"
                }
                message.edit({content: mess, flags:[4096]})
            } else {
                global.notif.voted = []
                global.notif.joined = []
                global.notif[t] = [p]

                if(t == "voted"){
                    global.notif.id = await global.channels["general"].send({content:`<@${p}> just voted! You can too by clicking [here](https://discords.com/servers/486957006628847626/upvote).`,
                    flags: [ 4096 ]})
                } else if(t == "joined"){
                    global.notif.id = await global.channels["general"].send({content:`Welcome to the server <@${p}>!`,
                    flags: [ 4096 ]})
                }
            }
        })
    },
    build_message(p, t){
        let lim = 1;
        let types = {
            voted: {start: "", end: " just voted! You can too by clicking [here](https://discords.com/servers/486957006628847626/upvote)"},
            joined: {start: "Welcome to the server ", end: "!"},
        }
        global.channels["general"].messages.fetch({limit : lim}).then(async messages => {
            //console.log(messages)
            let found = false
            let message = null
            for(let m of messages){
                if(m[0] == global.notif[t+"_id"]){
                    if(lim == 1) message = m[1];
                        else {
                        try{
                            await m[1].delete()
                        } catch(e) {
                            llog.error("failed to delete message")
                        }
                    }
                    found = true
                    break
                }
            }
            if(!found) global.notif[t] = [p]
            let con = types[t].start
            for(let i = 0; i != global.notif[t].length - 1; i++){
                con += "<@"+global.notif[t][i]+">, "
            }
            if(global.notif[t].length > 1) con += "and "
            con += "<@"+global.notif[t][global.notif[t].length - 1]+">"+types[t].end

            if(lim == 1 && !found || lim != 1){
                global.notif[t+"_id"] = await global.channels["general"].send({content:con, flags:[4096]})
            } else {
                message.edit({content:con, flags:[4096]})
            }
            

        })
    },
    async main (client,Discord){
        client.on("guildMemberUpdate",(oldMember, newMember)=>{
            if(!oldMember.roles.cache.has("761225110060662854") && 
                    newMember.roles.cache.has("761225110060662854")){
                
                if(global.notif == null)
                    global.notif = {}
                if(global.notif.voted == null)
                    global.notif.joined = global.notif.voted = []

                global.notif.voted.push(newMember.id)

                this.build_message(newMember.id, "voted")
                
                
            }
       })
    },
}