Skip to content

Commit

Permalink
add sticker command
Browse files Browse the repository at this point in the history
  • Loading branch information
Larsundso committed Nov 21, 2023
1 parent b666be0 commit 0c2d0b1
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/BaseClient/ClientHelperModules/getLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async (guildIDOrLocale: bigint | undefined | null | string) => {
if (!guildIDOrLocale) {
const { default: Lang } = await import('../Other/language.js');

const lang = new Lang('en');
const lang = new Lang('en-GB');
await lang.init();

return lang;
Expand All @@ -30,7 +30,7 @@ export default async (guildIDOrLocale: bigint | undefined | null | string) => {
.then((r) => r?.lan);

const { default: Lang } = await import('../Other/language.js');
const lang = new Lang(lan || 'en');
const lang = new Lang((lan === 'en' ? 'en-GB' : lan) || 'en-GB');
await lang.init();

return lang;
Expand Down
5 changes: 4 additions & 1 deletion src/BaseClient/Other/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ export default {
`https://cdn.discordapp.com/emojis/${emote.id}.${emote.animated ? 'gif' : 'png'}?size=4096`,
getEmote: (
emoji: Discord.Emoji | { name: string; id?: string | null; animated?: boolean | null },
) => (emoji.id ? `<${emoji.animated ? 'a:' : ':'}${emoji.name}:${emoji.id}>` : `${emoji.name}`),
) =>
emoji.id
? `<${emoji.animated ? 'a:' : ':'}${emoji.name}:${emoji.id}>`
: `${/\w/g.test(emoji.name ?? '') ? `:${emoji.name}:` : emoji.name}`,
getTime: (time: number) =>
`<t:${String(time).slice(0, -3)}:f> (<t:${String(time).slice(0, -3)}:R>)`,
msgurl: (g: string | undefined | null, c: string, m: string) =>
Expand Down
1 change: 1 addition & 0 deletions src/BaseClient/Other/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export default class Language {
auditLogAction: { [key in Discord.GuildAuditLogsEntry['action']]: string } = auditLogAction(this);

constructor(type: string | 'en-GB') {
if (type === 'en') throw new Error('EN given');
this.CURRENT_LANGUAGE = type;
}

Expand Down
2 changes: 2 additions & 0 deletions src/BaseClient/Other/language/slashCommands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bot from './info/bot.js';
import user from './info/user.js';
import server from './info/server.js';
import channel from './info/channel.js';
import stickers from './info/stickers.js';

export default (t: CT.Language) => ({
...t.JSON.slashCommands.info,
Expand All @@ -18,4 +19,5 @@ export default (t: CT.Language) => ({
user: user(t),
server: server(t),
channel: channel(t),
stickers: stickers(t),
});
6 changes: 6 additions & 0 deletions src/BaseClient/Other/language/slashCommands/info/stickers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as CT from '../../../../../Typings/CustomTypings.js';

export default (t: CT.Language) => ({
...t.JSON.slashCommands.info.stickers,
author: t.stp(t.JSON.slashCommands.info.stickers.author, { t }),
});
2 changes: 1 addition & 1 deletion src/Commands/ButtonCommands/info/emojis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import * as Discord from 'discord.js';
import emojis from '../../SlashCommands/info/emoji.js';

export default async (cmd: Discord.ButtonInteraction, args: string[]) =>
emojis(cmd, Number(args.shift()));
emojis(cmd, [], Number(args.shift()));
2 changes: 1 addition & 1 deletion src/Commands/ButtonCommands/info/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import * as Discord from 'discord.js';
import servers from '../../SlashCommands/info/servers.js';

export default async (cmd: Discord.ButtonInteraction, args: string[]) =>
servers(cmd, Number(args.shift()));
servers(cmd, [], Number(args.shift()));
5 changes: 5 additions & 0 deletions src/Commands/ButtonCommands/info/stickers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Discord from 'discord.js';
import sticker from '../../SlashCommands/info/sticker.js';

export default async (cmd: Discord.ButtonInteraction, args: string[]) =>
sticker(cmd, [], Number(args.shift()));
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async (cmd: Discord.ChannelSelectMenuInteraction) => {
const lan = language.slashCommands.embedbuilder.send;

await ch.send(channels, {
embeds: [new Discord.EmbedBuilder(cmd.message.embeds[0].data).data],
embeds: [new Discord.EmbedBuilder(cmd.message.embeds[1].data).data],
});

cmd.reply({
Expand Down
1 change: 1 addition & 0 deletions src/Commands/SlashCommands/info/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import client from '../../../BaseClient/Client.js';

export default async (
cmd: Discord.ChatInputCommandInteraction | Discord.ButtonInteraction,
_args: string[],
page?: number,
) => {
if (
Expand Down
1 change: 1 addition & 0 deletions src/Commands/SlashCommands/info/servers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as CT from '../../../Typings/CustomTypings.js';

export default async (
cmd: Discord.ChatInputCommandInteraction | Discord.ButtonInteraction,
_args: string[],
page = 1,
) => {
if (cmd.inGuild() && !cmd.inCachedGuild()) return;
Expand Down
234 changes: 234 additions & 0 deletions src/Commands/SlashCommands/info/sticker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import * as Discord from 'discord.js';
import * as ch from '../../../BaseClient/ClientHelper.js';
import * as CT from '../../../Typings/CustomTypings.js';

export default async (
cmd: Discord.ChatInputCommandInteraction | Discord.ButtonInteraction,
_args: string[],
page?: number,
) => {
if (!cmd.inCachedGuild()) return;

const messageLinkOrStickerID =
cmd instanceof Discord.ChatInputCommandInteraction
? cmd.options.getString('sticker', false)
: undefined;
const language = await ch.getLanguage(cmd.locale);

if (messageLinkOrStickerID) single(cmd as Discord.ChatInputCommandInteraction<'cached'>, language);
else multiple(cmd, language, page);
};

const single = async (
cmd: Discord.ChatInputCommandInteraction<'cached'>,
language: CT.Language,
) => {
let stickerIDs: string[] = [];

const messageLinkOrStickerID = cmd.options.getString('sticker', true);

if (messageLinkOrStickerID.includes('discord.com')) {
const message = await ch.getMessage(messageLinkOrStickerID);
if (!message) {
ch.errorCmd(cmd, language.errors.messageNotFound, language);
return;
}

stickerIDs = message.stickers.map((s) => s.id);
} else {
stickerIDs = [messageLinkOrStickerID];
}

const stickers = (
await Promise.all(stickerIDs.map((s) => cmd.client.fetchSticker(s).catch(() => undefined)))
).filter((s): s is Discord.Sticker => !!s);
if (!stickers.length) {
ch.errorCmd(cmd, language.errors.stickerNotFound, language);
return;
}

ch.replyCmd(cmd, { embeds: await getEmbeds(stickers, cmd, language) });
};

export const multiple = async (
cmd: Discord.ChatInputCommandInteraction<'cached'> | Discord.ButtonInteraction<'cached'>,
language: CT.Language,
page: number = 1,
) => {
const embeds = await getEmbeds(
cmd.guild.stickers.cache.map((s) => s).slice((page - 1) * 10, page * 10),
cmd,
language,
);

const components: Discord.APIActionRowComponent<Discord.APIButtonComponent>[] = [
{
type: Discord.ComponentType.ActionRow,
components: [
{
type: Discord.ComponentType.Button,
style: Discord.ButtonStyle.Secondary,
custom_id: `info/stickers_${page - 1}`,
emoji: ch.emotes.back,
disabled: page === 1,
},
{
type: Discord.ComponentType.Button,
style: Discord.ButtonStyle.Secondary,
disabled: true,
custom_id: '-',
label: `${page}/${Math.ceil(cmd.guild.stickers.cache.size / 10)}`,
},
{
type: Discord.ComponentType.Button,
style: Discord.ButtonStyle.Secondary,
custom_id: `info/stickers_${page + 1}`,
emoji: ch.emotes.forth,
disabled: page === Math.ceil(cmd.guild.stickers.cache.size / 10),
},
],
},
];

if (cmd instanceof Discord.ButtonInteraction) {
cmd.update({ embeds, components });
return;
}

ch.replyCmd(cmd, { embeds, components });
};

const getEmbeds = async (
stickers: Discord.Sticker[],
cmd: Discord.ChatInputCommandInteraction<'cached'> | Discord.ButtonInteraction<'cached'>,
language: CT.Language,
): Promise<Discord.APIEmbed[]> => {
const lan = language.slashCommands.info.stickers;
const packs = stickers.find((s) => s.packId)
? await cmd.client.fetchPremiumStickerPacks()
: undefined;
const color = ch.getColor(cmd.guild ? await ch.getBotMemberFromGuild(cmd.guild) : undefined);

return stickers.map((sticker) => {
const pack = packs?.find((p) => p.id === sticker.packId);
const partialEmoji = sticker.tags ? Discord.resolvePartialEmoji(sticker.tags) : undefined;
const emoji = partialEmoji?.id
? sticker.guild?.emojis.cache.get(partialEmoji.id)
: (partialEmoji as { name: string; animated: false; id: undefined });

return {
color,
author: {
name: lan.author,
},
thumbnail: {
url: sticker.url,
},
description: [
sticker.name
? {
name: `${ch.util.makeBold(language.t.name)}:`,
value: `${ch.util.makeInlineCode(sticker.name)}`,
}
: undefined,
sticker.description
? {
name: `${ch.util.makeBold(language.t.Description)}:`,
value: `${ch.util.makeInlineCode(sticker.description)}`,
}
: undefined,
sticker.id
? {
name: `${ch.util.makeBold('ID')}:`,
value: `${ch.util.makeInlineCode(sticker.id)}`,
}
: undefined,
sticker.url
? {
name: `${ch.util.makeBold('URL')}:`,
value: sticker.url,
}
: undefined,
sticker.createdTimestamp
? {
name: `${ch.util.makeBold(language.t.createdAt)}:`,
value: `${ch.constants.standard.getTime(sticker.createdTimestamp)}`,
}
: undefined,
{
name: `${ch.util.makeBold(language.slashCommands.info.emojis.available)}:`,
value: `${ch.settingsHelpers.embedParsers.boolean(!!sticker.available, language)}`,
},
sticker.guild
? {
name: `\n${ch.util.makeBold(language.t.Server)}:\n`,
value: language.languageFunction.getGuild(sticker.guild),
}
: undefined,
sticker.user
? {
name: `${ch.util.makeBold(language.slashCommands.info.emojis.uploader)}:\n`,
value: language.languageFunction.getUser(sticker.user),
}
: undefined,
sticker.format
? {
name: `${ch.util.makeBold(lan.formatName)}: `,
value: `${ch.util.makeInlineCode(Discord.StickerFormatType[sticker.format])}`,
}
: undefined,
emoji
? {
name: `${ch.util.makeBold(lan.tags)}: `,
value: `${ch.constants.standard.getEmote(emoji)}`,
}
: undefined,
]
.filter((v): v is { name: string; value: string } => !!v)
.map((v) => `${v.name} ${v.value}`)
.join('\n'),
fields: pack
? [
{
name: lan.pack,
value: [
pack.name
? {
name: `${ch.util.makeBold(language.t.name)}:`,
value: `${ch.util.makeInlineCode(pack.name)}`,
}
: undefined,
pack.description
? {
name: `${ch.util.makeBold(language.t.Description)}:`,
value: `${ch.util.makeInlineCode(pack.description)}`,
}
: undefined,
pack.id
? {
name: `${ch.util.makeBold('ID')}:`,
value: `${ch.util.makeInlineCode(pack.id)}`,
}
: undefined,
pack.createdTimestamp
? {
name: `${ch.util.makeBold(language.t.createdAt)}:`,
value: `${ch.constants.standard.getTime(pack.createdTimestamp)}`,
}
: undefined,
]
.filter((v): v is { name: string; value: string } => !!v)
.map((v) => `${v.name} ${v.value}`)
.join('\n'),
},
]
: undefined,
image:
pack && pack.bannerId
? {
url: pack.bannerURL({ size: 4096 }) as string,
}
: undefined,
};
});
};
6 changes: 3 additions & 3 deletions src/Commands/SlashCommands/stickers/create/from-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ export default async (cmd: Discord.ChatInputCommandInteraction<'cached'>) => {

const selectedEmoji = Emojis.find((e) => e === emoji.replace(/:/g, ''));
if (!selectedEmoji) {
ch.errorCmd(cmd, language.t.errors.emoteNotFound, language);
ch.errorCmd(cmd, language.errors.emoteNotFound, language);
return;
}

const message = await ch.getMessage(messageLink);
if (!message) {
ch.errorCmd(cmd, language.t.errors.messageNotFound, language);
ch.errorCmd(cmd, language.errors.messageNotFound, language);
return;
}

const sticker = stickerName
? message.stickers.find((s) => s.name === stickerName || s.id === stickerName)
: message.stickers.first();
if (!sticker) {
ch.errorCmd(cmd, language.t.errors.stickerNotFound, language);
ch.errorCmd(cmd, language.errors.stickerNotFound, language);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SlashCommands/stickers/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default async (cmd: Discord.ChatInputCommandInteraction<'cached'>) => {
(s) => s.id === stickerIDorName || s.name === stickerIDorName,
);
if (!sticker) {
ch.errorCmd(cmd, language.t.errors.stickerNotFound, language);
ch.errorCmd(cmd, language.errors.stickerNotFound, language);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Commands/SlashCommands/stickers/edit/description.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default async (
(s) => s.name === stickerIDorName || s.id === stickerIDorName,
);
if (!sticker) {
ch.errorCmd(cmd, language.t.errors.stickerNotFound, language);
ch.errorCmd(cmd, language.errors.stickerNotFound, language);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Commands/SlashCommands/stickers/edit/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export default async (cmd: Discord.ChatInputCommandInteraction<'cached'>) => {

const selectedEmoji = Emojis.find((e) => e === emoji.replace(/:/g, ''));
if (!selectedEmoji) {
ch.errorCmd(cmd, language.t.errors.emoteNotFound, language);
ch.errorCmd(cmd, language.errors.emoteNotFound, language);
return;
}

const sticker = cmd.guild.stickers.cache.find(
(s) => s.name === stickerIDorName || s.id === stickerIDorName,
);
if (!sticker) {
ch.errorCmd(cmd, language.t.errors.stickerNotFound, language);
ch.errorCmd(cmd, language.errors.stickerNotFound, language);
return;
}

Expand Down
Loading

0 comments on commit 0c2d0b1

Please sign in to comment.