|
| 1 | +/* |
| 2 | + * Copyright (C) 2025 InterChat |
| 3 | + * |
| 4 | + * InterChat is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as published |
| 6 | + * by the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * InterChat is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with InterChat. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + */ |
| 17 | + |
| 18 | +import BaseCommand from '#src/core/BaseCommand.js'; |
| 19 | +import Context from '#src/core/CommandContext/Context.js'; |
| 20 | +import db from '#src/utils/Db.js'; |
| 21 | +import { ApplicationCommandOptionType, Invite } from 'discord.js'; |
| 22 | + |
| 23 | +export default class ConfigSetInviteSubcommand extends BaseCommand { |
| 24 | + constructor() { |
| 25 | + super({ |
| 26 | + name: 'set-invite', |
| 27 | + description: |
| 28 | + 'Set the invite link for the server. People can use it to join through InterChat leaderboards.', |
| 29 | + types: { slash: true, prefix: true }, |
| 30 | + options: [ |
| 31 | + { |
| 32 | + type: ApplicationCommandOptionType.String, |
| 33 | + name: 'invite', |
| 34 | + description: 'The invite link to set for the server. (Leave empty to remove)', |
| 35 | + required: false, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }); |
| 39 | + } |
| 40 | + async execute(ctx: Context) { |
| 41 | + if (!ctx.inGuild()) return; |
| 42 | + await ctx.deferReply(); |
| 43 | + |
| 44 | + const inviteLink = ctx.options.getString('invite'); |
| 45 | + if (!inviteLink?.length) { |
| 46 | + await db.serverData.upsert({ |
| 47 | + where: { id: ctx.guild.id }, |
| 48 | + create: { id: ctx.guildId }, |
| 49 | + update: { inviteCode: null }, |
| 50 | + }); |
| 51 | + |
| 52 | + await ctx.replyEmbed('config.setInvite.removed', { |
| 53 | + edit: true, |
| 54 | + t: { emoji: ctx.getEmoji('tick_icon') }, |
| 55 | + }); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const inviteCode = inviteLink.match(Invite.InvitesPattern)?.[1]; |
| 60 | + if (!inviteCode) { |
| 61 | + await ctx.replyEmbed('config.setInvite.invalid', { |
| 62 | + edit: true, |
| 63 | + t: { emoji: ctx.getEmoji('x_icon') }, |
| 64 | + }); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + const inviteInGuild = (await ctx.guild.invites.fetch()).get(inviteCode); |
| 69 | + if (!inviteInGuild) { |
| 70 | + await ctx.replyEmbed('config.setInvite.notFromServer', { |
| 71 | + edit: true, |
| 72 | + t: { emoji: ctx.getEmoji('x_icon') }, |
| 73 | + }); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + await db.serverData.upsert({ |
| 78 | + where: { id: ctx.guild.id }, |
| 79 | + create: { id: ctx.guildId, inviteCode }, |
| 80 | + update: { inviteCode }, |
| 81 | + }); |
| 82 | + |
| 83 | + await ctx.replyEmbed('config.setInvite.success', { |
| 84 | + edit: true, |
| 85 | + t: { emoji: ctx.getEmoji('tick_icon') }, |
| 86 | + }); |
| 87 | + } |
| 88 | +} |
0 commit comments