1 |
import { BanOptions, CommandInteraction, Guild, GuildMember, Interaction, Message, User } from 'discord.js'; |
2 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
3 |
import DiscordClient from '../../client/Client'; |
4 |
import CommandOptions from '../../types/CommandOptions'; |
5 |
import InteractionOptions from '../../types/InteractionOptions'; |
6 |
import MessageEmbed from '../../client/MessageEmbed'; |
7 |
import getUser from '../../utils/getUser'; |
8 |
import History from '../../automod/History'; |
9 |
import getMember from '../../utils/getMember'; |
10 |
import ms from 'ms'; |
11 |
|
12 |
export async function warn(client: DiscordClient, user: User, reason: string | undefined, msg: Message | CommandInteraction, callback: Function, warned_by1?: User) { |
13 |
await client.db.get('INSERT INTO warnings(user_id, guild_id, strike, reason, warned_by) VALUES(?, ?, 1, ?, ?)', [user.id, msg.guild!.id, reason === undefined ? '\c\b\c' : reason, warned_by1 === undefined ? msg.member!.user.id : warned_by1.id], async (err: any) => { |
14 |
if (err) { |
15 |
console.log(err); |
16 |
} |
17 |
|
18 |
await client.db.get('SELECT * FROM warnings WHERE user_id = ? AND guild_id = ? ORDER BY id DESC LIMIT 0, 1', [user.id, msg.guild!.id], async (err: any, data: any) => { |
19 |
if (err) { |
20 |
console.log(err); |
21 |
} |
22 |
|
23 |
await client.db.get('SELECT id, COUNT(*) as count, guild_id, user_id FROM warnings WHERE user_id = ? AND guild_id = ?', [user.id, msg.guild!.id], async (err: any, data3: any) => { |
24 |
await client.logger.logWarn(msg as Message, user, warned_by1 === undefined ? msg.member!.user as User : warned_by1, typeof reason === 'undefined' ? '*No reason provided*' : reason, data.id); |
25 |
|
26 |
await History.create(user.id, msg.guild!, 'warn', warned_by1 === undefined ? msg.member!.user.id : warned_by1.id, typeof reason === 'undefined' ? null : reason, async () => { |
27 |
await user.send({ |
28 |
embeds: [ |
29 |
new MessageEmbed() |
30 |
.setAuthor({ |
31 |
iconURL: msg.guild!.iconURL() as string, |
32 |
name: `\tYou have been warned in ${msg.guild!.name}` |
33 |
}) |
34 |
.addFields([ |
35 |
{ |
36 |
name: "Reason", |
37 |
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
38 |
}, |
39 |
{ |
40 |
name: "Strike", |
41 |
value: data3.count + ' time(s)' |
42 |
} |
43 |
]) |
44 |
] |
45 |
}); |
46 |
|
47 |
console.log(data3); |
48 |
|
49 |
callback({count: data3.count, ...data}); |
50 |
}); |
51 |
}); |
52 |
}); |
53 |
}); |
54 |
} |
55 |
|
56 |
export default class WarnCommand extends BaseCommand { |
57 |
supportsInteractions: boolean = true; |
58 |
|
59 |
constructor() { |
60 |
super('warn', 'moderation', []); |
61 |
} |
62 |
|
63 |
async run(client: DiscordClient, msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions) { |
64 |
if (!options.isInteraction && typeof options.args[0] === 'undefined') { |
65 |
await msg.reply({ |
66 |
embeds: [ |
67 |
new MessageEmbed() |
68 |
.setColor('#f14a60') |
69 |
.setDescription(`This command requires at least one argument.`) |
70 |
] |
71 |
}); |
72 |
|
73 |
return; |
74 |
} |
75 |
|
76 |
let user: GuildMember; |
77 |
let reason: string | undefined; |
78 |
|
79 |
if (options.isInteraction) { |
80 |
user = await <GuildMember> options.options.getMember('member'); |
81 |
|
82 |
if (!user) { |
83 |
await msg.reply({ |
84 |
embeds: [ |
85 |
new MessageEmbed() |
86 |
.setColor('#f14a60') |
87 |
.setDescription("Invalid user given.") |
88 |
] |
89 |
}); |
90 |
|
91 |
return; |
92 |
} |
93 |
|
94 |
if (options.options.getString('reason')) { |
95 |
reason = <string> options.options.getString('reason'); |
96 |
} |
97 |
} |
98 |
else { |
99 |
try { |
100 |
const user2 = await getMember((msg as Message), options); |
101 |
|
102 |
if (!user2) { |
103 |
throw new Error('Invalid user'); |
104 |
} |
105 |
|
106 |
user = user2; |
107 |
} |
108 |
catch (e) { |
109 |
await msg.reply({ |
110 |
embeds: [ |
111 |
new MessageEmbed() |
112 |
.setColor('#f14a60') |
113 |
.setDescription(`Invalid user given.`) |
114 |
] |
115 |
}); |
116 |
|
117 |
return; |
118 |
} |
119 |
|
120 |
console.log(user); |
121 |
|
122 |
if (options.args[1]) { |
123 |
await options.args.shift(); |
124 |
reason = options.args.join(' '); |
125 |
} |
126 |
} |
127 |
|
128 |
try { |
129 |
await warn(client, user.user, reason, msg, async (data: any) => { |
130 |
await msg.reply({ |
131 |
embeds: [ |
132 |
new MessageEmbed() |
133 |
.setDescription(`The user ${user.user.tag} has been warned`) |
134 |
.addFields([ |
135 |
{ |
136 |
name: "Reason", |
137 |
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
138 |
}, |
139 |
{ |
140 |
name: "Strike", |
141 |
value: data.count + ' time(s)' |
142 |
}, |
143 |
{ |
144 |
name: "Warned by", |
145 |
value: (msg.member?.user as User).tag |
146 |
}, |
147 |
{ |
148 |
name: "ID", |
149 |
value: data.id + '' |
150 |
} |
151 |
]) |
152 |
] |
153 |
}); |
154 |
}, msg.member?.user as User); |
155 |
} |
156 |
catch (e) { |
157 |
console.log(e); |
158 |
} |
159 |
} |
160 |
} |