Skip to content

Commit

Permalink
fix connections issue + allow custom emoji set
Browse files Browse the repository at this point in the history
  • Loading branch information
dispherical committed Jun 28, 2024
1 parent 0daa697 commit a850a62
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 34 deletions.
5 changes: 3 additions & 2 deletions commands/optout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const prisma = new PrismaClient();
*/
module.exports = async function ({ app }) {
app.command("/optout-library", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await ack();
const channelId = body.channel_id;
const channel = await app.client.conversations.info({
Expand All @@ -15,7 +16,7 @@ module.exports = async function ({ app }) {
});
if (command.user_id != channel.channel.creator || !user.user.is_admin)
return await respond(
"Only channel managers and workspace admins can opt a user out.",
"Only channel managers and workspace admins can opt a channel out.",
);
const channelRecord = await prisma.channel.findFirst({
where: {
Expand Down Expand Up @@ -56,6 +57,6 @@ module.exports = async function ({ app }) {
channel: channel.channel.id,
});

await respond(`${command.text}`);
await prisma.$disconnect()
});
};
70 changes: 70 additions & 0 deletions commands/setemoji.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

const emojis = require("../utils/emojis")
/**
* @param {{app: import('@slack/bolt').App}} param1
*/
module.exports = async function ({ app }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/dev-setemoji", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await ack();
if (!command.text)
return await respond(
"Please provide an emoji. I.e. /setemoji 🎒 or /setemoji :hackclub:",
);
const channelId = body.channel_id;
const channel = await app.client.conversations.info({
channel: body.channel_id,
});
const user = await app.client.users.info({
user: command.user_id,
});
if (command.user_id != channel.channel.creator && !user.user.is_admin)
return await respond(
"Only channel managers and workspace admins can opt .",
);
const channelRecord = await prisma.channel.findFirst({
where: {
id: channelId,
},
});
if (!command.text && !command.text.match(/:[a-zA-Z0-9_-]+:/) && !emojis.includes(command.text))
return await respond("Please provide an emoji")


if (!channel.channel.is_member)
try {
await app.client.conversations.join({
channel: channel.channel.id,
});
} catch (e) { }
if (!channelRecord)
await prisma.channel.create({
data: {
id: channelId,
emoji: command.text
},
});
else if (channelRecord.locked && !user.user.is_admin)
return await respond(
"This channel cannot be locked as it is locked in the database. Usually, this is because it is a public, community-owned channel, i.e. #lounge, #code, etc.",
);

await prisma.channel.update({
where: {
id: channelId,
},
data: {
optout: false,
emoji: command.text
},
});
await app.client.chat.postEphemeral({
channel: channel.channel.id,
user: command.user_id,
text: `Set your channel emoji to ${command.text}`,
});
await prisma.$disconnect()
});
};
17 changes: 10 additions & 7 deletions commands/setlocation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();


/**
* @param {{app: import('@slack/bolt').App}} param1
*/
module.exports = async function ({ app }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/setlocation", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await ack();
if (!command.text)
return await respond(
Expand All @@ -30,18 +32,18 @@ module.exports = async function ({ app }) {
var locations = await (
await fetch(
`https://nominatim.openstreetmap.org/search?` +
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
)
).json();
if (!channel.channel.is_member)
try {
await app.client.conversations.join({
channel: channel.channel.id,
});
} catch (e) {}
} catch (e) { }
if (!locations.length)
return await respond(
"No locations found. If this keeps failing, please add it to OpenStreetMaps.",
Expand Down Expand Up @@ -75,5 +77,6 @@ module.exports = async function ({ app }) {
user: command.user_id,
text: `Set your channel location to \`${display_name}\`. Others near you will be able to discover your channel via their location.`,
});
await prisma.$disconnect()
});
};
15 changes: 9 additions & 6 deletions commands/setuserlocation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();


