Skip to content

Commit

Permalink
unknown command, did you mean x ?
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Dec 16, 2023
1 parent 18e0ee1 commit cff97cb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/BaseClient/Other/language/slashCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export default (t: CT.Language) => ({
info: info(t),
settings: settings(t),
giveaway: giveaway(t),
useSlashCommands: (c: string) => t.stp(t.JSON.slashCommands.useSlashCommands, { c }),
});
42 changes: 40 additions & 2 deletions src/Events/messageEvents/messageCreate/commandHandler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as stringSimilarity from 'string-similarity';
import * as Jobs from 'node-schedule';
import * as Discord from 'discord.js';
import { glob } from 'glob';
Expand All @@ -23,7 +24,8 @@ const dmCommand = async (msg: Discord.Message) => {
.trim()
.split(/\s+|\n+/g);

const command = await getComand(args.shift()?.toLowerCase() as string);
const commandName = args.shift()?.toLowerCase() as string;
const command = await getComand(commandName);
if (!command) return;
if (!command.dmAllowed) return;

Expand All @@ -44,7 +46,40 @@ const guildCommand = async (msg: Discord.Message<true>) => {
if (!commandName) return;

const command = await getComand(commandName);
if (!command) return;
if (!command) {
const allSlashCommands = (await glob(`${process.cwd()}/Commands/SlashCommands/**/*`))
.filter((f) => f.endsWith('.js') && !f.endsWith('.map.js'))
.map((f) => f.replace(`${process.cwd()}/Commands/SlashCommands/`, '').replace('.js', ''))
.filter((f) => !f.includes('/'));

const slashCommand = await getSlashCommand(
msg.guild,
stringSimilarity.findBestMatch(commandName, allSlashCommands).bestMatch.target,
);
if (!slashCommand) return;

const canUse = checkCommandPermissions(msg, commandName);
if (!canUse) {
const reaction = await ch.request.channels.addReaction(
msg,
ch.constants.standard.getEmoteIdentifier(ch.emotes.cross),
);

if (typeof reaction !== 'undefined') ch.error(msg.guild, new Error(reaction.message));
return;
}

const language = await ch.getLanguage(msg.author.id);
const embed: Discord.APIEmbed = {
description: language.slashCommands.useSlashCommands(
`</${slashCommand.name}:${slashCommand.id}>`,
),
color: ch.constants.colors.ephemeral,
};

ch.replyMsg(msg, { embeds: [embed] });
return;
}

if (command.thisGuildOnly?.length && command.thisGuildOnly?.includes(msg.guildId)) return;
if (command.type === 'owner' && msg.author.id !== auth.ownerID) return;
Expand Down Expand Up @@ -132,6 +167,9 @@ const getComand = async (commandName: string) => {
return (await import(path)) as CT.Command<boolean>;
};

const getSlashCommand = (guild: Discord.Guild, commandName: string) =>
ch.getCustomCommand(guild, commandName as Parameters<typeof ch.getCustomCommand>[1]);

export const checkCommandPermissions = (
msg: {
guildId: string;
Expand Down
5 changes: 3 additions & 2 deletions src/Languages/en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,7 @@
"antirad": "{{t.botName}} Anti-Raid"
},
"slashCommands": {
"useSlashCommands": "Unknown Command. Did you mean {{c}}?",
"setLevel": {
"reset": "Reset",
"save": "Save and Apply",
Expand All @@ -1380,8 +1381,8 @@
"areYouSure2": "Are you sure you want to reset all Levels?\n**This cannot be un-done!**",
"all": "All Levels have been reset",
"areYouSure": "Are you sure you want to reset all Levels?\n**This cannot be un-done!**\n__Notice__: This will not un-assign Level-Roles\n\nThe Buttons will unlock {{t}}",
"confirmUser": "Confirm Reset of Levels of\n{{t.languageFunction.getUser(user)}}",
"confirmRole": "Confirm Reset of Levels of all {{amount}} Members in\n{{t.languageFunction.getRole(role)}}",
"confirmUser": "Confirm Reset of Levels of\n{{user}}",
"confirmRole": "Confirm Reset of Levels of all {{amount}} Members in\n{{role}}",
"user": "The Levels of\n{{user}}have been reset",
"role": "The Levels of all {{amount}} Members in\n{{role)}}have been reset"
},
Expand Down

0 comments on commit cff97cb

Please sign in to comment.