Skip to content

Commit 583d6a9

Browse files
Merge pull request #142 from Pdzly/feature/ban
2 parents c7858d5 + 0ecab8e commit 583d6a9

9 files changed

Lines changed: 615 additions & 2 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Command } from "djs-slash-helper";
2+
import {
3+
ApplicationCommandOptionType,
4+
ApplicationCommandType,
5+
} from "discord.js";
6+
import { config } from "../../Config.js";
7+
import { actualMention, fakeMention } from "../../util/users.js";
8+
9+
export const BanCommand: Command<ApplicationCommandType.ChatInput> = {
10+
name: "ban",
11+
description: "Ban a baaaaad boy",
12+
type: ApplicationCommandType.ChatInput,
13+
default_permission: false,
14+
options: [
15+
{
16+
type: ApplicationCommandOptionType.User,
17+
name: "user",
18+
description: "The user to be banned",
19+
required: true,
20+
},
21+
{
22+
type: ApplicationCommandOptionType.String,
23+
name: "reason",
24+
description: "The reason why the user gets banned",
25+
},
26+
{
27+
type: ApplicationCommandOptionType.Boolean,
28+
name: "delete_messages",
29+
description: "Should the users messages be deleted too? Defaults to True",
30+
},
31+
],
32+
33+
handle: async (interaction) => {
34+
if (
35+
!interaction.isChatInputCommand() ||
36+
!interaction.inGuild() ||
37+
interaction.guild === null
38+
)
39+
return;
40+
try {
41+
await interaction.deferReply();
42+
const deleteMessages =
43+
interaction.options.getBoolean("delete_messages") ?? true;
44+
const user = interaction.options.getUser("user", true);
45+
const reason = interaction.options.getString("reason", false);
46+
try {
47+
await user.send({
48+
content: `You got banned from ${interaction.guild.name} ${reason ? `with the reason: ${reason}` : ""}`,
49+
});
50+
} catch {
51+
/* empty */
52+
}
53+
await interaction.guild.bans.create(user, {
54+
reason: reason ?? undefined,
55+
deleteMessageSeconds: deleteMessages ? 604800 : undefined,
56+
});
57+
58+
const auditLogChannel = await interaction.guild.channels.fetch(
59+
config.channels.auditLog,
60+
);
61+
if (auditLogChannel?.isTextBased()) {
62+
await auditLogChannel.send({
63+
content: `${actualMention(interaction.user)} banned ${fakeMention(user)} (${user.id})\nDelete Messages: ${deleteMessages}`,
64+
allowedMentions: {
65+
parse: [],
66+
},
67+
});
68+
}
69+
70+
const banMessage = await interaction.followUp({
71+
content: `Banned ${fakeMention(user)} (${user.id})`,
72+
});
73+
74+
setTimeout(() => banMessage.delete().catch(() => null), 5000);
75+
} catch (e) {
76+
console.error("Failed to ban user: ", e);
77+
78+
if (interaction.replied) {
79+
await interaction.editReply("Something went wrong!");
80+
} else {
81+
await interaction.followUp("Something went wrong!");
82+
}
83+
}
84+
},
85+
};
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { Command } from "djs-slash-helper";
2+
import {
3+
ApplicationCommandOptionType,
4+
ApplicationCommandType,
5+
} from "discord.js";
6+
import { config } from "../../Config.js";
7+
import { actualMention, fakeMention } from "../../util/users.js";
8+
9+
export const BanCommand: Command<ApplicationCommandType.ChatInput> = {
10+
name: "kick",
11+
description: "Ban a baaaaad boy",
12+
type: ApplicationCommandType.ChatInput,
13+
default_permission: false,
14+
options: [
15+
{
16+
type: ApplicationCommandOptionType.User,
17+
name: "user",
18+
description: "The user to be banned",
19+
required: true,
20+
},
21+
{
22+
type: ApplicationCommandOptionType.String,
23+
name: "reason",
24+
description: "The reason why the user gets banned",
25+
},
26+
],
27+
28+
handle: async (interaction) => {
29+
if (
30+
!interaction.isChatInputCommand() ||
31+
!interaction.inGuild() ||
32+
interaction.guild === null
33+
)
34+
return;
35+
try {
36+
await interaction.deferReply();
37+
const user = interaction.options.getUser("user", true);
38+
const reason = interaction.options.getString("reason", false);
39+
40+
const member = await interaction.guild.members.fetch(user.id);
41+
try {
42+
await user.send({
43+
content: `You got kicked from ${interaction.guild.name} ${reason ? `with the reason: ${reason}` : ""}`,
44+
});
45+
} catch {
46+
/* empty */
47+
}
48+
await member.kick(reason ?? undefined);
49+
50+
const auditLogChannel = await interaction.guild.channels.fetch(
51+
config.channels.auditLog,
52+
);
53+
if (auditLogChannel?.isTextBased()) {
54+
await auditLogChannel.send({
55+
content: `${actualMention(interaction.user)} kicked ${fakeMention(user)} (${user.id})`,
56+
allowedMentions: {
57+
parse: [],
58+
},
59+
});
60+
}
61+
62+
const kickMessage = await interaction.followUp({
63+
content: `Kicked ${fakeMention(user)} (${user.id})`,
64+
});
65+
66+
setTimeout(() => kickMessage.delete().catch(() => null), 5000);
67+
} catch (e) {
68+
console.error("Failed to ban user: ", e);
69+
70+
if (interaction.replied) {
71+
await interaction.editReply("Something went wrong!");
72+
} else {
73+
await interaction.followUp("Something went wrong!");
74+
}
75+
}
76+
},
77+
};
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import Module from "../module.js";
22
import { InviteListeners } from "./discordInvitesMonitor.module.js";
3+
import { BanCommand } from "./ban.command.js";
4+
import { UnbanCommand } from "./unban.command.js";
5+
import { TempBanListener } from "./tempBan.module.js";
36

