Skip to content

Commit

Permalink
Hard-coded the allowed role and server registation for question command
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Jan 4, 2024
1 parent da3cfc6 commit 8f3b49d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 56 deletions.
58 changes: 4 additions & 54 deletions src/commands/info/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SlashCommandBuilder } from 'discord.js';
import { interactionService } from '../../services'
import RabbitMQ, { Event, Queue as RabbitMQQueue } from '@togethercrew.dev/tc-messagebroker';
import { ChatInputCommandInteraction_broker } from '../../interfaces/Hivemind.interface';

import { handleBigInts, removeCircularReferences } from '../../utils/obj';
export default {
data: new SlashCommandBuilder()
.setName('question')
Expand All @@ -14,64 +14,14 @@ export default {
.setRequired(true)),

async execute(interaction: ChatInputCommandInteraction_broker) {
if (!interaction.member?.roles.cache.has("1166350549889859657")) {
// TogetherCrew-Leads: 983364577096003604 TogetherCrew-Contributors: 983364691692748832
if (!(interaction.member?.roles.cache.has("983364577096003604") || interaction.member?.roles.cache.has("983364691692748832"))) {
return await interactionService.createInteractionResponse(interaction, { type: 4, data: { content: 'You do not have the required role to use this command!', flags: 64 } })
}


const serializedInteraction = interactionService.constructSerializableInteraction(interaction);
const processedInteraction = handleBigInts(serializedInteraction);
const cleanInteraction = removeCircularReferences(processedInteraction); // Pass processedInteraction here
const serializedData = JSON.stringify(cleanInteraction, null, 2);
RabbitMQ.publish(RabbitMQQueue.HIVEMIND, Event.HIVEMIND.INTERACTION_CREATED, { interaction: serializedData });
},
};


function handleBigInts(obj: any, seen = new WeakSet()): any {
if (typeof obj === 'bigint') {
return obj.toString(); // Convert BigInt to a string
} else if (Array.isArray(obj)) {
return obj.map((item) => handleBigInts(item, seen)); // Process arrays element-wise
} else if (typeof obj === 'object' && obj !== null) {
if (seen.has(obj)) {
// If we've seen this object before, don't process it again
return;
}
seen.add(obj); // Mark this object as seen

const result: any = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = handleBigInts(value, seen); // Recursively process nested objects
}
return result;
}
return obj; // Return the value unchanged if it's neither an object nor a BigInt
}


function removeCircularReferences(obj: any, parent = null) {
const seenObjects = new WeakMap();

function detect(obj: any, parent: any) {
if (obj && typeof obj === 'object') {
if (seenObjects.has(obj)) {
return '[Circular]';
}
seenObjects.set(obj, true);

Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object' && obj[key] !== null) {
if (parent === obj[key]) {
obj[key] = '[Circular]';
} else {
detect(obj[key], obj);
}
}
});
}
}

detect(obj, parent);
return obj;
}
};
3 changes: 2 additions & 1 deletion src/services/command.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ async function registerCommand() {
const rest = new REST().setToken(config.discord.botToken)
const commandData = [...client.commands.values()].map(command => command.data.toJSON());
await rest.put(
Routes.applicationGuildCommands(config.discord.clientId, "980858613587382322"),
// RnDAO: 915914985140531240
Routes.applicationGuildCommands(config.discord.clientId, "915914985140531240"),
{ body: commandData },
);
logger.info('Commands Registerd');
Expand Down
2 changes: 1 addition & 1 deletion src/services/interaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async function createFollowUpMessage(interaction: ChatInputCommandInteraction_br
throw new Error(await response.json());
}
} catch (error) {
logger.error({ interaction_id: interaction.id, interaction_token: interaction.token, error }, 'Failed to send interaction response');
logger.error({ interaction_id: interaction.id, interaction_token: interaction.token, error }, 'Failed to create followip message');
}
}

Expand Down
47 changes: 47 additions & 0 deletions src/utils/obj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export function handleBigInts(obj: any, seen = new WeakSet()): any {
if (typeof obj === 'bigint') {
return obj.toString(); // Convert BigInt to a string
} else if (Array.isArray(obj)) {
return obj.map((item) => handleBigInts(item, seen)); // Process arrays element-wise
} else if (typeof obj === 'object' && obj !== null) {
if (seen.has(obj)) {
// If we've seen this object before, don't process it again
return;
}
seen.add(obj); // Mark this object as seen

const result: any = {};
for (const [key, value] of Object.entries(obj)) {
result[key] = handleBigInts(value, seen); // Recursively process nested objects
}
return result;
}
return obj; // Return the value unchanged if it's neither an object nor a BigInt
}


export function removeCircularReferences(obj: any, parent = null) {
const seenObjects = new WeakMap();

function detect(obj: any, parent: any) {
if (obj && typeof obj === 'object') {
if (seenObjects.has(obj)) {
return '[Circular]';
}
seenObjects.set(obj, true);

Object.keys(obj).forEach(key => {
if (typeof obj[key] === 'object' && obj[key] !== null) {
if (parent === obj[key]) {
obj[key] = '[Circular]';
} else {
detect(obj[key], obj);
}
}
});
}
}

detect(obj, parent);
return obj;
}

0 comments on commit 8f3b49d

Please sign in to comment.