Browse Source

There are two ways to write error-free programs; only the third one works.

Robert 2 months ago
parent
commit
3f3364bf10
3 changed files with 152 additions and 31 deletions
  1. 0
    0
      New Text Document.txt
  2. 93
    30
      commands.js
  3. 59
    1
      database.js

+ 0
- 0
New Text Document.txt View File


+ 93
- 30
commands.js View File

@@ -1,31 +1,33 @@
1 1
 const db = require('./database');
2 2
 const debug = require('debug')('Bot:commands');
3
+const Moment = require('moment');
3 4
 const cmd = new Map();
4 5
 
5
-async function register(bot, args, message) {
6
-  let user = await db.getUser(message.guild.id, String(message.member.id));
7
-  if (user && user.roleID) {
8
-    message.channel.send(`${message.member} ばか~~、 Posiadasz juz role <@&${user.roleID}>!`);
9
-    return `User(${message.member.displayName}) already created role!`;
10
-  }
11
-  let position = message.guild.roles.resolve('496605731416571925').position + 1
12
-  let role = await message.guild.roles.create({
13
-    data: {
14
-      name: '%'+message.member.displayName,
15
-      color: 'RANDOM',
16
-      position: position
17
-    },
18
-    reason: 'Register command used.'
19
-  });
20
-  if(role && role.id) {
21
-    try {
22
-        message.member.roles.add(role);
23
-    } catch(err) { debug(err); throw err}
24
-    message.channel.send(`${message.author}, Your private role is ${role}`);
25
-    db.addUser(message.guild.id, String(message.member.id), String(role.id));
26
-  } else {message.channel.send(`${message.author}, Cos poszlo nie tak xD`);}
27
-}
28
-cmd.set('register', register);
6
+// async function register(bot, args, message) {
7
+//   let user = await db.getUser(message.guild.id, String(message.member.id));
8
+//   if (user && user.roleID) {
9
+//     message.channel.send(`${message.member} ばか~~、 Posiadasz juz role <@&${user.roleID}>!`);
10
+//     return `User(${message.member.displayName}) already created role!`;
11
+//   }
12
+//   let position = message.guild.roles.resolve('496605731416571925').position + 1
13
+//   let role = await message.guild.roles.create({
14
+//     data: {
15
+//       name: '%'+message.member.displayName,
16
+//       color: 'RANDOM',
17
+//       position: position
18
+//     },
19
+//     reason: 'Register command used.'
20
+//   });
21
+//   if(role && role.id) {
22
+//     try {
23
+//         message.member.roles.add(role);
24
+//     } catch(err) { debug(err); throw err}
25
+//     message.channel.send(`${message.author}, Your private role is ${role}`);
26
+//     db.addUser(message.guild.id, String(message.member.id), String(role.id));
27
+//   } else {message.channel.send(`${message.author}, Cos poszlo nie tak xD`);}
28
+// }
29
+
30
+cmd.set('register', (b,a,m) => m.reply('Rejestracja jest wyłączona!') );
29 31
 
