-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
82 lines (67 loc) · 2.46 KB
/
bot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { Client, GatewayIntentBits } from "discord.js";
import { config } from "dotenv";
import { handleInteraction } from "../copilot/aiHandler.js";
config(); // Load environment variables from .env file
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.DirectMessages,
],
partials: ["CHANNEL"], // Enable partials for direct messages
});
const token = process.env.DISCORD_BOT_TOKEN;
const maxCharSize = 500;
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName, options } = interaction;
// Restrict the bot from responding to direct messages
if (!interaction.channel || interaction.channel.type === "DM") {
await interaction.reply({
content:
"❌ This bot does not handle direct messages. Please use commands in a <#1223003189247217758>.",
allowedMentions: { parse: [] }, // Disable mention parsing to prevent potential security issues
});
return;
}
if (commandName === "fix") {
let userMessage = options.getString("message");
// Input validation and sanitization
if (typeof userMessage !== "string" || userMessage.trim() === "") {
await interaction.reply("❌ Please provide a valid message.");
return;
}
userMessage = userMessage.trim();
if (userMessage.length > maxCharSize) {
await interaction.reply(
`❌ Your message is too long! Please keep it under ${maxCharSize} characters.`
);
return;
}
try {
// Acknowledge the interaction to avoid timing out
await interaction.deferReply();
let aiResponse = await handleInteraction(userMessage);
aiResponse = JSON.parse(aiResponse);
if (aiResponse.correction && aiResponse.explanation) {
await interaction.editReply(
`:pencil: ${userMessage}\n:robot: ${aiResponse.explanation}\n:white_check_mark: ${aiResponse.correction}`
);
} else {
await interaction.editReply(
`:pencil: ${userMessage}\n👌 No issues found.`
);
}
} catch (error) {
console.error("Error handling interaction:", error);
await interaction.editReply(
`:pencil: ${userMessage}\n❌ Sorry, something went wrong while processing your request.`
);
}
}
});
client.login(token);