/**
* @param {{app: import('@slack/bolt').App}} param1
*/
module.exports = async function ({ app }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
app.command("/setuserlocation", async ({ command, body, ack, respond }) => {
await prisma.$connect()
await ack();
if (!command.text)
return await respond(
Expand All @@ -20,10 +22,10 @@ module.exports = async function ({ app }) {
var locations = await (
await fetch(
`https://nominatim.openstreetmap.org/search?` +
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
new URLSearchParams({
q: command.text.trim(),
format: "jsonv2",
}),
)
).json();
if (!locations.length)
Expand Down Expand Up @@ -54,5 +56,6 @@ module.exports = async function ({ app }) {
user: command.user_id,
text: `Set your user location to \`${display_name}\`.`,
});
await prisma.$disconnect()
});
};
9 changes: 5 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const cron = require("node-cron");
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: !Boolean(process.env.PORT),
socketMode: process.env.PORT ? false : true,
appToken: process.env.SLACK_APP_TOKEN,
port: process.env.PORT,
});
Expand All @@ -23,9 +23,10 @@ Array.prototype.random = function () {

// Load commands

require("./commands/optout")({ app, client });
require("./commands/setlocation")({ app, client });
require("./commands/setuserlocation")({ app, client });
await require("./commands/optout")({ app, client });
await require("./commands/setlocation")({ app, client });
await require("./commands/setuserlocation")({ app, client });
await require("./commands/setemoji")({ app, client });

// This deletes and sends a new message to bypass the 10 day editing limit

Expand Down
9 changes: 7 additions & 2 deletions sections/30-recent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const pms = require("pretty-ms");
const utils = require("../utils");
const util = require("util");
const generateFullTimeline = require("../utils/allTimeline");
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();

//const timeline = require("../utils/timeline.disabled.js")
module.exports = {
title: "🆕 Most Recent Activity",
Expand All @@ -12,6 +11,9 @@ module.exports = {
* @param {{app: import('@slack/bolt').App}} param1
*/
render: async function ({ app, client }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
await prisma.$connect()
var messages = await app.client.search.messages({
query: utils.queries.topChannels,
sort: "timestamp",
Expand Down Expand Up @@ -52,12 +54,15 @@ module.exports = {
// (${await timeline({ app, channel })})
}),
).then((texts) => texts.join(""));
await prisma.$disconnect();

return (
`This is a list of conversations that are actively ongoing and that you can jump in at any time and meet new people :yay:\n\n:siren-real: Latest message: (in <#${messages.messages.matches[0].channel.id}>) ${pms(Date.now() - Math.floor(messages.messages.matches[0].ts * 1000))} ago
${await generateFullTimeline(channels)}
` + text
);

},
};
16 changes: 9 additions & 7 deletions utils/allTimeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ module.exports = async function generateFullTimeline(messages) {
const startTime = Math.min(...Object.keys(intervalMessages).map(Number));
const endTime = Math.max(...Object.keys(intervalMessages).map(Number));
let output = "";
await prisma.$connect()

for (let time = startTime; time <= endTime; time += 10) {
if (intervalMessages[time]) {
for (const channelId of intervalMessages[time]) {
var emoji = "💥"
var emoji = "💥,"
const channelRecord = await prisma.channel.findFirst({
where: {
id: channelId
Expand All @@ -31,16 +32,17 @@ module.exports = async function generateFullTimeline(messages) {
output += emoji
continue
}
output += channelRecord.emoji
}
output += channelRecord.emoji + ","
}
} else {
output += "-";
output += "-,";
}
}

let chars = [...output];
let chars = output.split(",");
chars = chars
.map((char) => (emojis.includes(char) || char == "-" ? char : ""))
.slice(chars.length - 20);
return chars.join("");
.slice(chars.length - 40, chars.length);
await prisma.$disconnect()
return chars.join(",").replaceAll(",", "");
};
29 changes: 23 additions & 6 deletions utils/joinall.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

/**
* @param {{app: import('@slack/bolt').App}} param1
*/
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
/**
* @param {{app: import('@slack/bolt').App}} param1
*/
const emojis = require("./emojis");
Array.prototype.random = function () {
return this[Math.floor(Math.random() * this.length)];
};

module.exports = async function ({ app, client }) {
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
await prisma.$connect()

async function rake(cursor) {
const convos = await app.client.conversations.list({
limit: 999,
Expand All @@ -31,6 +36,14 @@ module.exports = async function ({ app, client }) {
await app.client.conversations.join({
channel: channel.id,
});
if (!channelRecord){
await prisma.channel.create({
data:{
id: channel.id,
emoji: emojis.random()
}
})
}
console.log(`Joined ${channel.name_normalized} (${channel.id})`);
setTimeout(resolve, 1200);
} catch (e) {
Expand All @@ -45,7 +58,11 @@ module.exports = async function ({ app, client }) {
setTimeout(async function () {
await rake(convos.response_metadata.next_cursor);
}, 3000);
else console.log("Finished joining all channels");
else {
console.log("Finished joining all channels");
await prisma.$disconnect()
}

}
rake();
};

0 comments on commit a850a62

Please sign in to comment.