/[sudobot]/trunk/commands/mute.js
ViewVC logotype

Contents of /trunk/commands/mute.js

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26