Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: conditional rendering of unthanking form elements #954

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions src/v2/autorespond/thanks/createResponse.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { MessageButton } from 'discord.js';
import { MessageActionRow, MessageSelectMenu } from 'discord.js';
import type { User, MessageOptions } from 'discord.js';
import type { User, MessageOptions, Client } from 'discord.js';
import type { EmbedField, Collection } from 'discord.js';

import { clampLength } from '../../utils/clampStr.js';
import { createEmbed } from '../../utils/discordTools.js';

export function createResponse(
thankedUsers: Collection<string, User>,
authorId: string
authorId: string,
client: Client
): MessageOptions {
const title = `Point${thankedUsers.size === 1 ? '' : 's'} received!`;

const description = `<@!${authorId}> has given a point to ${
thankedUsers.size === 1
? `<@!${thankedUsers.first().id}>`
: 'the users mentioned below'
}!`;
const description = `<@!${authorId}> has given a point to ${thankedUsers.size === 1
? `<@!${thankedUsers.first().id}>`
: 'the users mentioned below'
}!`;

const fields: EmbedField[] =
thankedUsers.size > 1
? [...thankedUsers].map(([, u], i) => ({
inline: false,
name: `${(i + 1).toString()}.`,
value: `<@!${u.id}>`,
}))
inline: false,
name: `${(i + 1).toString()}.`,
value: `<@!${u.id}>`,
}))
: [];

const output = createEmbed({
Expand All @@ -36,26 +36,30 @@ export function createResponse(
title,
}).embed;

const clientId = client.user.id;

const components = authorId === clientId ? [
new MessageActionRow().addComponents(
thankedUsers.size > 1
? new MessageSelectMenu()
.setCustomId(`thanks🤔${authorId}🤔select`)
.setPlaceholder('Accidentally Thank someone? Un-thank them here!')
.setMinValues(1)
.setOptions(
thankedUsers.map(user => ({
label: clampLength(user.username, 25),
value: user.id,
}))
)
: new MessageButton()
.setCustomId(`thanks🤔${authorId}🤔${thankedUsers.first().id}`)
.setStyle('SECONDARY')
.setLabel('This was an accident, UNDO!')
),
] : [];

return {
embeds: [output],
components: [
new MessageActionRow().addComponents(
thankedUsers.size > 1
? new MessageSelectMenu()
.setCustomId(`thanks🤔${authorId}🤔select`)
.setPlaceholder('Accidentally Thank someone? Un-thank them here!')
.setMinValues(1)
.setOptions(
thankedUsers.map(user => ({
label: clampLength(user.username, 25),
value: user.id,
}))
)
: new MessageButton()
.setCustomId(`thanks🤔${authorId}🤔${thankedUsers.first().id}`)
.setStyle('SECONDARY')
.setLabel('This was an accident, UNDO!')
),
],
components,
};
}
11 changes: 5 additions & 6 deletions src/v2/autorespond/thanks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ const getReply = async (msg: Message): Promise<undefined | Message> => {
}
};

const handleThanks = async (msg: Message): Promise<void> => {
const handleThanks = async (msg: Message, client: Client): Promise<void> => {
const botId = msg.author.bot;
const reply = await getReply(msg);
if (botId || (msg.mentions.users.size === 0 && !reply)) {
if (
['GUILD_PRIVATE_THREAD','GUILD_PUBLIC_THREAD'].includes(msg.channel.type)
['GUILD_PRIVATE_THREAD', 'GUILD_PUBLIC_THREAD'].includes(msg.channel.type)
) {
await handleThreadThanks(msg);
}
Expand Down Expand Up @@ -118,9 +118,8 @@ const handleThanks = async (msg: Message): Promise<void> => {
value: `<@!${u.id}>\n${diff} minute${diff === 1 ? '' : 's'}.`,
};
}),
footerText: `You can only give a point to a user every ${POINT_LIMITER_IN_MINUTES} minute${
Number.parseInt(POINT_LIMITER_IN_MINUTES) === 1 ? '' : 's'
}.`,
footerText: `You can only give a point to a user every ${POINT_LIMITER_IN_MINUTES} minute${Number.parseInt(POINT_LIMITER_IN_MINUTES) === 1 ? '' : 's'
}.`,
provider: 'spam',
title: 'Cooldown alert!',
}).embed,
Expand All @@ -134,7 +133,7 @@ const handleThanks = async (msg: Message): Promise<void> => {
}

thankableUsers.forEach(async user => pointHandler(user.id, msg));
const msgData = createResponse(thankableUsers, msg.author.id);
const msgData = createResponse(thankableUsers, msg.author.id, client);

const response = await msg.channel.send(msgData);

Expand Down
5 changes: 2 additions & 3 deletions src/v2/autorespond/thanks/threadThanks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ export async function handleThreadThanks(msg: Message): Promise<void> {
content: [
"Hey, it looks like you're trying to thank one or many users, but haven't specified who. Who would you like to thank?",
alreadyThanked.length > 0
? _`There ${_.mapper({ 1: 'is' }, 'are')} **${_.n} user${
_.s
? _`There ${_.mapper({ 1: 'is' }, 'are')} **${_.n} user${_.s
} that you can't thank as you've thanked them recently**, so they won't show up as an option.`(
alreadyThanked.length
)
Expand Down Expand Up @@ -160,7 +159,7 @@ export function attachThreadThanksHandler(client: Client): void {
thankedMembers.map(item => [item.user.id, item.user])
);

const responseData = createResponse(thankedUsers, user.id);
const responseData = createResponse(thankedUsers, user.id, client);
let response: Message;

const msg = await msgPromise;
Expand Down
8 changes: 4 additions & 4 deletions src/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ client.once('ready', async (): Promise<void> => {

try {
await client.user.setAvatar('./logo.png');
} catch {}
} catch { }
});

const detectVarLimited = limitFnByUser(detectVar, {
Expand Down Expand Up @@ -154,17 +154,17 @@ client.on('messageCreate', msg => {
}

if (NON_COMMAND_MSG_TYPES.has(msg.channel.type) && msg.guild) {
handleNonCommandGuildMessages(msg);
handleNonCommandGuildMessages(msg, client);
}
});

const handleNonCommandGuildMessages = async (msg: Message) => {
const handleNonCommandGuildMessages = async (msg: Message, client: Client) => {
const quoteLessContent = stripMarkdownQuote(msg.content);
if (msg.author.bot) {
return;
}
if (isWebdevAndWebDesignServer(msg) && isThanksMessage(quoteLessContent)) {
handleThanks(msg);
handleThanks(msg, client);
}
await detectDeprecatedCommands(msg);
await detectJustAsk(msg);
Expand Down