generated from nhcarrigan/template
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
ApplicationIntegrationType,
|
||||
SlashCommandBuilder,
|
||||
InteractionContextType,
|
||||
} from "discord.js";
|
||||
|
||||
const command = new SlashCommandBuilder().
|
||||
setContexts(
|
||||
InteractionContextType.Guild,
|
||||
).
|
||||
setIntegrationTypes(ApplicationIntegrationType.GuildInstall).
|
||||
setName("about").
|
||||
setDescription("Learn more about this bot!");
|
||||
|
||||
// eslint-disable-next-line no-console -- We don't need our logger here as this never runs in production.
|
||||
console.log(JSON.stringify(command.toJSON()));
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
ApplicationIntegrationType,
|
||||
SlashCommandBuilder,
|
||||
InteractionContextType,
|
||||
} from "discord.js";
|
||||
|
||||
const command = new SlashCommandBuilder().
|
||||
setContexts(
|
||||
InteractionContextType.Guild,
|
||||
).
|
||||
setIntegrationTypes(ApplicationIntegrationType.GuildInstall).
|
||||
setName("role").
|
||||
setDescription("Set the support role that is added to each ticket.").
|
||||
addRoleOption((option) => {
|
||||
return option.setName("role").
|
||||
setDescription("The role that should be invited to each thread.").
|
||||
setRequired(true);
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-console -- We don't need our logger here as this never runs in production.
|
||||
console.log(JSON.stringify(command.toJSON()));
|
||||
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
ApplicationIntegrationType,
|
||||
SlashCommandBuilder,
|
||||
InteractionContextType,
|
||||
ChannelType,
|
||||
} from "discord.js";
|
||||
|
||||
const command = new SlashCommandBuilder().
|
||||
setContexts(
|
||||
InteractionContextType.Guild,
|
||||
).
|
||||
setIntegrationTypes(ApplicationIntegrationType.GuildInstall).
|
||||
setName("start").
|
||||
setDescription("Send the ticket start post in the specified channel.").
|
||||
addChannelOption((option) => {
|
||||
return option.setName("channel").
|
||||
setDescription("The channel to send the ticket start post in.").
|
||||
setRequired(true).
|
||||
addChannelTypes(ChannelType.GuildText);
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-console -- We don't need our logger here as this never runs in production.
|
||||
console.log(JSON.stringify(command.toJSON()));
|
||||
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
/**
|
||||
* Guild IDs that are granted free access to this bot and do NOT
|
||||
* need to subscribe.
|
||||
*/
|
||||
export const entitledGuilds = [
|
||||
"443134315778539530",
|
||||
"1146133490933436476",
|
||||
];
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
import { Client, Events, GatewayIntentBits } from "discord.js";
|
||||
import { about } from "./modules/about.js";
|
||||
import { close } from "./modules/close.js";
|
||||
import { open } from "./modules/open.js";
|
||||
import { role } from "./modules/role.js";
|
||||
import { start } from "./modules/start.js";
|
||||
import { instantiateServer } from "./server/serve.js";
|
||||
import { logger } from "./utils/logger.js";
|
||||
|
||||
process.on("unhandledRejection", (error) => {
|
||||
if (error instanceof Error) {
|
||||
void logger.error("Unhandled Rejection", error);
|
||||
return;
|
||||
}
|
||||
void logger.error("unhandled rejection", new Error(String(error)));
|
||||
});
|
||||
|
||||
process.on("uncaughtException", (error) => {
|
||||
if (error instanceof Error) {
|
||||
void logger.error("Uncaught Exception", error);
|
||||
return;
|
||||
}
|
||||
void logger.error("uncaught exception", new Error(String(error)));
|
||||
});
|
||||
|
||||
const client = new Client({
|
||||
intents: [ GatewayIntentBits.Guilds ],
|
||||
});
|
||||
|
||||
const database = new PrismaClient();
|
||||
|
||||
// eslint-disable-next-line max-lines-per-function -- One too many...
|
||||
client.on(Events.InteractionCreate, (interaction) => {
|
||||
if (interaction.isButton()) {
|
||||
if (!interaction.inCachedGuild()) {
|
||||
void interaction.reply({
|
||||
content: "I'm sorry, but I can only be used in a server.",
|
||||
ephemeral: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
switch (interaction.customId) {
|
||||
case "open":
|
||||
void open(interaction, database);
|
||||
break;
|
||||
case "close":
|
||||
void close(interaction, database);
|
||||
break;
|
||||
default:
|
||||
void interaction.reply({
|
||||
content: "I'm sorry, I don't know what to do with this button.",
|
||||
ephemeral: true,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (interaction.isChatInputCommand()) {
|
||||
if (!interaction.inCachedGuild()) {
|
||||
void interaction.reply({
|
||||
content: "I'm sorry, but I can only be used in a server.",
|
||||
ephemeral: true,
|
||||
});
|
||||
return;
|
||||
}
|
||||
switch (interaction.commandName) {
|
||||
case "about":
|
||||
void about(interaction);
|
||||
break;
|
||||
case "start":
|
||||
void start(interaction, database);
|
||||
break;
|
||||
case "role":
|
||||
void role(interaction, database);
|
||||
break;
|
||||
default:
|
||||
void interaction.reply({
|
||||
content: `I'm sorry, I don't know the ${interaction.commandName} command.`,
|
||||
ephemeral: true,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
client.on(Events.EntitlementCreate, (entitlement) => {
|
||||
void logger.log("info", `User ${entitlement.userId} has subscribed!`);
|
||||
});
|
||||
|
||||
client.on(Events.EntitlementDelete, (entitlement) => {
|
||||
void logger.log("info", `User ${entitlement.userId} has unsubscribed... :c`);
|
||||
});
|
||||
|
||||
client.on(Events.ClientReady, () => {
|
||||
void logger.log("debug", "Bot is ready.");
|
||||
});
|
||||
|
||||
instantiateServer();
|
||||
await client.login(process.env.DISCORD_TOKEN);
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { execSync } from "node:child_process";
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
EmbedBuilder,
|
||||
MessageFlags,
|
||||
type ChatInputCommandInteraction,
|
||||
} from "discord.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
|
||||
/**
|
||||
* Responds with information about the bot.
|
||||
* @param interaction -- The interaction payload from Discord.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function -- We're iteraly one over.
|
||||
export const about = async(
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
|
||||
const version = process.env.npm_package_version ?? "Unknown";
|
||||
const commit = execSync("git rev-parse --short HEAD").toString().
|
||||
trim();
|
||||
|
||||
const embed = new EmbedBuilder();
|
||||
embed.setTitle("Gwen Abalise");
|
||||
embed.setDescription(
|
||||
// eslint-disable-next-line stylistic/max-len -- This is a long string.
|
||||
"Gwen uses private threads to provide a clean and user-friendly ticketing system for your server.",
|
||||
);
|
||||
embed.addFields(
|
||||
{
|
||||
name: "Version",
|
||||
value: version,
|
||||
},
|
||||
{
|
||||
name: "Current Commit",
|
||||
value: commit,
|
||||
},
|
||||
);
|
||||
|
||||
const supportButton = new ButtonBuilder().
|
||||
setLabel("Need help?").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://chat.nhcarrigan.com");
|
||||
const sourceButton = new ButtonBuilder().
|
||||
setLabel("Source code").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://git.nhcarrigan.com/nhcarrigan/gwen-abalise");
|
||||
const subscribeButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Premium).
|
||||
setSKUId("1343419585117945936");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
supportButton,
|
||||
sourceButton,
|
||||
subscribeButton,
|
||||
);
|
||||
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
embeds: [ embed ],
|
||||
});
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("about command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
ChannelType,
|
||||
MessageFlags,
|
||||
type ButtonInteraction,
|
||||
} from "discord.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
|
||||
/**
|
||||
* Closes a ticket when a user clicks on the button.
|
||||
* @param interaction - The interaction payload from Discord.
|
||||
* @param database - The Prisma client.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function, max-statements -- We're close!
|
||||
export const close = async(
|
||||
interaction: ButtonInteraction<"cached">,
|
||||
database: PrismaClient,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
|
||||
const subscribed = await isSubscribed(interaction);
|
||||
|
||||
if (!subscribed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const supportRole = await database.roles.findUnique({
|
||||
where: {
|
||||
serverId: interaction.guild.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!supportRole) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
// eslint-disable-next-line stylistic/max-len -- This is a long string.
|
||||
"No support role has been set for this server. Please notify the admins.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const { channel, user } = interaction;
|
||||
|
||||
if (channel?.type !== ChannelType.PrivateThread) {
|
||||
await interaction.editReply({
|
||||
content: "How did this button show up outside of a thread???",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const foundTicket = await database.tickets.findUnique({
|
||||
where: {
|
||||
threadId: channel.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!foundTicket) {
|
||||
await interaction.editReply({
|
||||
content: `I do not have a record for this ticket...`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!foundTicket.open) {
|
||||
await interaction.editReply({
|
||||
content: `This ticket has already been closed...`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await channel.members.remove(foundTicket.uuid);
|
||||
|
||||
await channel.send({
|
||||
allowedMentions: {
|
||||
parse: [],
|
||||
},
|
||||
content: `# Ticket Closed
|
||||
|
||||
This ticket has been closed by <@${user.id}> and locked. The member has been removed from the thread, and this thread is private so they cannot add themselves back.
|
||||
|
||||
Staff are welcome to discuss further here. The thread is preserved for logging.
|
||||
|
||||
Discord will auto-archive it, or if you are done discussing and want it out of the way you may archive it manually.`,
|
||||
});
|
||||
|
||||
await database.tickets.update({
|
||||
data: {
|
||||
open: false,
|
||||
},
|
||||
where: {
|
||||
id: foundTicket.id,
|
||||
},
|
||||
});
|
||||
|
||||
await interaction.editReply({
|
||||
content: `Ticket closed~!`,
|
||||
}).catch(() => {
|
||||
return null;
|
||||
});
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("close command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
ChannelType,
|
||||
MessageFlags,
|
||||
type ButtonInteraction,
|
||||
} from "discord.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
|
||||
/**
|
||||
* Opens a new ticket when a user clicks on the button.
|
||||
* @param interaction - The interaction payload from Discord.
|
||||
* @param database - The Prisma client.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function, max-statements -- We're close!
|
||||
export const open = async(
|
||||
interaction: ButtonInteraction<"cached">,
|
||||
database: PrismaClient,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
|
||||
const subscribed = await isSubscribed(interaction);
|
||||
|
||||
if (!subscribed) {
|
||||
return;
|
||||
}
|
||||
|
||||
const supportRole = await database.roles.findUnique({
|
||||
where: {
|
||||
serverId: interaction.guild.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!supportRole) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
// eslint-disable-next-line stylistic/max-len -- This is a long string.
|
||||
"No support role has been set for this server. Please notify the admins.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const { channel, user } = interaction;
|
||||
|
||||
if (channel?.type !== ChannelType.GuildText) {
|
||||
await interaction.editReply({
|
||||
content: "How did this button show up in a non-text channel???",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const hasTicket = await database.tickets.findFirst({
|
||||
where: {
|
||||
open: true,
|
||||
uuid: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (hasTicket) {
|
||||
await interaction.editReply({
|
||||
content: `You already have an open ticket: <#${hasTicket.threadId}>`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const ticket = await channel.threads.create({
|
||||
autoArchiveDuration: 1440,
|
||||
name: `ticket-${user.username}`,
|
||||
type: ChannelType.PrivateThread,
|
||||
});
|
||||
|
||||
const closeButton = new ButtonBuilder().setCustomId("close").
|
||||
setStyle(ButtonStyle.Danger).
|
||||
setLabel("Close Ticket").
|
||||
setEmoji("🔒");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().
|
||||
addComponents(closeButton);
|
||||
|
||||
await ticket.send({
|
||||
allowedMentions: {
|
||||
roles: [ supportRole.roleId ],
|
||||
users: [ user.id ],
|
||||
},
|
||||
components: [ row ],
|
||||
content: `Hey <@${user.id}>, this is your private ticket with the <@&${supportRole.roleId}> team. How can we help you today?`,
|
||||
});
|
||||
|
||||
await database.tickets.create({
|
||||
data: {
|
||||
open: true,
|
||||
threadId: ticket.id,
|
||||
uuid: user.id,
|
||||
},
|
||||
});
|
||||
|
||||
await interaction.editReply({
|
||||
content: `Ticket created~!`,
|
||||
});
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("open command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
MessageFlags,
|
||||
PermissionFlagsBits,
|
||||
type ChatInputCommandInteraction,
|
||||
} from "discord.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
|
||||
/**
|
||||
* Updates the role that is added to new tickets.
|
||||
* @param interaction - The interaction payload from Discord.
|
||||
* @param database - The Prisma client.
|
||||
*/
|
||||
export const role = async(
|
||||
interaction: ChatInputCommandInteraction<"cached">,
|
||||
database: PrismaClient,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
|
||||
const subscribed = await isSubscribed(interaction);
|
||||
|
||||
if (!subscribed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!interaction.member.permissions.has(PermissionFlagsBits.ManageGuild)) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
"You must have the `MANAGE_GUILD` permission to use this command.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const supportRole = interaction.
|
||||
options.
|
||||
getRole("role", true);
|
||||
|
||||
await database.roles.upsert({
|
||||
create: {
|
||||
roleId: supportRole.id,
|
||||
serverId: interaction.guild.id,
|
||||
},
|
||||
update: {
|
||||
roleId: supportRole.id,
|
||||
},
|
||||
where: {
|
||||
serverId: interaction.guild.id,
|
||||
},
|
||||
});
|
||||
|
||||
await interaction.editReply({
|
||||
content: `Your support role has been set to <@&${supportRole.id}>. This role will be pinged in new tickets.`,
|
||||
});
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("role command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
ChannelType,
|
||||
MessageFlags,
|
||||
PermissionFlagsBits,
|
||||
type ChatInputCommandInteraction,
|
||||
} from "discord.js";
|
||||
import { isSubscribed } from "../utils/isSubscribed.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import { replyToError } from "../utils/replyToError.js";
|
||||
import type { PrismaClient } from "@prisma/client";
|
||||
|
||||
/**
|
||||
* Sends the standard ticket start post in the specified channel.
|
||||
* @param interaction - The interaction payload from Discord.
|
||||
* @param database - The Prisma client.
|
||||
*/
|
||||
// eslint-disable-next-line max-lines-per-function, max-statements -- We're close enough.
|
||||
export const start = async(
|
||||
interaction: ChatInputCommandInteraction<"cached">,
|
||||
database: PrismaClient,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await interaction.deferReply({ flags: [ MessageFlags.Ephemeral ] });
|
||||
|
||||
const subscribed = await isSubscribed(interaction);
|
||||
|
||||
if (!subscribed) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!interaction.member.permissions.has(PermissionFlagsBits.ManageGuild)) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
"You must have the `MANAGE_GUILD` permission to use this command.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const { me } = interaction.guild.members;
|
||||
const requiredPermissions = [
|
||||
PermissionFlagsBits.SendMessages,
|
||||
PermissionFlagsBits.CreatePrivateThreads,
|
||||
PermissionFlagsBits.ManageThreads,
|
||||
];
|
||||
|
||||
if (!me) {
|
||||
await interaction.editReply(
|
||||
{ content: "Error loading my permissions. Please try again later." },
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const channel = interaction.
|
||||
options.
|
||||
getChannel("channel", true, [ ChannelType.GuildText ]);
|
||||
|
||||
const permissions = channel.permissionsFor(me);
|
||||
|
||||
if (requiredPermissions.some((permission) => {
|
||||
return !permissions.has(permission);
|
||||
})) {
|
||||
await interaction.editReply(
|
||||
{
|
||||
content:
|
||||
// eslint-disable-next-line stylistic/max-len -- This is a long string.
|
||||
"I do not have the required permissions send messages or manage private ticket threads in that channel.",
|
||||
},
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const role = await database.roles.findUnique({
|
||||
where: {
|
||||
serverId: interaction.guild.id,
|
||||
},
|
||||
});
|
||||
|
||||
if (!role) {
|
||||
await interaction.editReply({
|
||||
content:
|
||||
"You must set a support role before starting the ticket system.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const button = new ButtonBuilder().setCustomId("open").
|
||||
setLabel("Create Ticket").
|
||||
setEmoji("🎫").
|
||||
setStyle(ButtonStyle.Success);
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(button);
|
||||
|
||||
await channel.send({
|
||||
allowedMentions: {
|
||||
parse: [],
|
||||
},
|
||||
components: [ row ],
|
||||
content: `Please click the button below to create a new ticket with <@&${role.roleId}>.`,
|
||||
});
|
||||
|
||||
await interaction.editReply({
|
||||
content: "Your ticket system is ready!",
|
||||
});
|
||||
} catch (error) {
|
||||
await replyToError(interaction);
|
||||
if (error instanceof Error) {
|
||||
await logger.error("start command", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import fastify from "fastify";
|
||||
import { logger } from "../utils/logger.js";
|
||||
|
||||
const html = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Gwen Abalise</title>
|
||||
<meta charset="utf-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<meta name="description" content="Ticketing system for Discord!" />
|
||||
<script src="https://cdn.nhcarrigan.com/headers/index.js" async defer></script>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Gwen Abalise</h1>
|
||||
<section>
|
||||
<p>Ticketing system for Discord!</p>
|
||||
</section>
|
||||
<section>
|
||||
<h2>Links</h2>
|
||||
<p>
|
||||
<a href="https://git.nhcarrigan.com/nhcarrigan/gwen-abalise">
|
||||
<i class="fa-solid fa-code"></i> Source Code
|
||||
</a>
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://docs.nhcarrigan.com/">
|
||||
<i class="fa-solid fa-book"></i> Documentation
|
||||
</a>
|
||||
</p>
|
||||
<p>
|
||||
<a href="https://chat.nhcarrigan.com">
|
||||
<i class="fa-solid fa-circle-info"></i> Support
|
||||
</a>
|
||||
</p>
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
/**
|
||||
* Starts up a web server for health monitoring.
|
||||
*/
|
||||
export const instantiateServer = (): void => {
|
||||
try {
|
||||
const server = fastify({
|
||||
logger: false,
|
||||
});
|
||||
|
||||
server.get("/", (_request, response) => {
|
||||
response.header("Content-Type", "text/html");
|
||||
response.send(html);
|
||||
});
|
||||
|
||||
server.listen({ port: 5010 }, (error) => {
|
||||
if (error) {
|
||||
void logger.error("instantiate server", error);
|
||||
return;
|
||||
}
|
||||
void logger.log("debug", "Server listening on port 5010.");
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
void logger.error("instantiate server", error);
|
||||
return;
|
||||
}
|
||||
void logger.error("instantiate server", new Error(String(error)));
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
type ButtonInteraction,
|
||||
type ChatInputCommandInteraction,
|
||||
} from "discord.js";
|
||||
import { entitledGuilds } from "../config/entitledGuilds.js";
|
||||
|
||||
/**
|
||||
* Checks if a server has an active entitlement (subscription) for the bot.
|
||||
* If they do not, it responds to the interaction with a button to subscribe.
|
||||
* @param interaction -- The interaction payload from Discord.
|
||||
* @returns A boolean indicating whether the user is subscribed.
|
||||
*/
|
||||
export const isSubscribed = async(
|
||||
interaction:
|
||||
| ChatInputCommandInteraction<"cached">
|
||||
| ButtonInteraction<"cached">,
|
||||
): Promise<boolean> => {
|
||||
if (entitledGuilds.includes(interaction.guild.id)) {
|
||||
return true;
|
||||
}
|
||||
const isEntitled = interaction.entitlements.find((entitlement) => {
|
||||
return (
|
||||
entitlement.guildId === interaction.guild.id && entitlement.isActive()
|
||||
);
|
||||
});
|
||||
|
||||
if (!isEntitled && interaction.user.id !== "465650873650118659") {
|
||||
const subscribeButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Premium).
|
||||
setSKUId("1343419585117945936");
|
||||
const helpButton = new ButtonBuilder().
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://chat.nhcarrigan.com").
|
||||
setLabel("Need help?");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
subscribeButton,
|
||||
helpButton,
|
||||
);
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
// eslint-disable-next-line stylistic/max-len -- This is a long string.
|
||||
content: "Your server does not appear to have an active subscription. Without one, the ticket system will not function.",
|
||||
});
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { Logger } from "@nhcarrigan/logger";
|
||||
|
||||
export const logger = new Logger(
|
||||
"Gwen Abalise",
|
||||
process.env.LOG_TOKEN ?? "",
|
||||
);
|
||||
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* @copyright nhcarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
type ButtonInteraction,
|
||||
type ChatInputCommandInteraction,
|
||||
} from "discord.js";
|
||||
|
||||
/**
|
||||
* Responds to an interaction with a generic error message.
|
||||
* @param interaction -- The interaction payload from Discord.
|
||||
*/
|
||||
export const replyToError = async(
|
||||
interaction:
|
||||
| ChatInputCommandInteraction
|
||||
| ButtonInteraction,
|
||||
): Promise<void> => {
|
||||
const button = new ButtonBuilder().
|
||||
setLabel("Need help?").
|
||||
setStyle(ButtonStyle.Link).
|
||||
setURL("https://chat.nhcarrigan.com");
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(button);
|
||||
if (interaction.deferred || interaction.replied) {
|
||||
await interaction.editReply({
|
||||
components: [ row ],
|
||||
content: "Something went wrong with this command.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
await interaction.reply({
|
||||
components: [ row ],
|
||||
content: "Something went wrong with this command.",
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user