BanSystem/index.js
2024-08-20 14:32:33 -06:00

441 lines
13 KiB
JavaScript

require("dotenv").config()
const Discord = require("discord.js")
const colors = require("colors")
const express = require("express")
const noblox = require("noblox.js")
const fs = require("fs")
const { url } = require("inspector")
const sqlite3 = require("sqlite3").verbose()
noblox.setCookie(process.env.COOKIE)
const Client = new Discord.Client({
intents: [
"Guilds"
]
})
const {
REST,
Routes
} = require('discord.js');
const { title, send } = require("process");
const rest = new REST({
version: '10'
}).setToken(process.env.DISCORD_TOKEN);
const app = express()
const port = process.env.SERVER_PORT || 3000
const db = new sqlite3.Database("database.db")
db.on("open", () => {
console.log(`${colors.cyan("[DB]")} Connected to the database`);
fs.readdirSync("./migrations").forEach((file) => {
if (file.endsWith(".sql")) {
const migration = fs.readFileSync(`./migrations/${file}`, "utf8");
db.run(migration);
console.log(`${colors.cyan("[DB]")} ${file} ${colors.green("executed")}`);
}
});
});
app.use(express.json())
app.use((req, res, next) => {
console.log(`${colors.cyan("[API]")} ${req.ip} ${req.method} ${req.path}`);
next()
})
app.get("/", (req, res) => {
res.send("I'm alive!")
});
app.get("/api/v1/check/:placeId/:userId", async (req, res) => {
// check the bans table for the user, userid: text, banned: integer, reason: text
const userId = req.params.userId
const placeId = req.params.placeId
db.get("SELECT * FROM bans WHERE userId = ?", [userId], async (err, row) => {
if (err) {
console.error(err)
res.json({
status: "error",
message: "An error occurred while checking the user's ban status.",
stack: err.stack,
}).status(500)
} else {
if (row) {
if (row.banned === 1) {
res.json({
status: "success",
banned: true,
reason: row.reason
}).status(200);
if (req.query.nolog) return;
userInfo = await noblox.getPlayerInfo(userId)
userThumbnail = await noblox.getPlayerThumbnail([userId], 720, "png", false, "body")
const embed = {
title: "Banned user attempted to join",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0xff0000,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
},
{
name: "Ban Details",
value: `Reason: ${row.reason}
Timestamp: <t:${Math.floor(new Date(row.timestamp) / 1000)}:R> <t:${Math.floor(new Date(row.timestamp) / 1000)}:f>`
},
{
name: "Place ID",
value: `[${placeId}](https://www.roblox.com/games/${placeId}/urlgenerator)`
}
]
}
Client.channels.cache.get(process.env.LOG_CHANNEL).send({ embeds: [embed] })
} else {
res.json({
status: "success",
banned: false
}).status(200);
}
} else {
res.json({
status: "notfound",
banned: false
}).status(200);
}
}
})
});
Client.on("ready", async () => {
console.log(`${colors.cyan("[Discord]")} Logged in as ${Client.user.tag}`)
const commands = [
{
name: "ban",
description: "Ban a user from all games",
type: 1,
default_member_permissions: 8,
options: [
{
name: "user",
description: "The user ID to ban",
type: 3,
required: true
},
{
name: "reason",
description: "The reason for the ban",
type: 3,
required: true
}
]
},
{
name: "unban",
description: "Unban a user from all games",
type: 1,
default_member_permissions: 8,
options: [
{
name: "user",
description: "The user ID to unban",
type: 3,
required: true,
}
]
},
{
name: "check",
description: "Check if a user is banned",
type: 1,
options: [
{
name: "user",
description: "The user ID to check",
type: 3,
required: true
}
]
}
]
//Register commands
await(async () => {
try {
console.log(`${colors.cyan("[Discord]")} Registering Commands...`)
//Global
await rest.put(Routes.applicationCommands(Client.user.id), { body: commands })
console.log(`${colors.cyan("[Discord]")} Successfully registered commands.`);
} catch (error) {
console.error(error);
}
})();
app.listen(port, () => {
console.log(`${colors.cyan("[API]")} Listening on port ${port}`)
});
});
Client.on("interactionCreate", async (interaction) => {
switch (interaction.type) {
case Discord.InteractionType.ApplicationCommand:
switch (interaction.commandName) {
case "ban":
// If the user isnt already banned, ban them
userId = interaction.options.get("user").value
reason = interaction.options.get("reason").value
userInfo = await noblox.getPlayerInfo(userId)
console.log(userInfo)
userThumbnail = await noblox.getPlayerThumbnail([userId], 720, "png", false, "body")
db.get("SELECT * FROM bans WHERE userId = ?", [userId], (err, row) => {
if (err) {
console.error(err)
interaction.reply({
content: "An error occurred while checking the user's ban status.",
ephemeral: true
})
} else {
if (!row) {
db.run("INSERT INTO bans (userId, banned, reason, timestamp) VALUES (?, ?, ?, ?)", [userId, 1, reason, new Date().toISOString()], (err) => {
if (err) {
console.error(err)
interaction.reply({
content: "An error occurred while banning the user.",
ephemeral: true
})
} else {
embed = {
title: "User banned",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0xff0000,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
},
{
name: "Ban Details",
value: `Reason: ${reason}
Timestamp: <t:${Math.floor(new Date(Date.now()) / 1000)}:R> <t:${Math.floor(new Date(Date.now()) / 1000)}:f>`
}
]
}
return interaction.reply({ embeds: [embed] });
}
})
} else if(row.banned === 0) {
db.run("UPDATE bans SET banned = 1, reason = ?, timestamp = ? WHERE userId = ?", [reason, new Date().toISOString(), userId], (err) => {
if (err) {
console.error(err)
interaction.reply({
content: "An error occurred while banning the user.",
ephemeral: true
})
} else {
embed = {
title: "User banned",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0xff0000,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
},
{
name: "Ban Details",
value: `Reason: ${reason}
Timestamp: <t:${Math.floor(new Date(Date.now()) / 1000)}:R> <t:${Math.floor(new Date(Date.now()) / 1000)}:f>`
}
]
}
return interaction.reply({ embeds: [embed] });
}
})
} else {
interaction.repl({
content: "User is already banned.",
ephemeral: true
})
}
}
})
break;
case "unban":
// If the user is banned, unban them
userId = await interaction.options.get("user").value
userInfo = await noblox.getPlayerInfo(userId)
console.log(userInfo)
userThumbnail = await noblox.getPlayerThumbnail([userId], 720, "png", false, "body")
db.get("SELECT * FROM bans WHERE userId = ?", [userId], (err, row) => {
if (err) {
console.error(err)
interaction.reply({
content: "An error occurred while checking the user's ban status.",
ephemeral: true
})
} else {
if (row) {
if (!row.banned) {
interaction.reply({
content: "User is not banned.",
ephemeral: true
})
return;
}
db.run("UPDATE bans SET banned = 0 WHERE userId = ?", [userId], (err) => {
if (err) {
console.error(err)
interaction.reply({
content: "An error occurred while unbanning the user.",
ephemeral: true
})
} else {
embed = {
title: "User unbanned",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0x00ff00,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
},
{
name: "Ban Details",
value: `Reason: ${row.reason}
Timestamp: <t:${Math.floor(new Date(row.timestamp) / 1000)}:R> <t:${Math.floor(new Date(row.timestamp) / 1000)}:f>`
}
]
}
}
})
} else {
interaction.reply({
content: "User is not banned.",
ephemeral: true
})
}
}
})
break;
case "check":
// Check if the user is banned, if they are get their user details with noblox
userId = interaction.options.get("user").value
await interaction.deferReply({ephemeral: true})
userInfo = await noblox.getPlayerInfo(userId)
userThumbnail = await noblox.getPlayerThumbnail([userId], 720, "png", false, "body")
db.get("SELECT * FROM bans WHERE userId = ?", [userId], async (err, row) => {
if (err) {
console.error(err)
interaction.editReply({
content: "An error occurred while checking the user's ban status.",
ephemeral: true
})
} else {
if (row) {
if (row.banned === 1) {
embed = {
title: "User is banned",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0xff0000,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
},
{
name: "Ban Details",
value: `Reason: ${row.reason}
Timestamp: <t:${Math.floor(new Date(row.timestamp) / 1000)}:R> <t:${Math.floor(new Date(row.timestamp) / 1000)}:f>`
}
]
}
console.log(row)
console.log(`<t:${Math.floor(new Date(row.timestamp) / 1000)}:R>`)
interaction.editReply({ embeds: [embed] })
} else {
embed = {
title: "User is not banned",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0x00ff00,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
},
{
name: "Last Ban Details",
value: `Reason: ${row.reason}
Timestamp: <t:${Math.floor(new Date(row.timestamp) / 1000)}:R> <t:${Math.floor(new Date(row.timestamp) / 1000)}:f>`
}
]
}
interaction.editReply({ embeds: [embed] })
}
} else {
embed = {
title: "User is not banned",
thumbnail: {
url: userThumbnail[0].imageUrl
},
color: 0x00ff00,
fields: [
{
name: "User Details",
value: `@${userInfo.username} ${userInfo.displayName} (${userId})
Joined <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:R> <t:${Math.floor(new Date(userInfo.joinDate) / 1000)}:D>
Friends: ${userInfo.friendCount}
Followers: ${userInfo.followerCount}
Following: ${userInfo.followingCount}`
}
]
}
interaction.editReply({ embeds: [embed] })
}
}
})
}
break;
}
});
Client.login(process.env.DISCORD_TOKEN)