Add server history autocomplete
This commit is contained in:
parent
2afb0656a0
commit
1c6ab2a0bf
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -128,3 +128,4 @@ dist
|
|||
.yarn/build-state.yml
|
||||
.yarn/install-state.gz
|
||||
.pnp.*
|
||||
history.json
|
|
@ -7,7 +7,10 @@ const checkSrv = (hostname) => {
|
|||
hostname = `_minecraft._tcp.${hostname}`;
|
||||
return new Promise((resolve, reject) => {
|
||||
dns.resolveSrv(hostname, (err, addresses) => {
|
||||
if (err) reject(err);
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
if (!addresses || addresses.length <= 0) return reject("No SRV record found.");
|
||||
resolve(addresses[0]);
|
||||
});
|
||||
});
|
||||
|
|
47
index.js
47
index.js
|
@ -2,6 +2,7 @@ const findMc = require("./findMc");
|
|||
const colors = require("colors");
|
||||
require("dotenv").config();
|
||||
const Discord = require("discord.js");
|
||||
const fs = require("fs");
|
||||
const client = new Discord.Client({ intents: ["Guilds"] });
|
||||
|
||||
unavailEmote = "<:unreachable:1255354739382816798>"
|
||||
|
@ -27,7 +28,8 @@ const commands = [{
|
|||
name: "address",
|
||||
description: "The address of the server",
|
||||
type: 3,
|
||||
required: true
|
||||
required: true,
|
||||
autocomplete: true
|
||||
}],
|
||||
integration_types: [0, 1],
|
||||
contexts: [0, 1, 2]
|
||||
|
@ -65,13 +67,40 @@ client.on('ready', () => {
|
|||
})();
|
||||
})
|
||||
|
||||
// Try reading the history file, if it doesnt exist or is invalid, set addressHistory to an empty object.
|
||||
let addressHistory = {};
|
||||
try {
|
||||
addressHistory = JSON.parse(fs.readFileSync("./history.json"));
|
||||
} catch (err) {
|
||||
addressHistory = {};
|
||||
}
|
||||
// addressHistory[userid] = [address1, address2, address3, address4, address5]. Addresses never repeat, move them to the front of the array when they are used. Max of 10 addresses per user.
|
||||
|
||||
const doAddressHistory = (userid, address) => {
|
||||
if (!addressHistory[userid]) {
|
||||
addressHistory[userid] = [address];
|
||||
fs.writeFileSync("./history.json", JSON.stringify(addressHistory));
|
||||
return;
|
||||
}
|
||||
if (addressHistory[userid].length >= 10) {
|
||||
addressHistory[userid].pop();
|
||||
}
|
||||
// Check if the address is already in the array, if it is, remove it.
|
||||
if (addressHistory[userid].includes(address)) {
|
||||
addressHistory[userid].splice(addressHistory[userid].indexOf(address), 1);
|
||||
}
|
||||
addressHistory[userid].unshift(address);
|
||||
// Save history to file for future use
|
||||
fs.writeFileSync("./history.json", JSON.stringify(addressHistory));
|
||||
}
|
||||
|
||||
client.on('interactionCreate', async interaction => {
|
||||
if (!interaction.isCommand()) return;
|
||||
console.log(`${colors.cyan("[INFO]")} Command ${colors.green(interaction.commandName)} executed by ${colors.green(interaction.user.displayName)}`);
|
||||
if (interaction.isCommand()) {
|
||||
switch (interaction.commandName) {
|
||||
case "check":
|
||||
await interaction.deferReply();
|
||||
const address = interaction.options.getString("address");
|
||||
doAddressHistory(interaction.user.id, address);
|
||||
findMc(address, 5000).then((data) => {
|
||||
data.description = filterMOTD(data.description);
|
||||
msgData = {
|
||||
|
@ -127,6 +156,18 @@ client.on('interactionCreate', async interaction => {
|
|||
})
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(interaction.isAutocomplete()) {
|
||||
// Send their history as options
|
||||
const history = addressHistory[interaction.user.id] || [];
|
||||
// Map the history to options
|
||||
interaction.respond(history.map((address) => {
|
||||
return {
|
||||
name: address,
|
||||
value: address
|
||||
}
|
||||
}));
|
||||
}
|
||||
});
|
||||
|
||||
client.login(process.env.TOKEN);
|
Loading…
Reference in a new issue