Skip to content

Commit

Permalink
Merge pull request #619 from commons-stack/chore/525/update-to-discor…
Browse files Browse the repository at this point in the history
…d.jsv14

Update to discord.js v14
  • Loading branch information
kristoferlund authored Oct 7, 2022
2 parents 04932aa + 6e6e5e5 commit 22a8ba7
Show file tree
Hide file tree
Showing 51 changed files with 477 additions and 285 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed .yarn/cache/zod-npm-3.17.3-2f3f0c3086-f74b5d8a30.zip
Binary file not shown.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

### Changed

- **Discord-bot**: Updated the bot to use the new `discord.js` version 14 #619
- **Discord-bot**: Minor refactors to make code easier to read #619

### Fixed

- **Devops:**: `database-restore.sh` did not work under certain circumstances. Script have now been upgraded.
- **Discord-bot**: Praising does not work in threads #524 #619

## [0.12.2] - 2022-09-28

Expand Down
8 changes: 4 additions & 4 deletions packages/discord-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"license": "GPL-3.0-or-later",
"description": "The Praise Discord bot is the main way for users to interact with the Praise system.",
"dependencies": {
"@discordjs/builders": "^0.12.0",
"@discordjs/rest": "^0.3.0",
"@discordjs/builders": "^1.2.0",
"@discordjs/rest": "^1.2.0",
"@types/node": "^17.0.23",
"api": "*",
"date-fns": "^2.28.0",
"discord-api-types": "^0.26.1",
"discord.js": "^13.6.0",
"discord-api-types": "^0.37.11",
"discord.js": "^14.5.0",
"env-cmd": "^10.1.0",
"jet-logger": "^1.1.5",
"mongoose": "^6.2.0",
Expand Down
3 changes: 1 addition & 2 deletions packages/discord-bot/src/commands/activate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const activate: Command = {

help: {
name: 'activate',
text: 'Command to activate praise for discord account. You need to open the link returned by the command and sign a message with your eth wallet to link and activate your discord account with your eth wallet address.\n\
**Usage**: `/activate`\n',
text: 'Command to activate praise for discord account. You need to open the link returned by the command and sign a message with your eth wallet to link and activate your discord account with your eth wallet address.\n**Usage**: `/activate`\n',
},
};
3 changes: 1 addition & 2 deletions packages/discord-bot/src/commands/forward.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const forward: Command = {
},
help: {
name: 'forward',
text: 'Command to forward praise from a giver to receivers in the discord server. You need to have an activated account on the Praise System along with FORWARDER role permissions to use this command.\n\
**Usage**: `/forward giver: <@userA> receivers: <@user1 @user2 ...> reason: for something`\n',
text: 'Command to forward praise from a giver to receivers in the discord server. You need to have an activated account on the Praise System along with FORWARDER role permissions to use this command.\n**Usage**: `/forward giver: <@userA> receivers: <@user1 @user2 ...> reason: for something`\n',
},
};
5 changes: 1 addition & 4 deletions packages/discord-bot/src/commands/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import { helpHandler } from '../handlers/help';
import { HelpCommandBuilder, Command } from '../interfaces/Command';

