1 |
import { BanOptions, CommandInteraction, Guild, GuildMember, Interaction, Message, User } from 'discord.js'; |
/** |
2 |
|
* This file is part of SudoBot. |
3 |
|
* |
4 |
|
* Copyright (C) 2021-2022 OSN Inc. |
5 |
|
* |
6 |
|
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
|
* under the terms of the GNU Affero General Public License as published by |
8 |
|
* the Free Software Foundation, either version 3 of the License, or |
9 |
|
* (at your option) any later version. |
10 |
|
* |
11 |
|
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
* GNU Affero General Public License for more details. |
15 |
|
* |
16 |
|
* You should have received a copy of the GNU Affero General Public License |
17 |
|
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
|
*/ |
19 |
|
|
20 |
|
import { CommandInteraction, GuildMember, Message, User } from 'discord.js'; |
21 |
import BaseCommand from '../../utils/structures/BaseCommand'; |
import BaseCommand from '../../utils/structures/BaseCommand'; |
22 |
import DiscordClient from '../../client/Client'; |
import DiscordClient from '../../client/Client'; |
23 |
import CommandOptions from '../../types/CommandOptions'; |
import CommandOptions from '../../types/CommandOptions'; |
24 |
import InteractionOptions from '../../types/InteractionOptions'; |
import InteractionOptions from '../../types/InteractionOptions'; |
25 |
import MessageEmbed from '../../client/MessageEmbed'; |
import MessageEmbed from '../../client/MessageEmbed'; |
|
import getUser from '../../utils/getUser'; |
|
|
import History from '../../automod/History'; |
|
26 |
import getMember from '../../utils/getMember'; |
import getMember from '../../utils/getMember'; |
27 |
import ms from 'ms'; |
import PunishmentType from '../../types/PunishmentType'; |
28 |
|
import { hasPermission } from '../../utils/util'; |
29 |
|
|
30 |
export async function warn(client: DiscordClient, user: User, reason: string | undefined, msg: Message | CommandInteraction, callback: Function, warned_by1?: User) { |
export async function warn(client: DiscordClient, user: User, reason: string | undefined, msg: Message | CommandInteraction, warned_by?: User) { |
31 |
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) => { |
const { default: Punishment } = await import('../../models/Punishment'); |
|
if (err) { |
|
|
console.log(err); |
|
|
} |
|
32 |
|
|
33 |
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) => { |
const warning = await Punishment.create({ |
34 |
if (err) { |
guild_id: msg.guild!.id, |
35 |
console.log(err); |
user_id: user.id, |
36 |
} |
reason, |
37 |
|
mod_id: warned_by?.id ?? msg.member!.user.id, |
38 |
|
mod_tag: warned_by?.tag ?? (msg.member!.user as User).tag, |
39 |
|
type: PunishmentType.WARNING, |
40 |
|
createdAt: new Date() |
41 |
|
}); |
42 |
|
|
43 |
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) => { |
const strike = await Punishment.count({ |
44 |
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); |
guild_id: msg.guild!.id, |
45 |
|
user_id: user.id, |
46 |
await History.create(user.id, msg.guild!, 'warn', warned_by1 === undefined ? msg.member!.user.id : warned_by1.id, typeof reason === 'undefined' ? null : reason, async () => { |
type: PunishmentType.WARNING, |
47 |
await user.send({ |
}); |
|
embeds: [ |
|
|
new MessageEmbed() |
|
|
.setAuthor({ |
|
|
iconURL: msg.guild!.iconURL() as string, |
|
|
name: `\tYou have been warned in ${msg.guild!.name}` |
|
|
}) |
|
|
.addFields([ |
|
|
{ |
|
|
name: "Reason", |
|
|
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
|
|
}, |
|
|
{ |
|
|
name: "Strike", |
|
|
value: data3.count + ' time(s)' |
|
|
} |
|
|
]) |
|
|
] |
|
|
}); |
|
48 |
|
|
49 |
console.log(data3); |
// await History.create(user.id, msg.guild!, 'warn', warned_by?.id ?? msg.member!.user.id, reason ?? null); |
50 |
|
|
51 |
callback({count: data3.count, ...data}); |
let DMed = true; |
52 |
}); |
|
53 |
}); |
try { |
54 |
}); |
await user.send({ |
55 |
}); |
embeds: [ |
56 |
|
new MessageEmbed({ |
57 |
|
author: { |
58 |
|
name: `You have been warned in ${msg.guild!.name}`, |
59 |
|
iconURL: msg.guild!.iconURL()! |
60 |
|
}, |
61 |
|
fields: [ |
62 |
|
{ |
63 |
|
name: 'Reason', |
64 |
|
value: reason ?? '*No reason provided*' |
65 |
|
}, |
66 |
|
{ |
67 |
|
name: 'Strike', |
68 |
|
value: `${strike} time(s)` |
69 |
|
} |
70 |
|
] |
71 |
|
}) |
72 |
|
] |
73 |
|
}); |
74 |
|
} |
75 |
|
catch (e) { |
76 |
|
console.log(e); |
77 |
|
DMed = false; |
78 |
|
} |
79 |
|
|
80 |
|
await client.logger.logWarn(msg, user, (warned_by ?? msg.member!.user) as User, reason, warning.get('id') as number); |
81 |
|
|
82 |
|
return { warning, strike, DMed }; |
83 |
} |
} |
84 |
|
|
85 |
export default class WarnCommand extends BaseCommand { |
export default class WarnCommand extends BaseCommand { |
147 |
} |
} |
148 |
|
|
149 |
console.log(user); |
console.log(user); |
150 |
|
|
151 |
|
|
152 |
if (options.args[1]) { |
if (options.args[1]) { |
153 |
await options.args.shift(); |
await options.args.shift(); |
156 |
} |
} |
157 |
|
|
158 |
try { |
try { |
159 |
await warn(client, user.user, reason, msg, async (data: any) => { |
if (!(await hasPermission(client, user, msg, null, "You don't have permission to warn this user."))) { |
160 |
await msg.reply({ |
return; |
161 |
embeds: [ |
} |
162 |
new MessageEmbed() |
|
163 |
.setDescription(`The user ${user.user.tag} has been warned`) |
const { warning, strike, DMed } = await warn(client, user.user, reason, msg, msg.member?.user as User); |
164 |
.addFields([ |
|
165 |
{ |
await msg.reply({ |
166 |
name: "Reason", |
embeds: [ |
167 |
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
new MessageEmbed() |
168 |
}, |
.setDescription(`The user ${user.user.tag} has been warned` + (DMed ? "" : "\n:warning: The user has DMs disabled, so they might not know that they've been warned.")) |
169 |
{ |
.addFields([ |
170 |
name: "Strike", |
{ |
171 |
value: data.count + ' time(s)' |
name: "Reason", |
172 |
}, |
value: typeof reason === 'undefined' ? '*No reason provided*' : reason |
173 |
{ |
}, |
174 |
name: "Warned by", |
{ |
175 |
value: (msg.member?.user as User).tag |
name: "Strike", |
176 |
}, |
value: strike + ' time(s)' |
177 |
{ |
}, |
178 |
name: "ID", |
{ |
179 |
value: data.id + '' |
name: "Warned by", |
180 |
} |
value: (msg.member?.user as User).tag |
181 |
]) |
}, |
182 |
] |
{ |
183 |
}); |
name: "ID", |
184 |
}, msg.member?.user as User); |
value: warning.get('id') + '' |
185 |
|
} |
186 |
|
]) |
187 |
|
] |
188 |
|
}); |
189 |
} |
} |
190 |
catch (e) { |
catch (e) { |
191 |
console.log(e); |
console.log(e); |
192 |
} |
} |
193 |
} |
} |
|
} |
|
194 |
|
} |