Skip to content

Commit

Permalink
[PATCH] fixed permission syncing
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 30, 2023
1 parent ef72d62 commit b1c9897
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 55 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"type": "module",
"version": "2.0.21",
"version": "2.0.22",
"scripts": {
"start": "node --max-old-space-size=8192 ./dist/index.js",
"build": "npx tsc",
Expand Down
1 change: 1 addition & 0 deletions src/BaseClient/Other/language/slashCommands/rp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export default (t: CT.Language) => ({
t.stp(t.JSON.slashCommands.rp.notice, {
cmdId,
}),
willTake: (time: string) => t.stp(t.JSON.slashCommands.rp.willTake, { time }),
});
40 changes: 10 additions & 30 deletions src/Commands/ButtonCommands/rp/sync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import * as Discord from 'discord.js';
import * as Jobs from 'node-schedule';
import client from '../../../BaseClient/Client.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import * as CT from '../../../Typings/Typings.js';
import { getComponents } from '../../SlashCommands/rp/manager.js';
Expand All @@ -23,22 +21,20 @@ export default async (cmd: Discord.ButtonInteraction) => {
return;
}

const rpCmd = (await client.application?.commands.fetch())?.find((c) => c.name === 'rp');
const rpCmd = await ch.getCustomCommand(cmd.guild, 'rp');
if (!rpCmd) {
ch.error(cmd.guild, new Error('RP Command not found'));
return;
}

const perms = (await ch.cache.commandPermissions.get(cmd.guild, rpCmd.id)) ?? [];
const resolved: boolean[] = [];
const promises = (await cmd.guild.commands.fetch()).map((c) =>
ch.request.commands
.editGuildCommandPermissions(c.guild as Discord.Guild, user.accesstoken as string, c.id, {
permissions: perms,
})
.then(() => {
resolved.push(true);
}),
[...(ch.cache.commands.cache.get(cmd.guildId)?.values() ?? [])].map((c) =>
ch.request.commands.editGuildCommandPermissions(
cmd.guild as Discord.Guild,
user.accesstoken as string,
c.id,
{ permissions: perms },
),
);

const guildsettings = await ch.DataBase.guildsettings.update({
Expand All @@ -52,31 +48,15 @@ export default async (cmd: Discord.ButtonInteraction) => {

const embed: Discord.APIEmbed = {
color: CT.Colors.Loading,
description: lan.willTake(ch.constants.standard.getTime(Date.now() + 3600000)),
author: {
name: lan.syncing,
icon_url: ch.emotes.loading.link,
},
};

const message = await cmd.followUp({
await cmd.followUp({
ephemeral: true,
embeds: [embed],
});

const job = Jobs.scheduleJob('*/10 * * * * *', () => {
embed.description = `${lan.synced} ${resolved.length}/${cmd.guild.commands.cache.size}`;

if (promises.length === resolved.length) {
embed.color = CT.Colors.Success;
embed.author = {
name: lan.synced,
icon_url: ch.emotes.tick.link,
};
embed.description = `${lan.synced} ${resolved.length}/${cmd.guild.commands.cache.size}`;
job.cancel();
}

if (message) cmd.editReply({ embeds: [embed], message });
else job.cancel();
});
};
30 changes: 6 additions & 24 deletions src/Events/readyEvents/startupTasks/customBotCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,12 @@ import * as ch from '../../../BaseClient/ClientHelper.js';
export default async () => {
ch.request.commands.getGlobalCommands(undefined, client as Discord.Client<true>);

client.guilds.cache.forEach((g) => {
const globalCommands = async () => {
const commands = await ch.request.commands.getGlobalCommands(g, client as Discord.Client<true>);
client.guilds.cache.forEach(async (g) => {
const settings = await ch.DataBase.guildsettings.findUnique({
where: { guildid: g.id, token: { not: null } },
});
if (!settings?.token) return;

if ('message' in commands) {
ch.error(g, new Error(commands.message));
return;
}

commands.forEach((c) => ch.cache.commands.set(g.id, c.id, c));
};

const guildCommands = async () => {
const commands = await ch.request.commands.getGuildCommands(g);

if ('message' in commands) {
ch.error(g, new Error(commands.message));
return;
}

commands.forEach((c) => ch.cache.commands.set(g.id, c.id, c));
};

globalCommands();
guildCommands();
await ch.request.commands.getGlobalCommands(g, client as Discord.Client<true>);
});
};
1 change: 1 addition & 0 deletions src/Languages/en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,7 @@
"notice": "There is an additional Roleplay-Management-Command called </rp manager:{{cmdId}}>.\nVisit it to find out how to manage Permissions for Roleplay Commands,\nand to figure out how to set up Roleplay Slash-Commands on your Server",
"delay": "This Operation might take a while to finish, please be patient",
"syncing": "Syncing Permissions...",
"willTake": "This will finish {{time}}",
"synced": "Synced"
},
"stp": {
Expand Down

0 comments on commit b1c9897

Please sign in to comment.