1 |
const MessageEmbed = require("../src/MessageEmbed"); |
2 |
const ms = require('ms'); |
3 |
const { unmute } = require("./unmute"); |
4 |
const History = require("../src/History"); |
5 |
const { getUser } = require("../src/UserInput"); |
6 |
|
7 |
module.exports = { |
8 |
async handle(msg, cm) { |
9 |
if (typeof cm.args[0] === 'undefined') { |
10 |
await msg.reply({ |
11 |
embeds: [ |
12 |
new MessageEmbed() |
13 |
.setColor('#f14a60') |
14 |
.setDescription(`This command requires at least one argument.`) |
15 |
] |
16 |
}); |
17 |
|
18 |
return; |
19 |
} |
20 |
try { |
21 |
var user = await getUser(cm.args[0], msg); |
22 |
|
23 |
console.log(user); |
24 |
|
25 |
if (!user) { |
26 |
throw new Error('Invalid User'); |
27 |
} |
28 |
} |
29 |
catch (e) { |
30 |
console.log(e); |
31 |
|
32 |
await msg.reply({ |
33 |
embeds: [ |
34 |
new MessageEmbed() |
35 |
.setColor('#f14a60') |
36 |
.setDescription(`Invalid user given.`) |
37 |
] |
38 |
}); |
39 |
|
40 |
return; |
41 |
} |
42 |
|
43 |
let reason; |
44 |
let tmp = false; |
45 |
let timeMs, time2; |
46 |
|
47 |
let time = cm.args.find(arg => { |
48 |
if (arg === '-t') { |
49 |
tmp = true; |
50 |
return false; |
51 |
} |
52 |
|
53 |
return tmp; |
54 |
}); |
55 |
|
56 |
if (time) { |
57 |
timeMs = ms(time); |
58 |
|
59 |
if (timeMs === undefined) { |
60 |
await msg.reply({ |
61 |
embeds: [ |
62 |
new MessageEmbed() |
63 |
.setColor('#f14a60') |
64 |
.setDescription(`The option \`-t\` requires one argument and it must be a valid time interval.`) |
65 |
] |
66 |
}); |
67 |
|
68 |
return; |
69 |
} |
70 |
|
71 |
time2 = timeMs + new Date().getTime(); |
72 |
} |
73 |
|
74 |
let args = [...cm.args]; |
75 |
|
76 |
args.shift(); |
77 |
delete args[args.indexOf(time)]; |
78 |
delete args[args.indexOf('-t')]; |
79 |
|
80 |
if (args.length > 0) { |
81 |
await (reason = args.join(' ')); |
82 |
} |
83 |
|
84 |
if (time) { |
85 |
await app.db.get("INSERT INTO unmutes(user_id, guild_id, time) VALUES(?, ?, ?)", [user.id, msg.guild.id, new Date(time2).toISOString()], async (err) => { |
86 |
if (err) |
87 |
console.log(err); |
88 |
|
89 |
console.log('A timeout has been set.'); |
90 |
|
91 |
setTimeout(async () => { |
92 |
await app.db.get("SELECT * FROM unmutes WHERE time = ?", [new Date(time2).toISOString()], async (err, data) => { |
93 |
if (err) |
94 |
console.log(err); |
95 |
|
96 |
if (data) { |
97 |
await app.db.get('DELETE FROM unmutes WHERE id = ?', [data.id], async (err) => { |
98 |
let guild = await app.client.guilds.cache.find(g => g.id === data.guild_id); |
99 |
let member = await guild?.members.cache.find(m => m.id === data.user_id); |
100 |
|
101 |
if (member) { |
102 |
await unmute(member, null, guild, true, app.client.user); |
103 |
await History.create(member.id, msg.guild, 'unmute', app.client.user.id, async (data2) => {}); |
104 |
} |
105 |
|
106 |
console.log(data); |
107 |
}); |
108 |
} |
109 |
}); |
110 |
}, timeMs); |
111 |
}); |
112 |
} |
113 |
|
114 |
this.mute(user, reason, msg, true, timeMs !== undefined ? timeMs : undefined); |
115 |
}, |
116 |
async mute(user, reason, msg, log, timeMs) { |
117 |
try { |
118 |
let mutedRole = await msg.guild.roles.cache.find(role => role.id === app.config.get('mute_role')); |
119 |
// let generalRole = await msg.guild.roles.cache.find(role => role.id === app.config.get('gen_role')); |
120 |
|
121 |
if (typeof mutedRole !== 'object' || mutedRole === null) { |
122 |
await msg.reply({ |
123 |
embeds: [ |
124 |
new MessageEmbed() |
125 |
.setColor('#f14a60') |
126 |
.setDescription(`No muted role is set.`) |
127 |
] |
128 |
}); |
129 |
|
130 |
return; |
131 |
} |
132 |
|
133 |
// if (typeof generalRole !== 'object' || generalRole === null) { |
134 |
// await msg.reply({ |
135 |
// embeds: [ |
136 |
// new MessageEmbed() |
137 |
// .setColor('#f14a60') |
138 |
// .setDescription(`No general role is set.`) |
139 |
// ] |
140 |
// }); |
141 |
|
142 |
// return; |
143 |
// } |
144 |
|
145 |
if (!log) |
146 |
await History.create(user.id, msg.guild, 'mute', msg.author.id, typeof reason === 'undefined' || reason.trim() === '' ? null : reason, async (data2) => {}); |
147 |
|
148 |
await user.roles.add(mutedRole); |
149 |
// await user.roles.remove(generalRole); |
150 |
|
151 |
if (timeMs !== false) |
152 |
await app.logger.logMute(user, typeof reason === 'undefined' || reason.trim() === '' ? '*No reason provided*' : reason, timeMs, msg.author); |
153 |
} |
154 |
catch(e) { |
155 |
console.log(e); |
156 |
|
157 |
await msg.reply({ |
158 |
embeds: [ |
159 |
new MessageEmbed() |
160 |
.setColor('#f14a60') |
161 |
.setDescription(`:x: I don't have enough permission to assign the muted role to this user.`) |
162 |
] |
163 |
}); |
164 |
|
165 |
return; |
166 |
} |
167 |
|
168 |
await user.send({ |
169 |
embeds: [ |
170 |
new MessageEmbed() |
171 |
.setAuthor({ |
172 |
iconURL: msg.guild.iconURL(), |
173 |
name: `\tYou have been muted in ${msg.guild.name}` |
174 |
}) |
175 |
.addFields([ |
176 |
{ |
177 |
name: "Reason", |
178 |
value: typeof reason === 'undefined' || reason.trim() === '' ? '*No reason provided*' : reason |
179 |
} |
180 |
]) |
181 |
] |
182 |
}); |
183 |
|
184 |
await msg.reply({ |
185 |
embeds: [ |
186 |
new MessageEmbed() |
187 |
.setDescription(`The user ${user.user.tag} has been muted`) |
188 |
.addFields([ |
189 |
{ |
190 |
name: "Reason", |
191 |
value: typeof reason === 'undefined' || reason.trim() === '' ? '*No reason provided*' : reason |
192 |
} |
193 |
]) |
194 |
] |
195 |
}); |
196 |
} |
197 |
}; |