Skip to main content
Version: 0.10

Triggers

Build automatic actions into your app.

A trigger is an action you can build into your app that will occur automatically when the trigger condition is met.

Setup triggers

Setup triggers enable your app to automatically respond when a user is installing or configuring your app. These triggers are supported:

  • AppInstall
  • AppUpgrade

Event triggers

Event triggers let your app automatically respond to a user’s action. For example, if you set the OnSubredditSubscribe trigger, the app will automatically respond when a user joins the community. These triggers are supported:

  • CommentCreate
  • CommentDelete
  • CommentReport
  • CommentSubmit
  • CommentUpdate
  • ModAction
  • ModMail
  • PostCreate
  • PostDelete
  • PostFlairUpdate
  • PostReport
  • PostSubmit
  • PostUpdate

This example adds event triggers that will automatically execute your app. Once a trigger is added, your app listens for the event and the event handler executes the action.

import { Devvit } from '@devvit/public-api';

// Logging on a PostSubmit event
Devvit.addTrigger({
event: 'PostSubmit', // Event name from above
onEvent: async (event) => {
console.log(`Received OnPostSubmit event:\n${JSON.stringify(event)}`);
},
});

// Logging on multiple events: PostUpdate and PostReport
Devvit.addTrigger({
events: ['PostUpdate', 'PostReport'], // An array of events
onEvent: async (event) => {
if (event.type == 'PostUpdate') {
console.log(`Received OnPostUpdate event:\n${JSON.stringify(request)}`);
} else if (event.type === 'PostReport') {
console.log(`Received OnPostReport event:\n${JSON.stringify(request)}`);
}
},
});

export default Devvit;
note

Be careful when creating recursive triggers (like a comment trigger that creates a comment). This could cause your app to crash. To avoid this, check to see if the content creator is the app.

Modmail trigger

The modmail trigger alerts the mod when modmail is sent or received. This example enables the app to listen to modmail events and fetch the relevant message payload via the Reddit API wrapper.

import { Devvit } from '@devvit/public-api';

Devvit.configure({ redditAPI: true });

Devvit.addTrigger({
event: 'ModMail',
onEvent: async (event, context) => {
// Apps receive this event when:
// 1. A new modmail conversation thread is created
// 2. A new modmail message is added to an existing conversation
console.log(`Received modmail trigger event:\n${JSON.stringify(event)}`);

// Example conversation ID: ModmailConversation_1mzkfh
// We are fetching the latest conversation state from Reddit API
const conversationId = event.conversationId;
const result = await context.reddit.modMail.getConversation({
conversationId: conversationId,
markRead: false,
});
console.log(`Received conversation with subject: ${result.conversation?.subject}`);

// Looking up the incoming message from trigger event
// Example Message ID: ModmailMessage_2ch154
const messageId = event.messageId.split('_')[1];
const message = result.messages[messageId];
console.log(`Received modmail message: ${JSON.stringify(message)}`);
},
});

export default Devvit;

Mod actions

Mod actions are another kind of trigger that are just for mods. These triggers show up in the mod log. Check out the list of available mod actions, and if you don't see an action you want, let us know in r/devvit modmail.