This snippet allows you to have a message send after each new message send by another user. It will automatically delete the previous message send by the bot. Provide the channel ID in the Environment Variables and it will start working! You'll probably want to edit the messages send by the bot though :)
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let channel = process.env.channelID;
let lastSticky = await lib.utils.kv['@0.1.16'].get({
key: `${context.params.event.channel_id}_sticky`,
defaultValue: 0,
});
if (context.params.event.channel_id == channel) {
if (lastSticky !== 0) {
try {
await lib.discord.channels['@0.3.0'].messages.destroy({
message_id: `${lastSticky}`,
channel_id: `${context.params.event.channel_id}`,
});
} catch (error) {
console.error(
`The message does not exist anymore. This is most likely because someone deleted the message manually.`
);
}
await lib.utils.kv['@0.1.16'].clear({
key: `${context.params.event.channel_id}_sticky`,
});
}
let newSticky = // if you want to edit the message send by the bot, use the embed builder: https://autocode.com/tools/discord/embed-builder/ and replace the code below by that code (except the const lib = ... stuff)
await lib.discord.channels['@0.2.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `Sticky message`,
description: `This is a message that will appear every time someone sends a message in this channel! You can edit my contents in the [Autocode project](https://autocode.com)!`,
color: 0x00ffff,
},
],
});
// do not touch this though :)
await lib.utils.kv['@0.1.16'].set({
key: `${context.params.event.channel_id}_sticky`,
value: `${newSticky.id}`,
});
}