Skip to content

Commit

Permalink
Update Discord.js to v14 (#240)
Browse files Browse the repository at this point in the history
* Update to Discord.js v14

* Link gained commands to correct period

* Handle group gained error
  • Loading branch information
rorro authored Feb 26, 2024
1 parent e1a0ade commit 5bb5b78
Show file tree
Hide file tree
Showing 52 changed files with 569 additions and 547 deletions.
414 changes: 178 additions & 236 deletions package-lock.json

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"scripts": {
"build": "rimraf ./dist && tsc",
"lint": "eslint . --ext .ts",
"dev": "docker-compose up --build",
"dev": "docker-compose up --build -d && docker logs -f bot",
"prod": "prisma migrate deploy && pm2-runtime dist/index.js",
"watch": "prisma migrate dev && ts-node-dev --poll --exit-child --respawn --transpile-only --ignore-watch node_modules src/index.ts"
},
Expand All @@ -39,20 +39,17 @@
"prisma": "^4.6.1",
"rimraf": "^3.0.2",
"ts-node-dev": "^2.0.0",
"typescript": "^4.5.5"
"typescript": "^5.3.3"
},
"dependencies": {
"@discordjs/builders": "^0.12.0",
"@discordjs/rest": "^0.4.1",
"@prisma/client": "^4.6.1",
"@sapphire/discord.js-utilities": "^4.8.0",
"@sapphire/discord.js-utilities": "^7.1.6",
"@sentry/node": "^7.28.0",
"@sentry/tracing": "^7.28.0",
"@wise-old-man/utils": "^3.1.13",
"canvas": "^2.6.1",
"cors": "^2.8.5",
"discord-api-types": "^0.27.2",
"discord.js": "^13.7.0",
"discord.js": "^14.14.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"lodash": "^4.17.19",
Expand Down
28 changes: 20 additions & 8 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { Client, Guild, Intents, Interaction, MessageEmbed, TextChannel } from 'discord.js';
import {
Client,
Guild,
Interaction,
EmbedBuilder,
TextChannel,
GatewayIntentBits,
ChannelType,
PermissionFlagsBits
} from 'discord.js';
import config from './config';
import * as router from './commands/router';
import {
Expand All @@ -15,11 +24,11 @@ class Bot {
constructor() {
this.client = new Client({
intents: [
Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.GUILD_MESSAGE_TYPING,
Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
Intents.FLAGS.DIRECT_MESSAGE_TYPING
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMessageTyping,
GatewayIntentBits.GuildMessageReactions,
GatewayIntentBits.DirectMessageTyping
],
shards: 'auto'
});
Expand Down Expand Up @@ -64,7 +73,7 @@ class Bot {
}

function buildJoinMessage() {
return new MessageEmbed()
return new EmbedBuilder()
.setColor(config.visuals.blue)
.setTitle(`❤️ Thanks for adding me!`)
.setDescription(
Expand All @@ -88,7 +97,10 @@ function buildJoinMessage() {
*/
function findOpenChannel(guild: Guild) {
const channel = guild.channels.cache.find(c => {
return c.type === 'GUILD_TEXT' && Boolean(guild.me?.permissions.has('SEND_MESSAGES'));
return (
c.type === ChannelType.GuildText &&
Boolean(guild.members.me?.permissions.has(PermissionFlagsBits.SendMessages))
);
});

return channel as TextChannel;
Expand Down
12 changes: 8 additions & 4 deletions src/commands/instances/config/ConfigGroupCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { ApplicationCommandOptionType, ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import config from '../../../config';
import { updateServerGroup } from '../../../services/prisma';
import womClient from '../../../services/wiseoldman';
Expand All @@ -9,7 +9,7 @@ const CONFIG: CommandConfig = {
description: "Configure the server's Wise Old Man group.",
options: [
{
type: 'integer',
type: ApplicationCommandOptionType.Integer,
name: 'group_id',
description: 'The group ID to link to the server.',
required: true
Expand All @@ -23,7 +23,7 @@ class ConfigGroupCommand extends Command {
this.requiresAdmin = true;
}

async execute(interaction: CommandInteraction) {
async execute(interaction: ChatInputCommandInteraction) {
const guildId = interaction.guild?.id || '';

if (!guildId || guildId.length === 0) {
Expand All @@ -32,14 +32,18 @@ class ConfigGroupCommand extends Command {

const groupId = interaction.options.getInteger('group_id', true);

if (groupId === null) {
throw new CommandError('The provided group ID is invalid.');
}

const group = await womClient.groups.getGroupDetails(groupId).catch(() => {
throw new CommandError("Couldn't find that group.");
});

// Update the server's group ID in the database
await updateServerGroup(guildId, groupId);

const response = new MessageEmbed()
const response = new EmbedBuilder()
.setColor(config.visuals.green)
.setTitle(`✅ Server group updated`)
.setDescription(`All notifications and commands will be in reference to **${group.name}**`)
Expand Down
29 changes: 18 additions & 11 deletions src/commands/instances/config/ConfigNotificationsCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Channel, CommandInteraction, MessageEmbed } from 'discord.js';
import {
ApplicationCommandOptionType,
Channel,
ChannelType,
ChatInputCommandInteraction,
EmbedBuilder,
PermissionFlagsBits
} from 'discord.js';
import config from '../../../config';
import { updateBotDefaultChannel, updateNotificationPreferences } from '../../../services/prisma';
import {
Expand All @@ -16,31 +23,31 @@ const CONFIG: CommandConfig = {
description: "Configure the bot's notification preferences.",
options: [
{
type: 'string',
type: ApplicationCommandOptionType.String,
required: true,
name: 'notification_type',
description: 'The notification type to configure.',
choices: [
...Object.values(NotificationType).map(type => ({
label: NotificationName[type],
name: NotificationName[type],
value: type
}))
]
},
{
type: 'channel',
type: ApplicationCommandOptionType.Channel,
required: true,
name: 'notification_channel',
description: 'The channel to which notifications are sent.',
channelType: 0 // Only add text channels
channelType: ChannelType.GuildText
},
{
type: 'string',
type: ApplicationCommandOptionType.String,
name: 'status',
description: `Enable or disable notifications of a certain type. Default channel cannot be disabled.`,
choices: [
{ label: 'Enable', value: 'enable' },
{ label: 'Disable', value: 'disable' }
{ name: 'Enable', value: 'enable' },
{ name: 'Disable', value: 'disable' }
]
}
]
Expand All @@ -52,7 +59,7 @@ class ConfigNotificationsCommand extends Command {
this.requiresAdmin = true;
}

async execute(interaction: CommandInteraction) {
async execute(interaction: ChatInputCommandInteraction) {
const guildId = interaction.guild?.id || '';

if (!guildId || guildId.length === 0) {
Expand All @@ -73,7 +80,7 @@ class ConfigNotificationsCommand extends Command {
const channelPermissions =
channel.client.user !== null ? channel.permissionsFor(channel.client.user) : null;

if (channelPermissions !== null && !channelPermissions.has('VIEW_CHANNEL')) {
if (channelPermissions !== null && !channelPermissions.has(PermissionFlagsBits.ViewChannel)) {
throw new CommandError(`Error: The bot does not have access to <#${channel.id}>.`);
}

Expand Down Expand Up @@ -113,7 +120,7 @@ class ConfigNotificationsCommand extends Command {
description = `"${notificationName}" notifications will now be sent to <#${channel.id}>`;
}

const response = new MessageEmbed()
const response = new EmbedBuilder()
.setColor(config.visuals.green)
.setTitle(`✅ Notification Preferences Updated`)
.setDescription(description);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/instances/general/HelpCommand.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GroupDetails } from '@wise-old-man/utils';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { ApplicationCommandOptionType, ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import config from '../../../config';
import { CUSTOM_COMMANDS } from '../../../commands/custom';
import prisma, { getServer } from '../../../services/prisma';
Expand All @@ -24,7 +24,7 @@ const CONFIG: CommandConfig = {
description: 'Ask for help.',
options: [
{
type: 'string',
type: ApplicationCommandOptionType.String,
name: 'category',
description: 'What do you need help with?',
autocomplete: true
Expand All @@ -37,7 +37,7 @@ class HelpCommand extends Command {
super(CONFIG);
}

async execute(interaction: CommandInteraction) {
async execute(interaction: ChatInputCommandInteraction) {
if (!interaction.inGuild()) {
throw new CommandError('This command can only be used in a Discord server.');
}
Expand Down Expand Up @@ -93,7 +93,7 @@ class HelpCommand extends Command {
};
});

const response = new MessageEmbed()
const response = new EmbedBuilder()
.setColor(config.visuals.blue)
.setTitle(`ℹ️ Need help?`)
.setDescription(`${LINE_COMMANDS}\n\n${LINE_SUPPORT}\n\n⚠️${LINE_PERMS}`)
Expand Down
6 changes: 3 additions & 3 deletions src/commands/instances/group/GroupAchievementsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getEmoji,
getLinkedGroupId
} from '../../../utils';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import womClient from '../../../services/wiseoldman';
import config from '../../../config';

Expand All @@ -21,7 +21,7 @@ class GroupAchievements extends Command {
super(CONFIG);
}

async execute(interaction: CommandInteraction) {
async execute(interaction: ChatInputCommandInteraction) {
const groupId = await getLinkedGroupId(interaction);

const groupAchievements = await womClient.groups
Expand All @@ -39,7 +39,7 @@ class GroupAchievements extends Command {
)
.join('\n');

const response = new MessageEmbed()
const response = new EmbedBuilder()
.setColor(config.visuals.blue)
.setTitle('Recent Group Achievements')
.setDescription(achievementList)
Expand Down
18 changes: 9 additions & 9 deletions src/commands/instances/group/GroupCompetitionCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isCompetitionStatus,
MetricProps
} from '@wise-old-man/utils';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { ApplicationCommandOptionType, ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import { uniq } from 'lodash';
import config from '../../../config';
import womClient, { getCompetitionStatus, getCompetitionTimeLeft } from '../../../services/wiseoldman';
Expand All @@ -27,22 +27,22 @@ const CONFIG: CommandConfig = {
description: "View a group's ongoing/upcoming competition.",
options: [
{
type: 'integer',
type: ApplicationCommandOptionType.Integer,
name: 'competition_id',
description: 'Competition ID'
},
{
type: 'string',
type: ApplicationCommandOptionType.String,
name: 'status',
description: 'View an ongoing or upcoming group competition.',
choices: [
{
value: CompetitionStatus.ONGOING,
label: CompetitionStatusProps[CompetitionStatus.ONGOING].name
name: CompetitionStatusProps[CompetitionStatus.ONGOING].name,
value: CompetitionStatus.ONGOING
},
{
value: CompetitionStatus.UPCOMING,
label: CompetitionStatusProps[CompetitionStatus.UPCOMING].name
name: CompetitionStatusProps[CompetitionStatus.UPCOMING].name,
value: CompetitionStatus.UPCOMING
}
]
}
Expand All @@ -54,7 +54,7 @@ class GroupCompetitionCommand extends Command {
super(CONFIG);
}

async execute(interaction: CommandInteraction) {
async execute(interaction: ChatInputCommandInteraction) {
const groupId = await getLinkedGroupId(interaction);

// Extract the "status" param, or fallback to "ongoing"
Expand All @@ -70,7 +70,7 @@ class GroupCompetitionCommand extends Command {
throw new CommandError("Couldn't find that competition.");
});

const response = new MessageEmbed()
const response = new EmbedBuilder()
.setColor(config.visuals.blue)
.setTitle(competition.title)
.setURL(`https://wiseoldman.net/competitions/${competition.id}/`)
Expand Down
10 changes: 5 additions & 5 deletions src/commands/instances/group/GroupCompetitionsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
CompetitionTypeProps,
GroupDetails
} from '@wise-old-man/utils';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { ChatInputCommandInteraction, EmbedBuilder } from 'discord.js';
import womClient, { getCompetitionStatus, getCompetitionTimeLeft } from '../../../services/wiseoldman';
import config from '../../../config';
import { Command, CommandConfig, CommandError, getEmoji, getLinkedGroupId } from '../../../utils';
Expand All @@ -28,7 +28,7 @@ class GroupCompetitionsCommand extends Command {
super(CONFIG);
}

async execute(interaction: CommandInteraction) {
async execute(interaction: ChatInputCommandInteraction) {
const groupId = await getLinkedGroupId(interaction);

const group = await womClient.groups.getGroupDetails(groupId).catch(() => {
Expand Down Expand Up @@ -56,7 +56,7 @@ class GroupCompetitionsCommand extends Command {
return;
}

const embedTemplate = new MessageEmbed()
const embedTemplate = new EmbedBuilder()
.setColor(config.visuals.blue)
.setTitle(`${group.name} - Most Recent Competitions`)
.setURL(`https://wiseoldman.net/groups/${groupId}/competitions`);
Expand Down Expand Up @@ -103,7 +103,7 @@ function buildPages(group: GroupDetails, competitions: CompetitionListItem[]) {
throw new CommandError("Couldn't find any competitions for this group.");
}

const pages: Array<MessageEmbed> = [];
const pages: Array<EmbedBuilder> = [];

for (let i = 0; i < pageCount; i++) {
const pageCompetitions = competitionsList.slice(
Expand All @@ -112,7 +112,7 @@ function buildPages(group: GroupDetails, competitions: CompetitionListItem[]) {
);

pages.push(
new MessageEmbed().setTitle(`${group.name} - Most Recent Competitions`).addFields(
new EmbedBuilder().setTitle(`${group.name} - Most Recent Competitions`).addFields(
pageCompetitions.map(c => {
return { name: c.name, value: c.value };
})
Expand Down
Loading

0 comments on commit 5bb5b78

Please sign in to comment.