export const help: HelpCommandBuilder = (commands) => {
const commandNames: [name: string, value: string][] = Array.from(
commands
).map((i) => [i[0], i[0]]);
return {
help: {
data: new SlashCommandBuilder()
.setName('help')
.setDescription('Shows help text for praise')
.addStringOption((option) =>
option
.addChoices(commandNames)
.setName('command')
.setDescription('The command you want to look up')
.setAutocomplete(true)
.setRequired(false)
),

Expand Down
3 changes: 1 addition & 2 deletions packages/discord-bot/src/commands/praise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export const praise: Command = {

help: {
name: 'praise',
text: 'Command to praise users in the discord server. You need to have an activated account on the Praise System to use this command.\n\
**Usage**: `/praise receivers: <@user1 @user2 ...> reason: for something`\n',
text: 'Command to praise users in the discord server. You need to have an activated account on the Praise System to use this command.\n**Usage**: `/praise receivers: <@user1 @user2 ...> reason: for something`\n',
},
};
6 changes: 2 additions & 4 deletions packages/discord-bot/src/commands/praiseAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,11 @@ export const praiseAdmin: Command = {

help: {
name: 'admin',
text: 'Command to perform admin actions in the Praise system and the PraiseBot\n\
**Usage**: `/admin <announce|...>`\n',
text: 'Command to perform admin actions in the Praise system and the PraiseBot\n**Usage**: `/admin <announce|...>`\n',
subCommands: [
{
name: 'announce',
text: 'Command to publish announcements that are distributed as direct messages to Praise users.\n\
Usage: `/admin announce message: <you have been selected for quantification...>`',
text: 'Command to publish announcements that are distributed as direct messages to Praise users.\nUsage: `/admin announce message: <you have been selected for quantification...>`',
},
],
},
Expand Down
8 changes: 4 additions & 4 deletions packages/discord-bot/src/handlers/activate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { UserAccountModel } from 'api/dist/useraccount/entities';
import { UserAccount } from 'api/src/useraccount/types';
import { EventLogTypeKey } from 'api/src/eventlog/types';
import { logEvent } from 'api/src/eventlog/utils';
import { UserAccount } from 'api/dist/useraccount/types';
import { EventLogTypeKey } from 'api/dist/eventlog/types';
import { logEvent } from 'api/dist/eventlog/utils';
import randomstring from 'randomstring';
import { CommandHandler } from 'src/interfaces/CommandHandler';
import { alreadyActivatedError } from '../utils/praiseEmbeds';
import { alreadyActivatedError } from '../utils/embeds/praiseEmbeds';

/**
* Executes command /activate
Expand Down
26 changes: 16 additions & 10 deletions packages/discord-bot/src/handlers/announce.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { UserAccountModel } from 'api/dist/useraccount/entities';
import { UserAccount } from 'api/src/useraccount/types';
import { UserAccount } from 'api/dist/useraccount/types';
import { UserModel } from 'api/dist/user/entities';
import { Message, SelectMenuInteraction, MessageActionRow } from 'discord.js';
import {
SelectMenuInteraction,
ActionRowBuilder,
ButtonBuilder,
SelectMenuBuilder,
} from 'discord.js';
import { UserRole } from 'api/dist/user/types';
import { PeriodModel } from 'api/dist/period/entities';
import { CommandHandler } from 'src/interfaces/CommandHandler';
Expand All @@ -12,7 +17,7 @@ import {
import { dmTargetMenu } from '../utils/menus/dmTargetmenu';
import { selectTargets } from '../utils/dmTargets';
import { periodSelectMenu } from '../utils/menus/periodSelectMenu';
import { notActivatedError } from '../utils/praiseEmbeds';
import { notActivatedError } from '../utils/embeds/praiseEmbeds';

/**
* Executes command /announce
Expand Down Expand Up @@ -43,10 +48,12 @@ export const announcementHandler: CommandHandler = async (interaction) => {
if (currentUser?.roles.includes(UserRole.ADMIN)) {
const message = interaction.options.getString('message');

const userSelectionMsg = (await interaction.editReply({
const userSelectionMsg = await interaction.editReply({
content: 'Which users do you want to send the message to?',
components: [new MessageActionRow().addComponents([dmTargetMenu])],
})) as Message;
components: [
new ActionRowBuilder<SelectMenuBuilder>().addComponents([dmTargetMenu]),
],
});

const collector = userSelectionMsg.createMessageComponentCollector({
filter: (click) => click.user.id === interaction.user.id,
Expand Down Expand Up @@ -76,7 +83,7 @@ export const announcementHandler: CommandHandler = async (interaction) => {
await interaction.editReply({
content: 'Which period are you referring to?',
components: [
new MessageActionRow().addComponents([
new ActionRowBuilder<SelectMenuBuilder>().addComponents([
periodSelectMenu(openPeriods),
]),
],
Expand All @@ -89,7 +96,7 @@ export const announcementHandler: CommandHandler = async (interaction) => {
message || ''
}\n---`,
components: [
new MessageActionRow().addComponents([
new ActionRowBuilder<ButtonBuilder>().addComponents([
continueButton,
cancelButton,
]),
Expand All @@ -105,7 +112,7 @@ export const announcementHandler: CommandHandler = async (interaction) => {
message || ''
}\n---`,
components: [
new MessageActionRow().addComponents([
new ActionRowBuilder<ButtonBuilder>().addComponents([
continueButton,
cancelButton,
]),
Expand Down Expand Up @@ -153,6 +160,5 @@ export const announcementHandler: CommandHandler = async (interaction) => {
content:
'You do not have the needed permissions to use this command. If you would like to perform admin actions, you would need to be granted an `ADMIN` role on the Praise Dashboard.',
});
return;
}
};
71 changes: 31 additions & 40 deletions packages/discord-bot/src/handlers/forward.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { PraiseModel } from 'api/dist/praise/entities';
import { GuildMember, Util } from 'discord.js';
import { GuildMember } from 'discord.js';
import { UserModel } from 'api/dist/user/entities';
import { EventLogTypeKey } from 'api/src/eventlog/types';
import { logEvent } from 'api/src/eventlog/utils';
import { EventLogTypeKey } from 'api/dist/eventlog/types';
import { logEvent } from 'api/dist/eventlog/utils';
import logger from 'jet-logger';
import { UserRole } from 'api/dist/user/types';
import { settingValue } from 'api/dist/shared/settings';
import { getReceiverData } from '../utils/getReceiverData';
import { getUserAccount } from '../utils/getUserAccount';
import {
dmError,
Expand All @@ -19,10 +19,11 @@ import {
forwardSuccess,
giverNotActivatedError,
selfPraiseWarning,
} from '../utils/praiseEmbeds';
} from '../utils/embeds/praiseEmbeds';
import { assertPraiseGiver } from '../utils/assertPraiseGiver';
import { assertPraiseAllowedInChannel } from '../utils/assertPraiseAllowedInChannel';
import { CommandHandler } from '../interfaces/CommandHandler';
import { createPraise } from '../utils/createPraise';

/**
* Execute command /firward
Expand Down Expand Up @@ -69,15 +70,14 @@ export const forwardHandler: CommandHandler = async (
if (!(await assertPraiseGiver(praiseGiver, interaction, true))) return;

const receivers = interaction.options.getString('receivers');
const receiverData = {
validReceiverIds: receivers?.match(/<@!([0-9]+)>/g),
undefinedReceivers: receivers?.match(/@([a-z0-9]+)/gi),
roleMentions: receivers?.match(/<@&([0-9]+)>/g),
};

if (!receivers || receivers.length === 0) {
await interaction.editReply(await invalidReceiverError());
return;
}

const receiverData = getReceiverData(receivers);
if (
!receivers ||
receivers.length === 0 ||
!receiverData.validReceiverIds ||
receiverData.validReceiverIds?.length === 0
) {
Expand Down Expand Up @@ -117,8 +117,6 @@ export const forwardHandler: CommandHandler = async (
(u) => u
);

const guildChannel = await guild.channels.fetch(channel?.id || '');

for (const receiver of Receivers) {
const receiverAccount = await getUserAccount(receiver);

Expand All @@ -131,21 +129,13 @@ export const forwardHandler: CommandHandler = async (
);
}
}
const praiseObj = await PraiseModel.create({
reason: reason,
/**
* ! Util.cleanContent might get deprecated in the coming versions of discord.js
* * We would have to make our own implementation (ref: https://github.com/discordjs/discord.js/blob/988a51b7641f8b33cc9387664605ddc02134859d/src/util/Util.js#L557-L584)
*/
reasonRealized: Util.cleanContent(reason, channel),
giver: giverAccount._id,
forwarder: forwarderAccount._id,
sourceId: `DISCORD:${guild.id}:${interaction.channelId}`,
sourceName: `DISCORD:${encodeURIComponent(
guild.name
)}:${encodeURIComponent(guildChannel?.name || '')}`,
receiver: receiverAccount._id,
});
const praiseObj = await createPraise(
interaction,
giverAccount,
receiverAccount,
reason,
forwarderAccount
);

await logEvent(
EventLogTypeKey.PRAISE,
Expand All @@ -172,17 +162,19 @@ export const forwardHandler: CommandHandler = async (
}
}

Receivers.length !== 0
? await interaction.editReply(
await forwardSuccess(
praiseGiver.user,
praised.map((id) => `<@!${id}>`),
reason
)
if (Receivers.length !== 0) {
await interaction.editReply(
await forwardSuccess(
praiseGiver.user,
praised.map((id) => `<@!${id}>`),
reason
)
: warnSelfPraise
? await interaction.editReply(await selfPraiseWarning())
: await interaction.editReply(await invalidReceiverError());
);
} else if (warnSelfPraise) {
await interaction.editReply(await selfPraiseWarning());
} else {
await interaction.editReply(await invalidReceiverError());
}

const warningMsg =
(receiverData.undefinedReceivers
Expand All @@ -204,5 +196,4 @@ export const forwardHandler: CommandHandler = async (
if (warningMsg && warningMsg.length !== 0) {
await interaction.followUp({ content: warningMsg, ephemeral: true });
}
return;
};
23 changes: 15 additions & 8 deletions packages/discord-bot/src/handlers/help.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { Collection, CommandInteraction, MessageEmbed } from 'discord.js';
import {
Collection,
ChatInputCommandInteraction,
EmbedBuilder,
} from 'discord.js';
import { Command } from 'src/interfaces/Command';

/**
* Executes command /help
* Provides documentation on how to use the praise discord bot
*
* @param {CommandInteraction} interaction
* @param {ChatInputCommandInteraction} interaction
* @param {Collection<string, Command>} commands
* @returns {Promise<void>}
*/
export const helpHandler = async (
interaction: CommandInteraction,
interaction: ChatInputCommandInteraction,
commands: Collection<string, Command>
): Promise<void> => {
const cmd = interaction.options.getString('command');
Expand All @@ -20,12 +24,12 @@ export const helpHandler = async (
.join('\n');
await interaction.editReply({
embeds: [
new MessageEmbed()
new EmbedBuilder()
.setTitle('help')
.setDescription(
'Welcome to the praise bot!\nTo start using praise, you need to activate praise by using the `/activate` command.\nTo praise, use the `/praise command`.'
)
.addField('Commands', cmdDescription),
.addFields({ name: 'Commands', value: cmdDescription }),
],
});
return;
Expand All @@ -34,20 +38,23 @@ export const helpHandler = async (
if (!commandHelp || !commandHelp.text) {
await interaction.editReply({
embeds: [
new MessageEmbed()
new EmbedBuilder()
.setTitle(`\`${cmd}\` command`)
.setDescription('No HelpText found for this command...'),
],
});
return;
}
const helpEmbed = new MessageEmbed()
const helpEmbed = new EmbedBuilder()
.setTitle(`\`${cmd}\` command`)
.setDescription(commandHelp.text);

if (commandHelp.subCommands) {
for (const subCommandHelp of commandHelp.subCommands) {
helpEmbed.addField(`${cmd} ${subCommandHelp.name}`, subCommandHelp.text);
helpEmbed.addFields({
name: `${cmd} ${subCommandHelp.name}`,
value: subCommandHelp.text,
});
}
}

Expand Down
Loading

0 comments on commit 22a8ba7

Please sign in to comment.