Skip to content

Commit

Permalink
text command based tags
Browse files Browse the repository at this point in the history
Signed-off-by: IThundxr <[email protected]>
  • Loading branch information
IThundxr committed Sep 17, 2023
1 parent a85eba7 commit b32550c
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 83 deletions.
12 changes: 5 additions & 7 deletions src/commands/_commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import { deleteWarningCommand } from './moderation/deletewarning.command';
import { listWarningsCommand } from './moderation/listwarnings.command';
import { warnCommand } from './moderation/warn.command';
import { sayCommand } from './util/say.command';
import { tagCommand } from './util/tag.command';
import { verificationEmbedCommand } from './util/verificationEmbed.command';
import { tagCommand, tagCommandTextBased } from './util/tag.command';

export const commands: Command[] = [
sayCommand,
tagCommand,
verificationEmbedCommand,
];
export const commands: Command[] = [sayCommand, tagCommand];

if (process.env.NODE_ENV !== 'development') {
commands.push(warnCommand, deleteWarningCommand, listWarningsCommand);
}

export default commands;

// Text commands start here
export const textCommands = [tagCommandTextBased];
30 changes: 29 additions & 1 deletion src/commands/util/tag.command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EmbedBuilder, SlashCommandBuilder } from 'discord.js';
import { EmbedBuilder, Message, SlashCommandBuilder } from 'discord.js';
import { getTags, getTagsSync } from '../../handlers/tag.handler';
import { Command } from '../../handlers/command.handler';

Expand Down Expand Up @@ -54,3 +54,31 @@ export const tagCommand: Command = {
});
},
};

export const tagCommandTextBased = async (event: Message<boolean>) => {
const tagName = event.content.split('!!')[1];

const tags = await getTags();

const tag = tags.find(
(tag) => tag.name === tagName || tag.aliases?.includes(tagName)
);

if (!tag) {
await event.channel.send({
content: `Tag \`${tagName}\` does not exist.`,
});
return;
}

const embed = new EmbedBuilder();
embed.setTitle(tag.title ?? tag.name);
embed.setDescription(tag.content);
if (tag.color) embed.setColor(tag.color);
if (tag.image) embed.setImage(tag.image);
if (tag.fields) embed.setFields(tag.fields);

await event.channel.send({
embeds: [embed],
});
};
74 changes: 0 additions & 74 deletions src/commands/util/verificationEmbed.command.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/handlers/textCommand.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { EmbedBuilder, Events } from 'discord.js';
import { Handler } from '..';
import { textCommands } from '../commands/_commands';

const textCommandHandler: Handler = (client) => {
client.on(Events.MessageCreate, async (event) => {
if (!event.content.startsWith('!')) return; // make sure that the interaction came from a command
try {
for (const command of textCommands) command(event); // try execute the command
} catch (error) {
// in case of an error
await event.channel.send({
// send a followup to the interaction
embeds: [
new EmbedBuilder({
color: 0xff2222,
title: 'Internal error',
}),
],
});
return;
}
});
};

export default textCommandHandler;
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { logHandler } from './handlers/log.handler';
import { reloadGlobalSlashCommands } from './handlers/command.handler';
import './webserver';
import { buttonHandler } from './handlers/button.handler';
import textCommandHandler from './handlers/textCommand.handler';

export const client = new Client({
intents: [
Expand Down Expand Up @@ -83,7 +84,12 @@ client.once(Events.ClientReady, async () => {

export type Handler = (client: Client<false>) => void;

const handlers: Handler[] = [commandHandler, logHandler, buttonHandler];
const handlers: Handler[] = [
commandHandler,
textCommandHandler,
logHandler,
buttonHandler,
];

for (const handler of handlers) {
handler(client);
Expand Down

0 comments on commit b32550c

Please sign in to comment.