A simple event creation snippet. The event is default an EXTERNAL entity type, but feel free to change it. This simple code example uses timestamps to generate custom start and end dates. Once you install your code, simply press run, and it will create the required command for you.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let commandCreated = await lib.keyvalue.store['@0.1.16'].get({
key: `commandcreation`,
defaultValue: false,
});
if (!commandCreated) {
await lib.discord.commands['@0.0.0'].create({
name: 'event',
description: 'Schedules an external event!',
options: [
{
type: 3,
name: 'name',
description: 'The name of your event!',
required: true,
},
{
type: 3,
name: 'description',
description: 'The description for your event.',
required: true,
},
{
type: 3,
name: 'location',
description: 'The location of your event (preferably a link).',
required: true,
},
{
type: 10,
name: 'starttime',
description:
'The minutes it takes to start your event after the command is run.',
required: true,
},
{
type: 10,
name: 'endtime',
description: 'The days after the command is run to end the event.',
required: true,
},
],
});
return lib.keyvalue.store['@0.1.16'].set({
key: `commandcreation`,
value: true,
});
}
const minuteInput = parseInt(context.params.event.data.options[3].value) * 60000; //minutes in ms
const dayInput = parseInt(context.params.event.data.options[4].value) * 86400000; //days in ms
const mins = Date.now() + minuteInput; //Adds the value of the current timestamp and the start time
const startdate = new Date(mins); //Creates a new timestamp for the event to start.
const days = Date.now() + dayInput; //Adds the value of the current timestamp and the end time
const enddate = new Date(days); //Creates a new timestamp for the event to end.
await lib.discord.scheduledevents['@0.0.1'].create({
guild_id: `${context.params.event.guild_id}`,
name: `${context.params.event.data.options[0].value}`,
description: `${context.params.event.data.options[1].value}`,
scheduled_start_time: startdate.toISOString(),
scheduled_end_time: enddate.toISOString(),
privacy_level: 'GUILD_MEMBERS',
entity_type: 'EXTERNAL',
entity_metadata: {location: `${context.params.event.data.options[2].value}`, //the "location" for your event.
},}).catch((err) => {
return lib.discord.interactions['@0.0.0'].followups.ephemeral.create({
token: context.params.event.token,
content:
"An error occurred: Either I don't have permissions to create an event, or you entered invalid start and/or end dates.",
})}); //returns an error message.
await lib.discord.interactions['@0.0.0'].followups.ephemeral.create({
token: context.params.event.token,
content: `Done! Created ${context.params.event.data.options[0].value} with description: "${context.params.event.data.options[1].value}, a start time of ${startdate.toISOString()} and an end date of ${enddate.toISOString()}."`,
}); //returns a success message!