Initial upload of discord-contrib-advanced repo
This commit is contained in:
3
discord/lib/bigint-compat.js
Normal file
3
discord/lib/bigint-compat.js
Normal file
@@ -0,0 +1,3 @@
|
||||
BigInt.prototype.toJSON = function () {
|
||||
return this.toString()
|
||||
}
|
||||
66
discord/lib/discordBotManager.js
Normal file
66
discord/lib/discordBotManager.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const { Client, GatewayIntentBits, Partials } = require('discord.js');
|
||||
require('./bigint-compat');
|
||||
|
||||
var bots = new Map();
|
||||
var getBot = function (configNode) {
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var bot = undefined;
|
||||
if (bots.get(configNode) === undefined) {
|
||||
bot = new Client({
|
||||
shards: 'auto',
|
||||
intents: [
|
||||
GatewayIntentBits.Guilds,
|
||||
GatewayIntentBits.GuildMembers,
|
||||
GatewayIntentBits.GuildMessages,
|
||||
GatewayIntentBits.GuildMessageReactions,
|
||||
GatewayIntentBits.DirectMessages,
|
||||
GatewayIntentBits.DirectMessageReactions,
|
||||
GatewayIntentBits.MessageContent
|
||||
],
|
||||
partials: [
|
||||
Partials.Channel,
|
||||
Partials.User,
|
||||
Partials.Message
|
||||
]
|
||||
});
|
||||
bots.set(configNode, bot);
|
||||
bot.token = configNode.token
|
||||
bot.numReferences = (bot.numReferences || 0) + 1;
|
||||
bot.login(configNode.token)
|
||||
.then(() => {
|
||||
return bot.application.fetch(); // Fetch the application to get the ID
|
||||
})
|
||||
.then((app) => {
|
||||
bot.id = app.id; // Set the application ID as a property on the bot object
|
||||
resolve(bot);
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
bot = bots.get(configNode);
|
||||
bot.numReferences = (bot.numReferences || 0) + 1;
|
||||
resolve(bot);
|
||||
}
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
var closeBot = function (bot) {
|
||||
bot.numReferences -= 1;
|
||||
setTimeout(function () {
|
||||
if (bot.numReferences === 0) {
|
||||
try {
|
||||
bot.destroy(); // if a bot is not connected, destroy() won't work, so let's just wrap it in a try-catch..
|
||||
} catch (e) {}
|
||||
for (var i of bots.entries()) {
|
||||
if (i[1] === bot) {
|
||||
bots.delete(i[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}, 1000);
|
||||
};
|
||||
module.exports = {
|
||||
getBot: getBot,
|
||||
closeBot: closeBot
|
||||
}
|
||||
62
discord/lib/discordFramework.js
Normal file
62
discord/lib/discordFramework.js
Normal file
@@ -0,0 +1,62 @@
|
||||
|
||||
const { GuildScheduledEventManager } = require('discord.js');
|
||||
|
||||
const checkIdOrObject = (check) => {
|
||||
try {
|
||||
if (typeof check !== 'string') {
|
||||
if (check.hasOwnProperty('id')) {
|
||||
return check.id;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return check;
|
||||
}
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const getGuild = async (bot, id) => {
|
||||
const guildId = checkIdOrObject(id);
|
||||
if (!guildId) {
|
||||
throw (`msg.guild wasn't set correctly`);
|
||||
}
|
||||
return await bot.guilds.fetch(guildId);
|
||||
}
|
||||
|
||||
const getEventManager = async (bot, id) => {
|
||||
|
||||
guild = await getGuild(bot,id)
|
||||
return new GuildScheduledEventManager(guild);
|
||||
}
|
||||
|
||||
const getChannel = async (bot, id) => {
|
||||
const channelID = checkIdOrObject(id);
|
||||
if (!channelID) {
|
||||
throw (`msg.channel wasn't set correctly`);
|
||||
}
|
||||
return await bot.channels.fetch(channelID);
|
||||
}
|
||||
|
||||
const getMessage = async (bot, channel, message) => {
|
||||
const channelID = checkIdOrObject(channel);
|
||||
const messageID = checkIdOrObject(message);
|
||||
if (!channelID) {
|
||||
throw (`msg.channel wasn't set correctly`);
|
||||
} else if (!messageID) {
|
||||
throw (`msg.message wasn't set correctly`)
|
||||
}
|
||||
|
||||
let channelInstance = await bot.channels.fetch(channelID);
|
||||
return await channelInstance.messages.fetch(messageID);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
checkIdOrObject: checkIdOrObject,
|
||||
getMessage: getMessage,
|
||||
getGuild: getGuild,
|
||||
getChannel: getChannel,
|
||||
getEventManager: getEventManager
|
||||
};
|
||||
16
discord/lib/interactionManager.js
Normal file
16
discord/lib/interactionManager.js
Normal file
@@ -0,0 +1,16 @@
|
||||
let interactions = {};
|
||||
|
||||
const registerInteraction = (interaction) => {
|
||||
interactions[interaction.id]=interaction;
|
||||
}
|
||||
|
||||
const getInteraction = (interactionId) => {
|
||||
let interaction = interactions[interactionId];
|
||||
//delete interactions[interactionId];
|
||||
return interaction;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
registerInteraction: registerInteraction,
|
||||
getInteraction: getInteraction
|
||||
}
|
||||
73
discord/lib/messagesFormatter.js
Normal file
73
discord/lib/messagesFormatter.js
Normal file
@@ -0,0 +1,73 @@
|
||||
const {
|
||||
AttachmentBuilder,
|
||||
ButtonBuilder,
|
||||
ActionRowBuilder,
|
||||
StringSelectMenuBuilder
|
||||
} = require('discord.js');
|
||||
|
||||
const formatAttachments = (inputAttachments) => {
|
||||
let attachments = [];
|
||||
if (inputAttachments) {
|
||||
if (typeof inputAttachments === 'string') {
|
||||
attachments.push(new AttachmentBuilder(inputAttachments));
|
||||
} else if (Array.isArray(inputAttachments)) {
|
||||
inputAttachments.forEach(attachment => {
|
||||
if (typeof attachment === 'string') {
|
||||
attachments.push(new AttachmentBuilder(attachment));
|
||||
} else if (typeof attachment === 'object') {
|
||||
attachments.push(new AttachmentBuilder(attachment.buffer, { name: attachment.name}));
|
||||
}
|
||||
});
|
||||
} else if (typeof inputAttachments === 'object') {
|
||||
attachments.push(new AttachmentBuilder(inputAttachments.buffer, {name: inputAttachments.name}));
|
||||
} else {
|
||||
throw "msg.attachments isn't a string or array";
|
||||
}
|
||||
}
|
||||
return attachments;
|
||||
}
|
||||
|
||||
const formatEmbeds = (inputEmbeds) => {
|
||||
let embeds = [];
|
||||
if (inputEmbeds) {
|
||||
if (Array.isArray(inputEmbeds)) {
|
||||
inputEmbeds.forEach(embed => {
|
||||
embeds.push(embed);
|
||||
});
|
||||
} else if (typeof inputEmbeds === 'object') {
|
||||
embeds.push(inputEmbeds);
|
||||
} else {
|
||||
throw "msg.embeds isn't a string or array";
|
||||
}
|
||||
}
|
||||
return embeds;
|
||||
}
|
||||
|
||||
const formatComponents = (inputComponents) => {
|
||||
let components = [];
|
||||
if (inputComponents) {
|
||||
inputComponents.forEach(component => {
|
||||
if (component.type == 1) {
|
||||
var actionRow = new ActionRowBuilder();
|
||||
component.components.forEach(subComponentData => {
|
||||
switch (subComponentData.type) {
|
||||
case 2:
|
||||
actionRow.addComponents(new ButtonBuilder(subComponentData));
|
||||
break;
|
||||
case 3:
|
||||
actionRow.addComponents(new StringSelectMenuBuilder(subComponentData));
|
||||
break;
|
||||
}
|
||||
});
|
||||
components.push(actionRow);
|
||||
}
|
||||
});
|
||||
}
|
||||
return components;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatComponents: formatComponents,
|
||||
formatAttachments: formatAttachments,
|
||||
formatEmbeds: formatEmbeds
|
||||
}
|
||||
Reference in New Issue
Block a user