Skip to content

Commit

Permalink
v0.0.1 added
Browse files Browse the repository at this point in the history
  • Loading branch information
thegadget-eth authored Jun 22, 2022
1 parent b2d86c7 commit 47de7c8
Show file tree
Hide file tree
Showing 7 changed files with 416 additions and 0 deletions.
91 changes: 91 additions & 0 deletions commands/surveyByRole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const { SlashCommandBuilder } = require("@discordjs/builders");
var formatDistanceToNow = require("date-fns/formatDistanceToNow");

module.exports = {
data: new SlashCommandBuilder()
.setName("survey")
.setDescription("Send surveys")
.addSubcommand((subcommand) =>
subcommand
.setName("by-role")
.setDescription("Send surveys by role")
.addRoleOption((option) =>
option
.setName("role")
.setDescription("name of the role")
.setRequired(true)
)
.addStringOption((option) =>
option.setName("link").setDescription("survey link").setRequired(true)
)
)
.addSubcommand((subcommand) =>
subcommand
.setName("by-date")
.setDescription("Send surveys by date")
.addStringOption((option) =>
option.setName("link").setDescription("survey link").setRequired(true)
)
.addStringOption((option) =>
option
.setName("since")
.setDescription("date in timestamp (milliseconds)")
.setRequired(true)
)
.addStringOption((option) =>
option
.setName("till")
.setDescription("date in timestamp (milliseconds)")
)
),
async execute(interaction) {
const guild = await interaction.client.guilds.cache.get(
interaction.guildId
);
const Members = await guild.members.cache.map((member) => member);
const link = interaction.options.getString("link");

if (interaction.options._subcommand === "by-role") {
const role = interaction.options.getRole("role");
Members.map(async ({ roles, id, user, joinedTimestamp }) => {
if (roles.cache.some((memberRole) => memberRole.name === role.name)) {
const targetUser = await interaction.client.users.fetch(id);
const message = `Dear ${user}, It's been ${formatDistanceToNow(
joinedTimestamp
)} Since you joined to the ${
guild.name
}. \n\nWe'd love to hear how things are going!\nYour feedback is important to us!\n\nWe use it to ensure our dao is providing proper direction and to ensure we have the opportunity \nto change our approach to better suit your needs if necessary\n\nPlease use the following link to fill out the survey:
\n${link}`;
targetUser.send(message);
}
});
} else {
const since = interaction.options.getString("since");
const till = interaction.options.getString("till");
Members.map(async ({ id, user, joinedTimestamp }) => {
const message = `Dear ${user}, It's been ${formatDistanceToNow(
joinedTimestamp
)} Since you joined to the ${
guild.name
}. \n\nWe'd love to hear how things are going!\nYour feedback is important to us!\n\nWe use it to ensure our dao is providing proper direction and to ensure we have the opportunity \nto change our approach to better suit your needs if necessary\n\nPlease use the following link to fill out the survey:
\n${link}`;
if (!user.bot) {
if (till) {
if (joinedTimestamp < till && joinedTimestamp > since) {
const targetUser = await interaction.client.users.fetch(id);
console.log(targetUser);
targetUser.send(message); // 1655729528883 till
}
} else {
if (joinedTimestamp > since) {
const targetUser = await interaction.client.users.fetch(id);
console.log(targetUser);
targetUser.send(message); // 1654087928883 since
}
}
}
});
}
await interaction.reply({ content: "Surveys sent!!!", ephemeral: true });
},
};
43 changes: 43 additions & 0 deletions deploy-command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require("node:fs");
const path = require("node:path");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
require("dotenv").config();

const commands = [];
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
commands.push(command.data.toJSON());
}

const rest = new REST({ version: "9" }).setToken(process.env.TOKEN);

(async () => {
try {
console.log("Started refreshing application (/) commands.");

await rest.put(
Routes.applicationGuildCommands(
process.env.CLIENT_ID,
process.env.GUILD_ID
),
{
body: commands,
}
);

await rest.put(Routes.applicationCommands(process.env.CLIENT_ID), {
body: commands,
});

console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
})();
20 changes: 20 additions & 0 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module.exports = {
name: "interactionCreate",
async execute(interaction) {
if (!interaction.isCommand()) return;

const command = interaction.client.commands.get(interaction.commandName);

if (!command) return;

try {
await command.execute(interaction);
} catch (error) {
console.error(error);
await interaction.reply({
content: "There was an error while executing this command!",
ephemeral: true,
});
}
},
};
7 changes: 7 additions & 0 deletions events/ready.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
name: 'ready',
once: true,
execute(client) {
console.log(`Ready! Logged in as ${client.user.tag}`);
},
}
36 changes: 36 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require("node:fs");
const path = require("node:path");
const { Client, Collection, Intents } = require("discord.js");
require("dotenv").config();

const intents = new Intents(32767);
const client = new Client({ intents });

client.commands = new Collection();
const commandsPath = path.join(__dirname, "commands");
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
client.commands.set(command.data.name, command);
}

const eventsPath = path.join(__dirname, "events");
const eventFiles = fs
.readdirSync(eventsPath)
.filter((file) => file.endsWith(".js"));

for (const file of eventFiles) {
const filePath = path.join(eventsPath, file);
const event = require(filePath);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args));
} else {
client.on(event.name, (...args) => event.execute(...args));
}
}

client.login(process.env.TOKEN);
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "pulseSurvey",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"@discordjs/rest": "^0.5.0",
"date-fns": "^2.28.0",
"discord-api-types": "^0.34.0",
"discord.js": "^13.8.0"
},
"devDependencies": {
"dotenv": "^16.0.1"
}
}
Loading

0 comments on commit 47de7c8

Please sign in to comment.