Untitled
unknown
plain_text
a year ago
3.2 kB
9
Indexable
const fs = require("fs");
const path = require("path");
const discordIRC = require("discord-irc");
const createBot = discordIRC.default;
const { exec } = require("child_process");
const config = require("./config.json");
const mapsDir = path.join(__dirname, "Maps");
let availableMaps = [];
// Read available maps from the Maps folder
fs.readdir(mapsDir, (err, files) => {
if (err) {
console.error("Error reading the Maps directory:", err);
return;
}
availableMaps = files
.filter((file) => file.endsWith(".w3x"))
.map((file) => file.replace(".w3x", ""));
});
function findMapsWithSimilarName(map) {
return availableMaps.filter(
(availableMap) => availableMap.toLowerCase() === map.toLowerCase()
);
}
// Initialize the bot with the configuration
console.log("Bot is starting...");
let bot;
try {
bot = createBot(config);
console.log("Bot object:", bot);
} catch (error) {
console.error("Error initializing bot:", error);
return; // Exit if bot initialization fails
}
if (bot && bot.discord) {
// Discord client ready listener
bot.discord.once("ready", () => {
console.log(`Logged in as ${bot.discord.user.tag}!`);
});
console.log("Bot is running and relaying messages between Discord and IRC");
// Log IRC client object and ensure it's properly initialized
console.log("IRC client:", bot.ircClient);
console.log("Discord client:", bot.discord);
// Handle Discord messages
bot.discord.on("messageCreate", async (message) => {
if (!message.content.startsWith("/asuna") || message.author.bot) {
return;
}
const args = message.content.split(" ").slice(1);
const specificMap = args.join(" ").trim();
if (!specificMap) {
message.reply("Please specify a map!");
return;
}
const matchingMaps = findMapsWithSimilarName(specificMap);
if (matchingMaps.length === 0) {
message.reply("No maps found matching that name.");
return;
}
if (matchingMaps.length > 1) {
message.reply(
`Multiple maps found with the name "${specificMap}". Did you mean one of these? ${matchingMaps.join(
", "
)}`
);
return;
}
const selectedMap = matchingMaps[0]; // There's exactly one matching map
const ircCommand = `PRIVMSG asunabot :!map ${selectedMap}\r\n`;
// Send the command to the IRC client
bot.ircClient.write(ircCommand, (error) => {
if (error) {
console.error(`Error sending command to Asuna: ${error.message}`);
message.reply("Failed to send the command to Asuna.");
return;
}
message.reply(`Command sent to Asuna: !map ${selectedMap}`);
});
});
// Add IRC event listeners to check connection status and errors
bot.ircClient.on("registered", () => {
console.log("IRC client connected and registered.");
});
bot.ircClient.on("error", (error) => {
console.error("IRC connection error:", error);
});
} else {
console.error(
"Bot is not initialized correctly. Check the createBot function."
);
}Editor is loading...
Leave a Comment