Skip to content

Commit

Permalink
related #272
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 10, 2024
1 parent c912d9e commit 6ba20a9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import * as Discord from 'discord.js';
import { API } from '../../../Bot/Client.js';
import * as Classes from '../../../Other/classes.js';
import cache from '../../cache.js';
import error, { sendDebugMessage } from '../../error.js';
import error from '../../error.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';
import { makeRequestHandler } from '../../requestHandler.js';
import requestHandlerError from '../../requestHandlerError.js';
import { canGetCommands } from './getGlobalCommand.js';
import { makeRequestHandler } from '../../requestHandler.js';

/**
* Overwrites all existing global commands for this application in this guild.
Expand Down Expand Up @@ -40,13 +40,6 @@ export default async (
return (cache.apis.get(guild.id) ?? API).applicationCommands
.bulkOverwriteGuildCommands(await getBotIdFromGuild(guild), guild.id, body)
.then((cmds) => {
sendDebugMessage({
content: `Deleting all guild commands in ${guild.name}`,
files: [
guild.client.util.txtFileWriter(JSON.stringify(new Error(), null, 2), undefined, 'temp.json'),
],
});

const parsed = cmds.map(
(cmd) => new Classes.ApplicationCommand(guild.client, cmd, guild, guild.id),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Discord from 'discord.js';
import { API } from '../../../Bot/Client.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';
import cache from '../../cache.js';
import error, { sendDebugMessage } from '../../error.js';
import error from '../../error.js';
import { guild as getBotIdFromGuild } from '../../getBotIdFrom.js';
import { makeRequestHandler } from '../../requestHandler.js';
import requestHandlerError from '../../requestHandlerError.js';
import { canGetCommands } from './getGlobalCommand.js';
import { hasMissingScopes, setHasMissingScopes } from './bulkOverwriteGuildCommands.js';
import { makeRequestHandler } from '../../requestHandler.js';
import { canGetCommands } from './getGlobalCommand.js';

/**
* Deletes a guild command from the Discord API and removes it from the guild's command cache.
Expand Down Expand Up @@ -40,13 +40,6 @@ export default async (guild: Discord.Guild, commandId: string) => {
return (cache.apis.get(guild.id) ?? API).applicationCommands
.deleteGuildCommand(await getBotIdFromGuild(guild), guild.id, commandId)
.then(() => {
sendDebugMessage({
content: `Deleting guild command ${commandId} in ${guild.name}`,
files: [
guild.client.util.txtFileWriter(JSON.stringify(new Error(), null, 2), undefined, 'temp.json'),
],
});

cache.commands.delete(guild.id, commandId);
guild.commands.cache.delete(commandId);
})
Expand Down
44 changes: 34 additions & 10 deletions src/Commands/SlashCommands/settings/custom-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { API } from '@discordjs/core';
import * as Discord from 'discord.js';
import { sendDebugMessage } from '../../../BaseClient/UtilModules/error.js';
import client from '../../../BaseClient/Bot/Client.js';
import Lang from '../../../BaseClient/Other/language.js';
import requestHandler from '../../../BaseClient/UtilModules/requestHandler.js';
Expand Down Expand Up @@ -90,7 +92,9 @@ export const getComponents: CT.SettingsFile<typeof name>['getComponents'] = (
url: settings.token
? client.util.constants.standard.invite.replace(
process.env.mainId ?? '',
settings.appid ?? process.env.mainId ?? '',
(settings.appid ?? settings.token)
? client.util.getBotIdFromToken(settings.token)
: (process.env.mainId ?? ''),
)
: 'https://ayakobot.com',
},
Expand Down Expand Up @@ -139,34 +143,39 @@ const tokenCreate = async (
>,
) => {
const id = guild.client.util.getBotIdFromToken(newSettings.token);

await guild.client.util.DataBase.customclients.update({
where: { guildid: guild.id },
data: { appid: id },
});

requestHandler(guild.id, newSettings.token);

const me = await getMe(guild);
await updateApp(guild, me);
const api = new API(
new Discord.REST({ version: '10', api: 'http://nirn:8080/api' }).setToken(newSettings.token),
);

const me = await api.applications.getCurrent().catch((e: Discord.DiscordAPIError) => e);
await updateApp(api, me, guild);
if (!meIsValid(guild, me)) return;

sendWebhookRequest(guild, me.id);
doCommands(guild, me);
guild.client.util.cache.customClients.delete(guild.id);
};

const updateApp = async (
guild: Discord.Guild,
api: API,
me: Discord.APIApplication | Discord.DiscordAPIError,
guild: Discord.Guild,
) => {
if ('verify_key' in me && me.verify_key) {
await client.util.DataBase.customclients.update({
where: { guildid: guild.id },
data: { publickey: me.verify_key, appid: me.id },
data: { publickey: me.verify_key },
});
}

await client.util.request.applications.editCurrent(guild, {
await api.applications.editCurrent({
interactions_endpoint_url: process.env.customInteractionsEndpointURL,
custom_install_url: process.env.customInstallURL,
install_params: null!,
Expand All @@ -181,8 +190,6 @@ const updateApp = async (
});
};

export const getMe = (guild: Discord.Guild) => client.util.request.applications.getCurrent(guild);

const deleteEntry = (guildId: string) => {
client.util.cache.apis.delete(guildId);

Expand All @@ -193,7 +200,7 @@ const deleteEntry = (guildId: string) => {

const meIsValid = (
guild: Discord.Guild,
me: Awaited<ReturnType<typeof getMe>>,
me: Discord.DiscordAPIError | Discord.APIApplication,
): me is Discord.APIApplication => {
if (!me || 'message' in me) {
client.util.error(guild, new Error(me ? me.message : 'Unknown Application'));
Expand Down Expand Up @@ -281,6 +288,23 @@ export const doCommands = async (guild: Discord.Guild, me: Discord.APIApplicatio

await client.util.request.commands.bulkOverwriteGuildCommands(guild, [...existingCommands]);

const debugErr = new Error();

sendDebugMessage({
content: `Deleting all guild commands in ${guild.name}`,
files: [
guild.client.util.txtFileWriter(
JSON.stringify(
{ name: debugErr.name, message: debugErr.message, stack: debugErr.stack },
null,
2,
),
undefined,
'temp',
),
],
});

const settings = await client.util.DataBase.guildsettings.findUnique({
where: { guildid: guild.id },
select: { enabledrp: true },
Expand Down
5 changes: 3 additions & 2 deletions src/Events/BotEvents/guildEvents/guildMemberAdd/ccJoin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { GuildMember } from 'discord.js';
import requestHandler from '../../../../BaseClient/UtilModules/requestHandler.js';
import { doCommands, getMe } from '../../../../Commands/SlashCommands/settings/custom-client.js';
import { doCommands } from '../../../../Commands/SlashCommands/settings/custom-client.js';

export default async (member: GuildMember) => {
if (!member.user.bot) return;
if (Number(member?.joinedTimestamp) < Date.now() - 30000) return;

const cc = await member.client.util.DataBase.customclients.findUnique({
where: { guildid: member.guild.id, appid: member.user.id, token: { not: null } },
Expand All @@ -14,7 +15,7 @@ export default async (member: GuildMember) => {
const apiCreated = await requestHandler(member.guild.id, cc.token);
if (!apiCreated) return;

const me = await getMe(member.guild);
const me = await member.client.util.request.applications.getCurrent(member.guild);
if ('message' in me) return;

doCommands(member.guild, me);
Expand Down

0 comments on commit 6ba20a9

Please sign in to comment.