47
export const ModerationModule: Module = {
58
name: "moderation",
6-
commands: [],
7-
listeners: [...InviteListeners],
9+
commands: [BanCommand, UnbanCommand],
10+
listeners: [...InviteListeners, TempBanListener],
811
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { Command } from "djs-slash-helper";
2+
import {
3+
ApplicationCommandOptionType,
4+
ApplicationCommandType,
5+
} from "discord.js";
6+
import { config } from "../../Config.js";
7+
import { actualMention, fakeMention } from "../../util/users.js";
8+
9+
export const SoftBanCommand: Command<ApplicationCommandType.ChatInput> = {
10+
name: "ban",
11+
description: "Ban a baaaaad boy",
12+
type: ApplicationCommandType.ChatInput,
13+
default_permission: false,
14+
options: [
15+
{
16+
type: ApplicationCommandOptionType.User,
17+
name: "user",
18+
description: "The user to be banned",
19+
required: true,
20+
},
21+
{
22+
type: ApplicationCommandOptionType.String,
23+
name: "reason",
24+
description: "The reason why the user gets banned",
25+
},
26+
{
27+
type: ApplicationCommandOptionType.Boolean,
28+
name: "delete_messages",
29+
description: "Should the users messages be deleted too? Defaults to True",
30+
},
31+
],
32+
33+
handle: async (interaction) => {
34+
if (
35+
!interaction.isChatInputCommand() ||
36+
!interaction.inGuild() ||
37+
interaction.guild === null
38+
)
39+
return;
40+
try {
41+
await interaction.deferReply();
42+
const user = interaction.options.getUser("user", true);
43+
const reason = interaction.options.getString("reason", false);
44+
const deleteMessages =
45+
interaction.options.getBoolean("delete_messages", false) ?? true;
46+
try {
47+
await user.send({
48+
content: `You got soft-banned from ${interaction.guild.name} ${reason ? `with the reason: ${reason}` : ""}`,
49+
});
50+
} catch {
51+
/* empty */
52+
}
53+
54+
await interaction.guild.bans.create(user, {
55+
reason: reason ?? undefined,
56+
deleteMessageSeconds: deleteMessages ? 604800 : undefined,
57+
});
58+
59+
const auditLogChannel = await interaction.guild.channels.fetch(
60+
config.channels.auditLog,
61+
);
62+
if (auditLogChannel?.isTextBased()) {
63+
await auditLogChannel.send({
64+
content: `${actualMention(interaction.user)} soft banned ${fakeMention(user)} (${user.id})\nDelete Messages: ${deleteMessages}`,
65+
allowedMentions: {
66+
parse: [],
67+
},
68+
});
69+
}
70+
71+
const softBanMessage = await interaction.followUp({
72+
content: `Soft banned ${fakeMention(user)} (${user.id})`,
73+
});
74+
75+
setTimeout(() => softBanMessage.delete().catch(() => null), 5000);
76+
setTimeout(
77+
() =>
78+
interaction.guild && interaction.guild.bans.remove(user, "Softban"),
79+
5000,
80+
);
81+
} catch (e) {
82+
console.error("Failed to ban user: ", e);
83+
84+
if (interaction.replied) {
85+
await interaction.editReply("Something went wrong!");
86+
} else {
87+
await interaction.followUp("Something went wrong!");
88+
}
89+
}
90+
},
91+
};
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { Command } from "djs-slash-helper";
2+
import {
3+
ApplicationCommandOptionType,
4+
ApplicationCommandType,
5+
} from "discord.js";
6+
import { config } from "../../Config.js";
7+
import { actualMention, fakeMention } from "../../util/users.js";
8+
import { createTempBanModAction } from "./tempBan.js";
9+
10+
export const SoftBanCommand: Command<ApplicationCommandType.ChatInput> = {
11+
name: "tempban",
12+
description: "Temp Ban a baaaaad boy",
13+
type: ApplicationCommandType.ChatInput,
14+
default_permission: false,
15+
options: [
16+
{
17+
type: ApplicationCommandOptionType.User,
18+
name: "user",
19+
description: "The user to be banned",
20+
required: true,
21+
},
22+
{
23+
type: ApplicationCommandOptionType.String,
24+
name: "reason",
25+
description: "The reason why the user gets banned",
26+
},
27+
{
28+
type: ApplicationCommandOptionType.Number,
29+
name: "ban_duration_days",
30+
description: "The duration of the ban in days",
31+
required: true,
32+
},
33+
{
34+
type: ApplicationCommandOptionType.Boolean,
35+
name: "delete_messages",
36+
description: "Should the users messages be deleted too? Defaults to True",
37+
},
38+
],
39+
40+
handle: async (interaction) => {
41+
if (
42+
!interaction.isChatInputCommand() ||
43+
!interaction.inGuild() ||
44+
interaction.guild === null
45+
)
46+
return;
47+
try {
48+
await interaction.deferReply();
49+
const user = interaction.options.getUser("user", true);
50+
const reason = interaction.options.getString("reason", false);
51+
const days = interaction.options.getNumber("ban_duration_days", true);
52+
const deleteMessages =
53+
interaction.options.getBoolean("delete_messages", false) ?? true;
54+
try {
55+
await user.send({
56+
content: `You got temp-banned from ${interaction.guild.name} ${reason ? `with the reason: ${reason}` : ""} for ${days} Days`,
57+
});
58+
} catch {
59+
/* empty */
60+
}
61+
62+
await interaction.guild.bans.create(user, {
63+
reason: reason ?? undefined,
64+
deleteMessageSeconds: deleteMessages ? 604800 : undefined,
65+
});
66+
const unbanTimestamp = Math.floor(
67+
(Date.now() + days * 24 * 60 * 60 * 1000) / 1000,
68+
);
69+
await createTempBanModAction(
70+
interaction.user,
71+
user,
72+
new Date(unbanTimestamp),
73+
reason,
74+
);
75+
76+
const auditLogChannel = await interaction.guild.channels.fetch(
77+
config.channels.auditLog,
78+
);
79+
if (auditLogChannel?.isTextBased()) {
80+
await auditLogChannel.send({
81+
content: `${actualMention(interaction.user)} temp banned ${fakeMention(user)} (${user.id})\nDelete Messages: ${deleteMessages}\nDays: ${days}\nUntil: <t:${
82+
unbanTimestamp
83+
}:R>`,
84+
allowedMentions: {
85+
parse: [],
86+
},
87+
});
88+
}
89+
90+
const tempBanMessage = await interaction.followUp({
91+
content: `Temp banned ${fakeMention(user)} (${user.id})`,
92+
});
93+
94+
setTimeout(() => tempBanMessage.delete().catch(() => null), 5000);
95+
} catch (e) {
96+
console.error("Failed to ban user: ", e);
97+
98+
if (interaction.replied) {
99+
await interaction.editReply("Something went wrong!");
100+
} else {
101+
await interaction.followUp("Something went wrong!");
102+
}
103+
}
104+
},
105+
};

0 commit comments

Comments
 (0)