lets a user submit an attachment, stores the url in a sheet and sends the file in the current channel
/*
this command is made for users to submit a single image for an event in an
event channel.
it was set up for only 1 submission, but if you set maxSubmissions to a value
higher than 1, the topmost submission in the google sheet should get replaced
once the submission limit has been reached.
to set up a slash command for this, go to
https://autocode.com/tools/discord/command-builder/
and create the following command:
command name: submit
option 1 type: attachment (required) //upload a file here
write descriptions as you see fit
to set up a google sheet for this, create the columns with the following titles
and link the spreadsheet:
uid //stores the user ID
name //stores the user name
submission //the submission file link goes here
channel //channel ID where it was submitted
messageID //ID of the bot message that posted the image
credits to kangabru for some of the attachment code:
https://autocode.com/kangabru/snippets/cachsnpt_AYWukZqwgWADWJbv2hE421GW78Ws7zM8azzn/
and vestrius for the image buffering:
https://autocode.com/vestrius/snippets/cachsnpt_FVF5Yqm64aUAWxsaF76fDinJpCp7nWV8A1V7/
*/
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let maxSubmissions = 1 //set to 0 for unlimited submissions
let channel = await lib.discord.channels['@0.3.2'].retrieve({
channel_id: `${context.params.event.channel_id}`
});
//only allows text channels
if (channel.type != 0) {
return lib.discord.interactions['@1.0.1'].followups.ephemeral.create({
token: `${context.params.event.token}`,
content: `please use this in a server text channel instead.`
});
}
let user = context.params.event.member.user.id;
let username = context.params.event.member.user.username;
console.log(username + ' submitted in:' + channel.name);
// resend file and get the url /////////////////////////////////////////////////
let attachmentid = context.params.event.data.options[0]?.value;
let attachmentData =
context.params.event.data?.resolved?.attachments?.[attachmentid];
let submiturl = attachmentData.url
let title = attachmentData.filename
console.log(title);
let buffer = await lib.http.request['@1.1.5']({
method: 'GET',
url: `${submiturl}`, //this is the link where the file will be grabbed from
}).then((result) => {
return result.body;
});
let result = await lib.discord.channels['@0.3.2'].messages.create({
channel_id: `${channel.id}`,
content: `Submission by <@${user}>`,
attachments: [
{
filename: title,
file: Buffer.from(buffer),
}
]
});
let finalurl = result.attachments[0].url
console.log(finalurl);
// create thread and default reactions /////////////////////////////////////////
await lib.discord.channels['@0.3.2'].threads.create({
channel_id: `${channel.id}`,
name: `Submission by ${username}`,
auto_archive_duration: 1440,
message_id: result.id,
type: 'GUILD_PUBLIC_THREAD'
});
console.log(`thread created`);
await lib.discord.channels['@0.2.2'].messages.reactions.create({
emoji: `❤️`,
message_id: result.id,
channel_id: channel.id,
});
return lib.discord.channels['@0.2.2'].messages.reactions.create({
emoji: `🔁`,
message_id: result.id,
channel_id: channel.id,
});
// update the google sheet /////////////////////////////////////////////////////
let database = await lib.googlesheets.query['@0.3.0'].select({
range: `A:E`,
bounds: 'FIRST_EMPTY_ROW',
where: [{
uid: user,
channel: channel.name,
}],
limit: {
'count': 0,
'offset': 0
}
});
if (database.rows[maxSubmissions-1]==undefined || maxSubmissions<=0) {
console.log(`inserting new`);
await lib.googlesheets.query['@0.3.0'].insert({
range: `A:E`,
fieldsets: [
{
uid: user,
name: username,
submission: finalurl,
channel: channel.name ,
messageID: result.id,
},
]
});
}
else {
console.log(`deleting the old message + thread`);
await lib.discord.channels['@0.3.2'].messages.destroy({
message_id: database.rows[0].fields['messageID'],
channel_id: channel.id
});
await lib.discord.channels['@0.3.2'].destroy({
channel_id: database.rows[0].fields['messageID']
});
console.log(`replacing sheet entry`);
await lib.googlesheets.query['@0.3.0'].update({
range: `A:E`,
bounds: 'FIRST_EMPTY_ROW',
where: [{
channel: channel.name,
messageID: database.rows[0].fields['messageID'],
}],
limit: {
'count': 0,
'offset': 0
},
fields: {
uid: user,
name: username,
submission: finalurl,
channel: channel.name ,
messageID: result.id,
},
});
}