feat: first version of bot (#2)

### Explanation

This should set up everything we need for our initial launch. Test coverage is at 100% to ensure nothing breaks.

### Issue

_No response_

### Attestations

- [x] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/)
- [x] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/).
- [x] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/).

### Dependencies

- [x] I have pinned the dependencies to a specific patch version.

### Style

- [x] I have run the linter and resolved any errors.
- [x] My pull request uses an appropriate title, matching the conventional commit standards.
- [x] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request.

### Tests

- [x] My contribution adds new code, and I have added tests to cover it.
- [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes.
- [x] All new and existing tests pass locally with my changes.
- [x] Code coverage remains at or above the configured threshold.

### Documentation

Coming soon - I'm working on the infra for docs next

### Versioning

Major - My pull request introduces a breaking change.

Reviewed-on: https://codeberg.org/nhcarrigan/rig-task-bot/pulls/2
Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com>
Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit is contained in:
2024-09-30 01:41:25 +00:00
committed by Naomi the Technomancer
parent da6fbfd45e
commit 296a50fedd
49 changed files with 4816 additions and 3 deletions
+99
View File
@@ -0,0 +1,99 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { assign } from "../commands/assign.js";
import { complete } from "../commands/complete.js";
import { create } from "../commands/create.js";
import { deleteCommand } from "../commands/delete.js";
import { list } from "../commands/list.js";
import { priority } from "../commands/priority.js";
import { tag } from "../commands/tag.js";
import { update } from "../commands/update.js";
import { view } from "../commands/view.js";
import { defaultCommand } from "../modules/defaultCommand.js";
import {
createModal,
defaultModal,
updateModal,
type ModalHandler,
} from "../modules/modalHandlers.js";
import { errorHandler } from "../utils/errorHandler.js";
import type { Bot } from "../interfaces/bot.js";
import type { Command } from "../interfaces/command.js";
import type { Interaction } from "discord.js";
const commandMap: Record<string, Command> = {
assign: assign,
complete: complete,
create: create,
delete: deleteCommand,
list: list,
priority: priority,
tag: tag,
update: update,
view: view,
};
const modalMap: Record<string, ModalHandler> = {
// eslint-disable-next-line @typescript-eslint/naming-convention
"create-task": createModal,
// eslint-disable-next-line @typescript-eslint/naming-convention
"update-task": updateModal,
};
/**
* Handles all of the logic for interactions.
* If it's a slash command and we have the guild, try to run it.
* @param bot - The bot object, which contains the database and Discord client.
* @param interaction - The interaction payload from Discord.
*/
export const onInteractionCreate
// eslint-disable-next-line complexity
= async(bot: Bot, interaction: Interaction): Promise<void> => {
try {
if (interaction.isChatInputCommand()) {
if (!interaction.inCachedGuild()) {
await interaction.reply({
content: "How did you run this out of a guild?",
ephemeral: true,
});
return;
}
const command
= commandMap[interaction.commandName]?.run ?? defaultCommand;
await command(bot, interaction);
return;
}
if (interaction.isModalSubmit()) {
if (!interaction.inCachedGuild()) {
await interaction.reply({
content: "How did you get a modal outside of a guild?",
ephemeral: true,
});
return;
}
const handler = modalMap[interaction.customId] ?? defaultModal;
await handler(bot, interaction);
return;
}
throw new Error("Unknown interaction type.");
} catch (error) {
if (interaction.isAutocomplete()) {
await errorHandler(bot, "discord on interaction event", error);
return;
}
await errorHandler(
bot,
"discord on interaction event",
error,
interaction.replied
? interaction.editReply.bind(interaction)
: interaction.reply.bind(interaction),
);
}
};
+29
View File
@@ -0,0 +1,29 @@
/**
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
import { displayCommandCurl } from "../utils/displayCommandCurl.js";
import { errorHandler } from "../utils/errorHandler.js";
import { sendDebugLog } from "../utils/sendDebugLog.js";
import type { Bot } from "../interfaces/bot.js";
/**
* To be mounted on the ClientReady gateway event. Sends
* a message to the debug webhook to confirm the bot has
* authenticated to Discord.
* @param bot -- The bot object, containing the webhook client.
*/
export const onReady = async(bot: Bot): Promise<void> => {
try {
await sendDebugLog(bot, { content: "Bot has authenticated to Discord." });
await sendDebugLog(bot, { files: [ {
attachment: Buffer.from(displayCommandCurl(bot)),
name: "curl.sh",
} ] });
} catch (error) {
await errorHandler(bot, "discord on ready event", error);
}
};