aboutsummaryrefslogtreecommitdiff
path: root/src/util.js
blob: 68f2c5691a95d9461fe5ad8fd22f9ddf7a7cd7fe (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
112
113
114
115
116
117
118
119
120
121
const rem_emp = function (e) {return e !== "";}
const { ActivityType , PermissionsBitField} = require("discord.js");
module.exports = {
  is_mod(member){
    return member.permissions!=null&&
      (member.roles.cache.has("814558111234654258")||member.roles.cache.has("586781513471885323")||member.permissions?.has(PermissionsBitField.Flags.KickMembers))
  },
  generate_id(){
    let config = JSON.parse(fs.readFileSync(config_loc))
    var ticket = "";
    var characters = config["ticket-id-chars"].value;
    for (var i = 0; i < config["ticket-id-length"].value; i++) {
        ticket += characters.charAt(Math.floor(Math.random() * characters.length));
        if (i == config["ticket-id-split"].value) ticket += "-";
    }
    return ticket
  },
  diff(a,b){
    return (a>b)?(a-b):(b-a);
  },
  upload_limit(guild){
    //https://stackoverflow.com/questions/66396146/get-channels-filesize-limit
    switch (guild.premiumTier) {
      case 'TIER_3': return 100
      case 'TIER_2': return 50
      default: return 8
    }
  },
  limit_exp(exp,length){
    exp = exp.split('e')
    for(let i = 0; i!=length; i++)
      exp[0]+="0"
    exp[0] = exp[0].substring(0,length+2)
    return exp.join('e')
  },
  rem_emp,
  parse_inp(str){
    let open = false;
    let out = []
    let cur = ""
    for(let c of str){
      if(c=='"'){
        if(open){
          out.push(cur)
          cur=""
        }
        open=!open;
      }
      else {
        if(!open){
          if(c==' '){
            out.push(cur)
            cur=""
          } else {
            cur+=c
          }
        } else {
          cur+=c
        }
          
      }
    }
    out.push(cur)
    return out.filter(rem_emp)
  },
  similarity(s1, s2) {
    //https://stackoverflow.com/questions/10473745/compare-strings-javascript-return-of-likely
    function editDistance(s1, s2) {
      s1 = s1.toLowerCase();
      s2 = s2.toLowerCase();
    
      var costs = new Array();
      for (var i = 0; i <= s1.length; i++) {
        var lastValue = i;
        for (var j = 0; j <= s2.length; j++) {
          if (i == 0)
            costs[j] = j;
          else {
            if (j > 0) {
              var newValue = costs[j - 1];
              if (s1.charAt(i - 1) != s2.charAt(j - 1))
                newValue = Math.min(Math.min(newValue, lastValue),
                  costs[j]) + 1;
              costs[j - 1] = lastValue;
              lastValue = newValue;
            }
          }
        }
        if (i > 0)
          costs[s2.length] = lastValue;
      }
      return costs[s2.length];
    }
    var longer = s1;
    var shorter = s2;
    if (s1.length < s2.length) {
      longer = s2;
      shorter = s1;
    }
    var longerLength = longer.length;
    if (longerLength == 0) {
      return 1.0;
    }
    return (longerLength - editDistance(longer, shorter)) / parseFloat(longerLength);
  },

  deepCopy(src) {
    //https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/49497485
    let target = Array.isArray(src) ? [] : {};
    for (let prop in src) {
      let value = src[prop];
      if(value && typeof value === 'object') {
        target[prop] = deepCopy(value);
    } else {
        target[prop] = value;
    }
   }
      return target;
  }
  
}