Compare commits
4 Commits
feature/#1
...
b4e27162aa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4e27162aa | ||
|
|
bfda15866e | ||
|
|
f4475dc9d7 | ||
|
|
c4650a9e99 |
@@ -1,7 +1,7 @@
|
||||
// Modules
|
||||
const { customSlashCommandBuilder } = require('../utilities/customSlashCommandBuilder');
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const { getMembersInRole, getAllClientIds } = require("../utilities/utils");
|
||||
const { getMembersInRole, getAllClientIds, filterAutocompleteValues } = require("../utilities/utils");
|
||||
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
|
||||
const { getOnlineNodes, updateNodeInfo, addNodeConnection, getConnectionByNodeId } = require("../utilities/mysqlHandler");
|
||||
|
||||
@@ -88,15 +88,42 @@ async function joinServerWrapper(presetName, channelId, clientIdsUsed) {
|
||||
exports.joinServerWrapper = joinServerWrapper;
|
||||
|
||||
module.exports = {
|
||||
data: new customSlashCommandBuilder()
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('join')
|
||||
.setDescription('Join the channel you are in with the preset you choose')
|
||||
.addAllSystemPresetOptions(),
|
||||
.addStringOption(option =>
|
||||
option.setName("preset")
|
||||
.setDescription("The preset you would like to listen to")
|
||||
.setAutocomplete(true)
|
||||
.setRequired(true)),
|
||||
example: "join",
|
||||
isPrivileged: false,
|
||||
requiresTokens: false,
|
||||
defaultTokenUsage: 0,
|
||||
deferInitialReply: true,
|
||||
async autocomplete(interaction) {
|
||||
const nodeObjects = await new Promise((recordResolve, recordReject) => {
|
||||
getOnlineNodes((nodeRows) => {
|
||||
recordResolve(nodeRows);
|
||||
});
|
||||
});
|
||||
log.DEBUG("Node objects: ", nodeObjects);
|
||||
var presetsAvailable = [];
|
||||
for (const nodeObject of nodeObjects) {
|
||||
log.DEBUG("Node object: ", nodeObject);
|
||||
for (const presetName in nodeObject.nearbySystems) presetsAvailable.push(nodeObject.nearbySystems[presetName]);
|
||||
}
|
||||
|
||||
log.DEBUG("All Presets available: ", presetsAvailable);
|
||||
|
||||
// Remove duplicates
|
||||
options = [...new Set(presetsAvailable)];
|
||||
log.DEBUG("DeDuped Presets available: ", options);
|
||||
|
||||
// Filter the results to what the user is entering
|
||||
filterAutocompleteValues(interaction, options);
|
||||
|
||||
},
|
||||
async execute(interaction) {
|
||||
try{
|
||||
const guildId = interaction.guild.id;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
// Modules
|
||||
const { customSlashCommandBuilder } = require('../utilities/customSlashCommandBuilder');
|
||||
const { SlashCommandBuilder } = require('discord.js');
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const { getAllClientIds, getKeyByArrayValue } = require("../utilities/utils");
|
||||
const { getAllClientIds, getKeyByArrayValue, filterAutocompleteValues } = require("../utilities/utils");
|
||||
const { requestOptions, sendHttpRequest } = require("../utilities/httpRequests");
|
||||
const { checkNodeConnectionByClientId, removeNodeConnectionByNodeId, updateNodeInfo, getConnectedNodes, getAllConnections } = require('../utilities/mysqlHandler');
|
||||
const { checkNodeConnectionByClientId, removeNodeConnectionByNodeId, getAllConnections } = require('../utilities/mysqlHandler');
|
||||
|
||||
// Global Vars
|
||||
const log = new DebugBuilder("server", "leave");
|
||||
const logAC = new DebugBuilder("server", "leave_autocorrect");
|
||||
|
||||
async function leaveServerWrapper(clientIdObject) {
|
||||
if (!clientIdObject.clientId || !clientIdObject.name) return log.ERROR("Tried to leave server without client ID and/or Name");
|
||||
@@ -34,30 +33,26 @@ async function leaveServerWrapper(clientIdObject) {
|
||||
exports.leaveServerWrapper = leaveServerWrapper;
|
||||
|
||||
module.exports = {
|
||||
data: new customSlashCommandBuilder()
|
||||
data: new SlashCommandBuilder()
|
||||
.setName('leave')
|
||||
.setDescription('Disconnect a bot from the server')
|
||||
.addStringOption(option =>
|
||||
option.setName("bot")
|
||||
.setDescription("The bot to disconnect from the server")
|
||||
.setAutocomplete(true)),
|
||||
.setAutocomplete(true)
|
||||
.setRequired(true)),
|
||||
example: "leave",
|
||||
isPrivileged: false,
|
||||
requiresTokens: false,
|
||||
defaultTokenUsage: 0,
|
||||
deferInitialReply: true,
|
||||
async autocomplete(interaction) {
|
||||
const focusedValue = interaction.options.getFocused();
|
||||
async autocomplete(interaction) {
|
||||
const connections = await getAllConnections();
|
||||
const filtered = connections.filter(conn => String(conn.clientObject.name).startsWith(focusedValue)).map(conn => conn.clientObject.name);
|
||||
logAC.DEBUG("Focused Value: ", focusedValue, connections, filtered);
|
||||
await interaction.respond(
|
||||
filtered.map(option => ({ name: option, value: option })),
|
||||
);
|
||||
const options = connections.map(conn => conn.clientObject.name);
|
||||
await filterAutocompleteValues(interaction, options);
|
||||
},
|
||||
async execute(interaction) {
|
||||
try{
|
||||
const guildId = interaction.guild.id;
|
||||
try{
|
||||
const botName = interaction.options.getString('bot');
|
||||
log.DEBUG("Bot Name: ", botName)
|
||||
const clinetIds = await getAllClientIds();
|
||||
|
||||
@@ -5,7 +5,7 @@ const { FeedStorage, PostStorage } = require("./libStorage");
|
||||
const libUtils = require("./libUtils");
|
||||
const { DebugBuilder } = require("./utilities/debugBuilder");
|
||||
const log = new DebugBuilder("server", "libCore");
|
||||
const mysql = require("mysql");
|
||||
const mysql = require("mysql2");
|
||||
|
||||
const UserAgent = require("user-agents");
|
||||
process.env.USER_AGENT_STRING = new UserAgent({ platform: 'Win32' }).toString();
|
||||
@@ -32,17 +32,38 @@ var runningPostsToRemove = [{
|
||||
}]
|
||||
*/
|
||||
var runningPostsToRemove = {};
|
||||
const sourceFailureLimit = process.env.SOURCE_FAILURE_LIMIT ?? 3;
|
||||
const sourceFailureLimit = process.env.SOURCE_FAILURE_LIMIT ?? 15;
|
||||
|
||||
/**
|
||||
* Wrapper for feeds that cause errors. By default it will wait over a day for the source to come back online before deleting it.
|
||||
*
|
||||
* @param {*} sourceURL
|
||||
* @param {string} sourceURL The URL of the feed source causing issues
|
||||
*/
|
||||
exports.removeSource = function removeSource(sourceURL) {
|
||||
log.INFO("Removing source URL: ", sourceURL);
|
||||
if (!sourceURL in runningPostsToRemove) {runningPostsToRemove[sourceURL] = 1; return;}
|
||||
// Check to see if this is the first time this source has been attempted
|
||||
if (!Object.keys(runningPostsToRemove).includes(sourceURL)) {
|
||||
runningPostsToRemove[sourceURL] = { count: 1, timestamp: Date.now(), ignoredAttempts: 0 };
|
||||
return;
|
||||
}
|
||||
|
||||
const backoffDateTimeDifference = (Date.now() - new Date(runningPostsToRemove[sourceURL].timestamp));
|
||||
const backoffWaitTime = (runningPostsToRemove[sourceURL].count * 30000);
|
||||
|
||||
log.DEBUG("Datetime", runningPostsToRemove[sourceURL], backoffDateTimeDifference, backoffWaitTime);
|
||||
|
||||
// Check to see if the last error occurred within the backoff period or if we should try again
|
||||
if (backoffDateTimeDifference <= backoffWaitTime) {
|
||||
runningPostsToRemove[sourceURL].ignoredAttempts +=1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (runningPostsToRemove[sourceURL] < sourceFailureLimit) {runningPostsToRemove[sourceURL] += 1; return;}
|
||||
// Increase the retry counter
|
||||
if (runningPostsToRemove[sourceURL].count < sourceFailureLimit) {
|
||||
runningPostsToRemove[sourceURL].count += 1;
|
||||
runningPostsToRemove[sourceURL].timestamp = Date.now();
|
||||
return;
|
||||
}
|
||||
|
||||
feedStorage.getRecordBy('link', sourceURL, (err, record) => {
|
||||
if (err) log.ERROR("Error getting record from feedStorage", err);
|
||||
@@ -62,13 +83,14 @@ exports.removeSource = function removeSource(sourceURL) {
|
||||
/**
|
||||
* Unset a source URL from deletion if the source has not already been deleted
|
||||
* @param {*} sourceURL The source URL to be unset from deletion
|
||||
* @returns {*}
|
||||
*/
|
||||
exports.unsetRemoveSource = function unsetRemoveSource(sourceURL) {
|
||||
log.INFO("Unsetting source URL from deletion (if not already deleted): ", sourceURL);
|
||||
if (!sourceURL in runningPostsToRemove) return;
|
||||
if (!Object.keys(runningPostsToRemove).includes(sourceURL)) return;
|
||||
|
||||
if (runningPostsToRemove[sourceURL] > sourceFailureLimit) return delete runningPostsToRemove[sourceURL];
|
||||
delete runningPostsToRemove[sourceURL];
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,7 @@ const { RSSSourceRecord, RSSPostRecord } = require("./utilities/recordHelper");
|
||||
|
||||
// Storage Specific Modules
|
||||
// MySQL
|
||||
const mysql = require("mysql");
|
||||
const mysql = require("mysql2");
|
||||
|
||||
const rssFeedsTable = process.env.DB_RSS_FEEDS_TABLE;
|
||||
const rssPostsTable = process.env.DB_RSS_POSTS_TABLE;
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
"jsdoc": "^4.0.2",
|
||||
"jsonfile": "^6.1.0",
|
||||
"morgan": "^1.10.0",
|
||||
"mysql": "^2.18.1",
|
||||
"mysql2": "^3.3.5",
|
||||
"node-html-markdown": "^1.3.0",
|
||||
"node-html-parser": "^6.1.5",
|
||||
"openai": "^3.2.1",
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
const { SlashCommandBuilder, SlashCommandStringOption } = require('discord.js');
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const { BufferToJson } = require("../utilities/utils");
|
||||
const log = new DebugBuilder("server", "customSlashCommandBuilder");
|
||||
|
||||
const { getAllNodes, getAllNodesSync } = require("../utilities/mysqlHandler");
|
||||
|
||||
exports.customSlashCommandBuilder = class customSlashCommandBuilder extends SlashCommandBuilder {
|
||||
constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
async addAllSystemPresetOptions() {
|
||||
const nodeObjects = await new Promise((recordResolve, recordReject) => {
|
||||
getAllNodes((nodeRows) => {
|
||||
recordResolve(nodeRows);
|
||||
});
|
||||
});
|
||||
log.DEBUG("Node objects: ", nodeObjects);
|
||||
var presetsAvailable = [];
|
||||
for (const nodeObject of nodeObjects) {
|
||||
log.DEBUG("Node object: ", nodeObject);
|
||||
for (const presetName in nodeObject.nearbySystems) presetsAvailable.push(nodeObject.nearbySystems[presetName]);
|
||||
}
|
||||
|
||||
log.DEBUG("All Presets available: ", presetsAvailable);
|
||||
|
||||
// Remove duplicates
|
||||
presetsAvailable = [...new Set(presetsAvailable)];
|
||||
log.DEBUG("DeDuped Presets available: ", presetsAvailable);
|
||||
|
||||
this.addStringOption(option => option.setName("preset").setRequired(true).setDescription("The channels"));
|
||||
for (const preset of presetsAvailable){
|
||||
log.DEBUG("Preset: ", preset);
|
||||
this.options[0].addChoices({
|
||||
'name': String(preset),
|
||||
'value': String(preset)
|
||||
});
|
||||
}
|
||||
log.DEBUG("Preset Options: ", this);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
require('dotenv').config();
|
||||
const mysql = require('mysql');
|
||||
const mysql = require('mysql2');
|
||||
const utils = require('./utils');
|
||||
const { nodeObject, clientObject, connectionObject } = require("./recordHelper");
|
||||
const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
|
||||
@@ -3,6 +3,7 @@ const { DebugBuilder } = require("../utilities/debugBuilder");
|
||||
const { clientObject } = require("./recordHelper");
|
||||
const { readFileSync } = require('fs');
|
||||
const log = new DebugBuilder("server", "utils");
|
||||
const logAC = new DebugBuilder("server", "command-autocorrect");
|
||||
const path = require('path');
|
||||
|
||||
// Convert a JSON object to a buffer for the DB
|
||||
@@ -116,4 +117,20 @@ exports.getClientObjectByClientID = (clientId) => {
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
|
||||
exports.filterAutocompleteValues = async (interaction, options) => {
|
||||
// Get the command used
|
||||
const command = interaction.command;
|
||||
|
||||
// Find values that start with what the user is entering
|
||||
const focusedValue = interaction.options.getFocused();
|
||||
const filtered = options.filter(preset => preset.startsWith(focusedValue));
|
||||
|
||||
// Give the query response to the user
|
||||
logAC.DEBUG("Focused Value: ", command, focusedValue, options, filtered);
|
||||
await interaction.respond(
|
||||
filtered.map(option => ({ name: option, value: option })),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user