Creates a thread under every message in a channel and tries to pick a good name for it based on the message content. You need to set the channel ID in the event trigger. (environment variables didn't work for that)
// !!! set the channel ID in the event trigger !!!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let event = context.params.event;
//thread management-------------------------------------------------------------
let threadname = event.content;
if (threadname == '' || threadname.startsWith('http')) {
//fallback thread title
threadname = `post by ${event.author.username}`;
} else {
//replace all mentions with the respective usernames
threadname = threadname.split('>');
for (let i = 0; event.mentions[i] != undefined; i++) {
threadname[i] = threadname[i].slice(0, -20);
threadname[i] = threadname[i] + ` ${event.mentions[i].username}`;
}
threadname = threadname.join('');
//remove link from threadname, if there's a link after the message
if (threadname.includes('http')) {
threadname = threadname.split('http');
threadname = threadname[0];
}
//make sure the threadname doesn't get too long and gets cut off at a good spot
if (
threadname.length > 34 && //if too long
threadname.slice(0, 34).split(' ')[1] != undefined //and not a connected word
) {
threadname = threadname.slice(0, 34).split(' ');
threadname.pop(); //remove last word
threadname = threadname.join(' ');
} else {
threadname = threadname.slice(0, 34);
}
}
//depending on your server boost level, different thread timeouts are possible.
//this part tries to use the longest possible duration up to 3 days
try {
await lib.discord.channels['@0.2.0'].threads.create({
channel_id: event.channel_id,
name: threadname,
auto_archive_duration: 1440 * 3,
message_id: `${event.id}`,
type: 'GUILD_PUBLIC_THREAD',
});
} catch {
console.log('3 days duration not possible');
try {
await lib.discord.channels['@0.2.0'].threads.create({
channel_id: event.channel_id,
name: threadname,
auto_archive_duration: 1440,
message_id: `${event.id}`,
type: 'GUILD_PUBLIC_THREAD',
});
} catch {
console.log('unable to create thread as reply');
await lib.discord.channels['@0.2.0'].threads.create({
channel_id: event.channel_id,
name: threadname,
auto_archive_duration: 1440,
type: 'GUILD_PUBLIC_THREAD',
});
}
}
console.log(`thread created`);
//here are some standard reactions added by the bot to fake social media
//likes and retweets
await lib.discord.channels['@0.2.2'].messages.reactions.create({
emoji: `❤️`,
message_id: event.id,
channel_id: event.channel_id,
});
return lib.discord.channels['@0.2.2'].messages.reactions.create({
emoji: `🔁`,
message_id: event.id,
channel_id: event.channel_id,
});