Skip to content

Commit

Permalink
Merge branch 'main' of github.com:spacebarchat/docs-bot into main
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyUnderStars committed Dec 14, 2023
2 parents 37191bf + d737e86 commit 8b971a0
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 8 deletions.
Empty file modified .husky/pre-commit
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Bot {
if (!command) return;

const ret = await command.exec(interaction);
await interaction.reply(ret);
if (ret) await interaction.reply(ret);

return;
} else if (interaction.isAutocomplete()) {
Expand Down Expand Up @@ -67,9 +67,9 @@ export default class Bot {
if (!found) return;

// TODO: Parse command options and check for any required fields?
const withArgs: MessageWithArgs = Object.assign({}, message, { args });
const withArgs: MessageWithArgs = Object.assign(message, { args });
const ret = await found.exec(withArgs);
await message.reply(ret);
if (ret) await message.reply(ret);

return;
};
Expand Down
7 changes: 5 additions & 2 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
import type * as Discord from "discord.js";

import docs from "./docs.js";
import phrase from "./phrase.js";

export type MessageWithArgs = Discord.Message & {
args: string[];
};

type OrPromise<T> = T | Promise<T>;

export type CommandType = {
name: string;
description: string;
options: Discord.APIApplicationCommandOption[];
exec: (
caller: MessageWithArgs | Discord.ChatInputCommandInteraction,
) => Discord.BaseMessageOptions | Promise<Discord.BaseMessageOptions>;
) => OrPromise<Discord.BaseMessageOptions | undefined>;
autocomplete?: (caller: Discord.AutocompleteInteraction) => unknown;
};

const commands: CommandType[] = [docs];
const commands: CommandType[] = [docs, phrase];

export default commands;
44 changes: 44 additions & 0 deletions src/commands/phrase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2023 Spacebar Contributors <https://spacebar.chat>
// SPDX-License-Identifier: AGPL-3.0-or-later

import * as Discord from "discord.js";
import type { CommandType } from ".";
import { triggerPhrase } from "../util/triggerPhrases.js";

export default {
name: "phrase",
description: "Respond to this message as if it was a trigger phrase",
options: [
{
name: "keywords",
description:
"Trigger phrase keywords (cannot be used as a /command)",
type: Discord.ApplicationCommandOptionType.String,
},
],

exec: async (caller) => {
if (caller instanceof Discord.ChatInputCommandInteraction) return;

let message = caller.args.join(" ");

if (!message) {
if (
!(caller instanceof Discord.ChatInputCommandInteraction) &&
caller?.reference?.messageId
) {
const referenced = await caller.channel.messages.fetch(
caller.reference.messageId,
);

message = referenced.content;
}
}

caller.content = message;
caller.member;
await triggerPhrase(caller as Discord.Message, true);

return undefined;
},
} as CommandType;
23 changes: 20 additions & 3 deletions src/util/triggerPhrases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ const sendDocs = async (
Object.assign({}, caller, { args: query.split(" ") }),
);

if (!resp) return false;
resp.content = replyMessage;
caller.reply(resp);
return true;
};

export const triggerPhrase = async (caller: Message): Promise<boolean> => {
export const triggerPhrase = async (
caller: Message,
force = false,
): Promise<boolean> => {
if (!caller.member || !caller.member.joinedAt) return false;

if (process.env.NODE_ENV !== "development") {
if (!force && process.env.NODE_ENV !== "development") {
// don't bother people that have been here at least 2 weeks
const week = new Date();
week.setDate(new Date().getDate() - 14);
Expand Down Expand Up @@ -118,14 +122,27 @@ export const triggerPhrase = async (caller: Message): Promise<boolean> => {
content.includes("put it on"))
) {
await caller.reply(
"Hosting Spacebar on replit, heroku, vercel, or other such platforms is not supported. " +
"Hosting Spacebar on replit, heroku, vercel, or other such platforms is not supported.\n" +
"While you *can* do it, it is not a good experience for the user or the instance owner.\n" +
"A big issue with hosting on replit is that you have nowhere to host a dedicated database, which forces you to use sqlite, " +
"but you cannot edit the sqlite database that is used.",
);
return true;
}

if (
content.match(
/when will (((spacebar|space bar) ?(chat)?)|it|fosscord) release\??/,
)
) {
await caller.reply(
"Spacebar is an open source project and is already released!\n" +
"Please visit <https://docs.spacebar.chat> for information on how to setup and use Spacebar.\n" +
"You can also find our Github at <https://github.com/spacebarchat>",
);
return true;
}

if (
(content.includes("rory") ||
content.includes("talk") ||
Expand Down

0 comments on commit 8b971a0

Please sign in to comment.