Skip to content

Commit

Permalink
Allow responses by DM
Browse files Browse the repository at this point in the history
  • Loading branch information
fsvreddit committed Oct 15, 2024
1 parent 24a1e5e commit 57394ac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
2 changes: 1 addition & 1 deletion devvit.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
name: social-links-bot
version: 0.0.18
version: 0.0.19
25 changes: 18 additions & 7 deletions src/handleCommentCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TriggerContext, User, UserSocialLink } from "@devvit/public-api";
import { CommentCreate } from "@devvit/protos";
import { isLinkId } from "@devvit/shared-types/tid.js";
import Ajv, { JSONSchemaType } from "ajv";
import { AppSetting } from "./settings.js";
import { AppSetting, ResponseMethod } from "./settings.js";
import { addCleanup } from "./cleanup.js";
import { addMinutes } from "date-fns";

Expand Down Expand Up @@ -112,13 +112,24 @@ export async function handleCommentCreate (event: CommentCreate, context: Trigge
});
}

await context.reddit.submitComment({
id: event.comment.id,
text: JSON.stringify(userSocialLinks),
});
const [responseMethod] = settings[AppSetting.ResponseMethod] as [ResponseMethod] | undefined ?? [ResponseMethod.Reply];

console.log("Comment left.");
if (responseMethod === ResponseMethod.Reply) {
await context.reddit.submitComment({
id: event.comment.id,
text: JSON.stringify(userSocialLinks),
});

console.log("Comment left.");

await addCleanup(event.comment.id, context);
} else {
await context.reddit.sendPrivateMessage({
subject: "social-links-bot response",
to: event.author.name,
text: JSON.stringify(userSocialLinks),
});
}

await addCleanup(event.comment.id, context);
await context.redis.set(redisKey, new Date().getTime().toString(), { expiration: addMinutes(new Date(), 20) });
}
21 changes: 21 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { SettingsFormField } from "@devvit/public-api";
export enum AppSetting {
PostId = "postId",
AccountNames = "accountName",
ResponseMethod = "responseMethod",
}

export enum ResponseMethod {
Reply = "reply",
PrivateMessage = "privateMessage",
}

export const appSettings: SettingsFormField[] = [
Expand All @@ -19,4 +25,19 @@ export const appSettings: SettingsFormField[] = [
helpText: "Comma separated, not case sensitive",
defaultValue: "DrRonikBot",
},
{
name: AppSetting.ResponseMethod,
type: "select",
label: "Response method",
options: [
{ label: "Reply to comment", value: ResponseMethod.Reply },
{ label: "Private message", value: ResponseMethod.PrivateMessage },
],
defaultValue: [ResponseMethod.Reply],
onValidate: ({ value }) => {
if (!value || value.length === 0) {
return "You must choose a response method";
}
},
},
];

0 comments on commit 57394ac

Please sign in to comment.