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

Annotation of /trunk/commands/mute.js

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26