Bugfix to slash commands

This commit is contained in:
Erik 2023-12-05 21:41:24 +02:00
parent c860861d27
commit b36ec44bb2
7 changed files with 20 additions and 23 deletions

View File

@ -239,7 +239,6 @@ class DiscordClient extends Client
this.#built = true; this.#built = true;
this.emit('built'); this.emit('built');
return this; return this;
} }
async destroy () async destroy ()

View File

@ -35,9 +35,10 @@ class Intercom
throw new Error('Missing client application'); throw new Error('Missing client application');
const clientId = this.#client.application.id; const clientId = this.#client.application.id;
const commands = this.#client.registry const commands = this.#client.registry
.filter<SlashCommand>((c: SlashCommand) => c.type === 'command' && c.slash) .filter((c: SlashCommand) => c.type === 'command' && c.slash)
.map((c) => c.shape); .map((c) => c.shape);
// console.log(inspect(commands, { depth: 25 }));
if (process.env.NODE_ENV === 'development') if (process.env.NODE_ENV === 'development')
return this.send('commands', { type: 'guild', commands, clientId }); return this.send('commands', { type: 'guild', commands, clientId });

View File

@ -142,12 +142,14 @@ export default class AutoModeration extends Observer implements Initialisable
{ {
const { guildWrapper: guild, author } = message; const { guildWrapper: guild, author } = message;
let channel = message.channel as TextChannel; let channel = message.channel as TextChannel;
const member = await guild.memberWrapper(message.author);
if (channel.partial) if (channel.partial)
channel = await channel.fetch(); channel = await channel.fetch();
if (!guild || author.bot || message.filtered || !member) if (!guild || author.bot || message.filtered)
return; return;
const member = await guild.memberWrapper(message.author);
if (!member)
return;
const settings = await guild.settings(); const settings = await guild.settings();
const { wordfilter: setting } = settings; const { wordfilter: setting } = settings;
const { bypass, ignore, enabled, silent, explicit, fuzzy, regex, whitelist, actions } = setting; const { bypass, ignore, enabled, silent, explicit, fuzzy, regex, whitelist, actions } = setting;

View File

@ -504,11 +504,12 @@ class GuildWrapper
async memberWrapper (user: UserResolveable) async memberWrapper (user: UserResolveable)
{ {
const id = Util.hasId(user) ? user.id : user; const id = Util.hasId(user) ? user.id : user;
const member = await this.resolveMember(id); const member = user instanceof GuildMember ? user : await this.resolveMember(id);
if (!member) if (!member)
return Promise.reject(new Error('No member found')); // return Promise.reject(new Error('No member found'));
return null;
if (this.#memberWrappers.has(member.id)) if (this.#memberWrappers.has(member.id))
return this.#memberWrappers.get(member.id); return this.#memberWrappers.get(member.id)!;
const wrapper = new MemberWrapper(this.#client, member, this); const wrapper = new MemberWrapper(this.#client, member, this);
this.#memberWrappers.set(wrapper.id, wrapper); this.#memberWrappers.set(wrapper.id, wrapper);

View File

@ -365,7 +365,10 @@ abstract class Command extends Component
} }
if (!(opt.name instanceof Array)) if (!(opt.name instanceof Array))
{
this.#options.push(new CommandOption({ ...opt, client: this.client })); this.#options.push(new CommandOption({ ...opt, client: this.client }));
continue;
}
// Allows easy templating of subcommands that share arguments // Allows easy templating of subcommands that share arguments
const { name: names, description, type, ...opts } = opt; const { name: names, description, type, ...opts } = opt;

View File

@ -2,7 +2,7 @@ import { EventEmitter } from 'node:events';
import { inspect } from 'node:util'; import { inspect } from 'node:util';
import path from 'node:path'; import path from 'node:path';
import { IPCMessage } from '../../@types/Shared.js'; import { CommandsDef, IPCMessage } from '../../@types/Shared.js';
import { BroadcastEvalOptions, ShardMethod, ShardingOptions } from '../../@types/Shard.js'; import { BroadcastEvalOptions, ShardMethod, ShardingOptions } from '../../@types/Shard.js';
import { ControllerOptions } from '../../@types/Controller.js'; import { ControllerOptions } from '../../@types/Controller.js';
@ -235,7 +235,7 @@ class Controller extends EventEmitter
if (message._mEval) if (message._mEval)
return this.eval(shard, { script: message._mEval, debug: message.debug || false }); return this.eval(shard, { script: message._mEval, debug: message.debug || false });
if (message._commands) if (message._commands)
return this.#slashCommandManager._handleMessage(message._commands); return this.#slashCommandManager._handleMessage(message as CommandsDef);
if (message._api) if (message._api)
return this.apiRequest(shard, message); return this.apiRequest(shard, message);
} }

View File

@ -1,10 +1,3 @@
// const { REST } = require('@discordjs/rest');
// const { Routes } = require('discord-api-types/v9');
// const fs = require('fs');
// const path = require('path');
// const hash = require('object-hash');
// const { inspect } = require('util');
import { REST } from '@discordjs/rest'; import { REST } from '@discordjs/rest';
import { Routes } from 'discord-api-types/v9'; import { Routes } from 'discord-api-types/v9';
import hash from 'object-hash'; import hash from 'object-hash';
@ -57,15 +50,12 @@ class SlashCommandManager
async _handleMessage ({ commands, guilds, clientId, type }: CommandsDef) async _handleMessage ({ commands, guilds, clientId, type }: CommandsDef)
{ {
fs.writeFileSync('./commands.json', JSON.stringify(commands, null, 4), { encoding: 'utf-8' });
if (type === 'global') if (type === 'global')
{
await this.#global(commands, clientId); await this.#global(commands, clientId);
}
else if (type === 'guild') else if (type === 'guild')
{
await this.#guild(commands, { guilds, clientId }); await this.#guild(commands, { guilds, clientId });
} }
}
async #guild (commands: Command[], { guilds = [], clientId }: { guilds: string[], clientId: string }) async #guild (commands: Command[], { guilds = [], clientId }: { guilds: string[], clientId: string })
{ {
@ -126,6 +116,7 @@ class SlashCommandManager
#parseError (error: ClusterFuck, commands: Command[]) #parseError (error: ClusterFuck, commands: Command[])
{ {
// console.log(inspect(error, { depth: 25 }));
this.#client.logger.error(`An issue has occured while updating guild commands. Guild command refresh aborted.\n${error.stack || error}`); this.#client.logger.error(`An issue has occured while updating guild commands. Guild command refresh aborted.\n${error.stack || error}`);
// Figures out which command and option ran into issues // Figures out which command and option ran into issues
const invalid = error.rawError.errors; const invalid = error.rawError.errors;