Files
.gitignore
index.js
package.json
Code
3 757
index.js
require("dotenv").config();
require("dotenv").config({ path: ".env.txt" });
const {
Client,
GatewayIntentBits,
REST,
Routes,
SlashCommandBuilder,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
ModalBuilder,
TextInputBuilder,
TextInputStyle,
EmbedBuilder,
PermissionsBitField,
} = require("discord.js");
const config = {
groupId: process.env.ROBLOX_GROUP_ID,
guildId: process.env.GUILD_ID?.trim(),
clientId: process.env.CLIENT_ID?.trim(),
};
const token = process.env.DISCORD_TOKEN?.trim();
const requiredEnvironmentVariables = [
"DISCORD_TOKEN",
"CLIENT_ID",
"GUILD_ID",
];
const missingEnvironmentVariables = requiredEnvironmentVariables.filter(
(name) => !process.env[name]?.trim()
);
if (missingEnvironmentVariables.length) {
console.error(
`Missing required environment variables: ${missingEnvironmentVariables.join(", ")}`
);
process.exit(1);
}
const client = new Client({
intents: [GatewayIntentBits.Guilds],
});
const logChannelId = process.env.LOG_CHANNEL_ID?.trim();
if (!logChannelId) {
console.warn("LOG_CHANNEL_ID is not set; verification logs are disabled.");
}
function safeLogValue(value, maxLength = 1024) {
let text = String(value ?? "ไม่ระบุ");
if (token) {
text = text.split(token).join("[REDACTED]");
}
return text.replace(/[\u0000-\u001f\u007f]/g, " ").slice(0, maxLength) || "ไม่ระบุ";
}
function getErrorReason(error) {
const code = error?.code ?? error?.status;
const message = safeLogValue(error?.message ?? error, 700);
return `${code ? `รหัส ${code}: ` : ""}${message}`;
}
async function logVerification(interaction, {
status,
title,
description,
robloxUsername,
robloxUserId,
rankNumber,
rankName,
extraFields = [],
}) {
if (!logChannelId) return;
try {
const channel = await client.channels.fetch(logChannelId);
if (
!channel ||
!channel.guild ||
channel.guild.id !== interaction.guildId ||
!channel.isTextBased() ||
typeof channel.permissionsFor !== "function"
) {
console.warn("Verification log channel is unavailable or is not a text channel in the verification server.");
return;
}
const botMember = await channel.guild.members.fetchMe();
const permissions = channel.permissionsFor(botMember);
const missingPermissions = [];
if (!permissions?.has(PermissionsBitField.Flags.ViewChannel)) {
missingPermissions.push("View Channel");
}
if (!permissions?.has(PermissionsBitField.Flags.SendMessages)) {
missingPermissions.push("Send Messages");
}
if (missingPermissions.length) {
console.warn(
`Verification log channel is missing bot permissions: ${missingPermissions.join(", ")}`
);
return;
}
const fields = [
{ name: "Discord Username", value: safeLogValue(interaction.user.username), inline: true },
{ name: "Discord User ID", value: safeLogValue(interaction.user.id), inline: true },
];
if (robloxUsername != null) {
fields.push({ name: "Roblox Username", value: safeLogValue(robloxUsername), inline: true });
}
if (robloxUserId != null) {
fields.push({ name: "Roblox User ID", value: safeLogValue(robloxUserId), inline: true });
}
if (rankNumber != null) {
fields.push({ name: "Roblox Group Rank Number", value: safeLogValue(rankNumber), inline: true });
}
if (rankName != null) {
fields.push({ name: "Roblox Group Rank Name", value: safeLogValue(rankName), inline: true });
}
fields.push(...extraFields.map((field) => ({
name: safeLogValue(field.name, 256),
value: safeLogValue(field.value, 1024),
inline: Boolean(field.inline),
})));
const colors = {
success: 0x2ecc71,
failure: 0xe74c3c,
warning: 0xf1c40f,
};
const embed = new EmbedBuilder()
.setColor(colors[status] ?? colors.warning)
.setTitle(safeLogValue(title, 256))
.addFields(fields)
.setTimestamp();
if (description) {
embed.setDescription(safeLogValue(description, 4096));
}
await channel.send({ embeds: [embed] });
} catch (error) {
console.warn("Could not send verification log:", getErrorReason(error));
}
}
const rankRoles = [
{
min: 1,
max: 5,
title: "ชั้นประทวน",
roleId: process.env.ROLE_TROOP,
},
{
min: 6,
max: 10,
title: "นายร้อย",
roleId: process.env.ROLE_LIEUTENANT,
},
{
min: 11,
max: 15,
title: "นายพัน",
roleId: process.env.ROLE_COLONEL,
},
{
min: 16,
max: 19,
title: "นายพล",
roleId: process.env.ROLE_GENERAL,
},
{
min: 20,
max: 255,
title: "จอมพล",
roleId: process.env.ROLE_MARSHAL,
},
];
const allRoleIds = rankRoles.map((r) => r.roleId);
function getRankConfig(rank) {
return rankRoles.find(
(r) => rank >= r.min && rank <= r.max
);
}