30 32
 async function role(bot, args, message) {
31 33
     if (!args[0]) { return message.reply(`Usage of ...`); }
@@ -47,7 +49,7 @@ async function role(bot, args, message) {
47 49
 
48 50
     nestedCommands[args[0]](bot, args, message);
49 51
 
50
-    async function name(bot, args, message) {
52
+    function name(bot, args, message) {
51 53
       const nestedCommands = {
52 54
         get() {
53 55
           return message.reply(`role name is ${role}.`);
@@ -67,7 +69,7 @@ async function role(bot, args, message) {
67 69
       if (!{}.hasOwnProperty.call(nestedCommands, args[1])) { return message.reply(`Usage of ...`); }
68 70
       nestedCommands[args[1]](message, args);
69 71
     }
70
-    async function color(bot, args, message) {
72
+    function color(bot, args, message) {
71 73
       const nestedCommands = {
72 74
         get() {
73 75
           return message.reply(`role color is ${role.hexColor}.`);
@@ -81,7 +83,7 @@ async function role(bot, args, message) {
81 83
       if (!{}.hasOwnProperty.call(nestedCommands, args[1])) { return message.reply(`Usage of ...`); }
82 84
       nestedCommands[args[1]](message, args);
83 85
     }
84
-    async function restore(bot, args, message) {
86
+    function restore(bot, args, message) {
85 87
       if(!message.member.roles.get(role.id)) {
86 88
         try{ 
87 89
           message.member.roles.add(role.id);
@@ -95,9 +97,11 @@ async function role(bot, args, message) {
95 97
 }
96 98
 cmd.set('role', role);
97 99
 
98
-async function quote(bot, args, message) {
100
+function quote(bot, args, message) {
99 101
   const chQuotes = '300745912571920384';
100 102
   let embedAuthor;
103
+  let userID = args.shift().split('<@').join('').split('>').join('');
104
+  if(userID.startsWith('!')) userID.substr(1);
101 105
 
102 106
   if(!message.mentions.users.first()){
103 107
     embedAuthor = {
@@ -106,8 +110,8 @@ async function quote(bot, args, message) {
106 110
   } else {
107 111
     args.shift();
108 112
     embedAuthor = {
109
-      name: message.guild.members.resolve(message.mentions.users.first().id).displayName,
110
-      icon_url: message.mentions.users.first().displayAvatarURL
113
+      name: message.guild.members.resolve(userID).displayName,
114
+      icon_url: message.guild.members.resolve(userID).displayAvatarURL
111 115
     };
112 116
   }
113 117
 
@@ -124,4 +128,63 @@ async function quote(bot, args, message) {
124 128
 }
125 129
 cmd.set('quote', quote);
126 130
 
131
+async function bound(bot, args, message) {
132
+  if (!args[0]) { return message.reply(`Usage of ...`); }
133
+  const nestedCommands = {
134
+    create: create,
135
+    get: get,
136
+    approve: approve,
137
+  };
138
+  if (!{}.hasOwnProperty.call(nestedCommands, args[0])) { return message.reply(`Usage of ...`); }
139
+
140
+  const callerBound = await db.getBound(String(message.member.id));
141
+  if(!callerBound.approvedBy) return message.reply('Musisz sam zostac zatwierdzony by uzyc tej komedy.')
142
+  
143
+  command = args.shift();
144
+  nestedCommands[command](bot, args, message);
145
+
146
+  async function create(bot, args, message) {
147
+    let userID = args.shift().split('<@').join('').split('>').join('');
148
+    if(userID.startsWith('!')) userID.substr(1);
149
+    let user = await db.getBound(String(userID));
150
+    if(user && user.userID) return message.reply('Ta osoba jest juz poloczona.')
151
+    db.addBound({
152
+      id: userID,
153
+      name: message.guild.members.resolve(userID).displayName,
154
+    },{
155
+      id: message.author.id,
156
+      name: message.member.displayName,
157
+    }).catch(error => debug(error))
158
+  }
159
+
160
+  async function get(bot, args, message) {
161
+    let userID = args.shift().split('<@').join('').split('>').join('');
162
+    if(userID.startsWith('!')) userID = userID.substr(1);
163
+    let user = await db.getBound(String(userID));
164
+    if(!user && !user.userID) return message.reply('Ta osoba nie zostala jeszcze poloczona.')
165
+    const username = message.guild.members.resolve(user.userID) ? 
166
+                      message.guild.members.resolve(user.userID).displayName : 
167
+                      `${user.userName || ''} <@!${user.userID}>`
168
+    return message.reply(undefined, {embed: {
169
+      author: {
170
+        name: username,
171
+      },
172
+      fields: [
173
+        {name: 'Staged User', value: username},
174
+        {name: 'Staged By', value: message.guild.members.resolve(user.boundID) ? 
175
+                                    message.guild.members.resolve(user.boundID).displayName : 
176
+                                    `${user.boundUsername || ''} <@!${user.boundID}>`},
177
+        {name: 'Approved By', value: message.guild.members.resolve(user.approvedBy) ? 
178
+                                      message.guild.members.resolve(user.approvedBy).displayName : '----'},
179
+      ]
180
+    }})
181
+  }
182
+  async function approve() {return}
183
+}
184
+cmd.set('bound', bound)
185
+
186
+cmd.set('repair', (b,a,m) => {
187
+  if(m.author.id !== '103210786695974912') return
188
+  db.repairBound(b,a,m);
189
+})
127 190
 module.exports = cmd;

+ 59
- 1
database.js View File

@@ -17,9 +17,23 @@ const userScheme = new mongoose.Schema({
17 17
   userID: String, roleID: String, timestamp: String,
18 18
 });
19 19
 
20
+const boundScheme = new mongoose.Schema({
21
+  approvedBy: String, boundID: String, boundUsername: String, userID: String, userName: String
22
+});
23
+
24
+/**
25
+ * @param {String} serverID
26
+ * @param {String} userID
27
+ */
20 28
 function getUser(serverID, userID) {
21 29
   return mongoose.model(serverID, userScheme).findOne({ userID });
22 30
 }
31
+
32
+/**
33
+ * @param {String} serverID
34
+ * @param {String} userID
35
+ * @param {String} roleID
36
+ */
23 37
 function addUser(serverID, userID, roleID) {
24 38
   if (!serverID) throw new Error('serverID is undefined!');
25 39
   if (!userID || !roleID) throw new Error(`${!userID ? 'userID' : 'roleID'} is undefined!`);
@@ -28,7 +42,51 @@ function addUser(serverID, userID, roleID) {
28 42
   });
29 43
 }
30 44
 
45
+/**
46
+ * @param {String} userID
47
+ */
48
+function getBound(userID) {
49
+  return mongoose.model('busersbounds', boundScheme).findOne({ userID })
50
+}
51
+
52
+/**
53
+ * @param {Object} user
54
+ * @param {String} user.id
55
+ * @param {String} user.name
56
+ * @param {Object} bound
57
+ * @param {String} bound.id
58
+ * @param {String} bound.name
59
+ */
60
+function addBound(user, bound) {
61
+  return mongoose.model('busersbounds', boundScheme).create({
62
+    approvedBy: undefined, boundID: bound.id, boundUsername: bound.name, userID: user.id, userName: user.name,
63
+  });
64
+}
65
+
66
+/**
67
+ * @param {String} userID
68
+ * @param {String} approverID
69
+ */
70
+function approvedBound(userID, approverID) {
71
+  return mongoose.model('busersbounds', boundScheme).findOneAndUpdate({ userID }, {approvedBy: approverID})
72
+}
73
+
74
+async function repairBound(b,a,m) {
75
+  const users = await mongoose.model('busersbounds', boundScheme).find().exec()
76
+  for(user of users) {
77
+    try {
78
+      mongoose.model('busersbounds', boundScheme).findOneAndUpdate({ userID: user.userID }, {userName: m.guild.members.resolve(user.userID).displayName}).exec()
79
+    } catch (error) {
80
+      debug(error, user.userID)
81
+    }
82
+  }
83
+}
84
+
31 85
 module.exports = {
32 86
   getUser,
33
-  addUser
87
+  addUser,
88
+  getBound,
89
+  addBound,
90
+  approvedBound,
91
+  repairBound,
34 92
 };

Loading…
Cancel
Save