Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build/commands/pickup.command.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ exports.default = {
await interaction.reply("ony works in server");
return;
}
await (0, jobs_1.DailyPickupsWithoutThread)(interaction.guild, services);
await interaction.reply("done");
await (0, jobs_1.DailyPickupsWithoutThread)(interaction.guild, services, interaction);
}
};
101 changes: 60 additions & 41 deletions build/jobs/daily-pickups.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,79 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DailyPickupsThread = exports.DailyPickupsWithoutThread = exports.DailyPickupsUsingEmbed = void 0;
exports.DailyPickupsWithoutThread = exports.PickupsRefreshEvent = void 0;
const discord_js_1 = require("discord.js");
const service_1 = require("../service");
const nm_const_1 = require("../nm-const");
async function DailyPickupsUsingEmbed(guild, services) {
const channel = (0, service_1.getChannelByName)(guild, today());
const pickups = await services.pickupsDataService.getPickupsFor(today());
const pickupDescriptions = await Promise.all(pickups.map(async (pickup) => `* [${pickup.org}](https://www.example.com) at ${pickup.time}: ${await getVolunteerList(services, pickup)}`));
const embed = new discord_js_1.EmbedBuilder()
.setColor(0x0099ff)
.setTitle(`pickups`)
.setDescription(pickupDescriptions.join('\n'));
channel.send({ embeds: [embed] });
}
exports.DailyPickupsUsingEmbed = DailyPickupsUsingEmbed;
async function DailyPickupsWithoutThread(guild, services) {
const channel = (0, service_1.getChannelByName)(guild, today());
// ping everyone signed up to help with today
const roleId = await getRoleByName(guild, today()).then((role) => role.id);
channel.send(`Lets go ${(0, discord_js_1.roleMention)(roleId)}!`);
const link = 'https://davisnightmarket.github.io/';
// list all the pick ups happening today
const pickups = await services.pickupsDataService.getPickupsFor(today());
const PickupsRefreshEvent = (sevicesConfig) => async (interaction) => {
// Is this our interaction to deal with?
interaction = interaction;
const { customId } = interaction;
if (!customId)
return;
const [name, day] = customId.split('--');
if (name !== "pickups-refresh")
return;
// Get em services
const guild = interaction.guild;
if (!guild)
return;
const services = await sevicesConfig.getServicesForGuildId(guild.id);
// make sure we have the most up to date info
await services.pickupsDataService.updateCache();
// regenerate the message
const roleId = await getRoleByName(guild, day);
let message = `## ${(0, discord_js_1.roleMention)(roleId)} pickups!\n`;
const pickups = await services.pickupsDataService.getPickupsFor(day);
for (const pickup of pickups) {
channel.send([
`${(0, discord_js_1.bold)(pickup.org)} at ${pickup.time}: ${await getVolunteerList(services, pickup)}`
].join('\n'));
message += `> ${(0, discord_js_1.bold)(pickup.org)} at ${pickup.time}: ${await getVolunteerList(guild, services, pickup)}\n`;
}
}
exports.DailyPickupsWithoutThread = DailyPickupsWithoutThread;
async function DailyPickupsThread(guild, services) {
const thread = await createTodaysPickupThread(guild);
// update it
interaction.message.edit(message);
};
exports.PickupsRefreshEvent = PickupsRefreshEvent;
async function DailyPickupsWithoutThread(guild, services, interaction) {
// ping everyone signed up to help with today
const roleId = await getRoleByName(guild, today()).then((role) => role.id);
thread.send(`Lets go ${(0, discord_js_1.roleMention)(roleId)}!`);
const roleId = await getRoleByName(guild, today());
// list all the pick ups happening today
const pickups = await services.pickupsDataService.getPickupsFor(today());
let message = `## ${(0, discord_js_1.roleMention)(roleId)} pickups!\n`;
for (const pickup of pickups) {
thread.send([
`${(0, discord_js_1.bold)(pickup.org)} at ${pickup.time}. ${pickup.comments ?? ''}`,
``,
`people helping: ${await getVolunteerList(services, pickup)}`,
``
].join('\n'));
message += `> ${(0, discord_js_1.bold)(pickup.org)} at ${pickup.time}: ${await getVolunteerList(guild, services, pickup)}\n`;
}
if (interaction) {
const editButton = new discord_js_1.ButtonBuilder()
.setCustomId(`pickups-edit--${today()}`)
.setLabel('Edit')
.setStyle(discord_js_1.ButtonStyle.Primary);
const helpButton = new discord_js_1.ButtonBuilder()
.setCustomId(`pickups-help--${today()}`)
.setLabel('Help')
.setStyle(discord_js_1.ButtonStyle.Success);
const refreshButton = new discord_js_1.ButtonBuilder()
.setCustomId(`pickups-refresh--${today()}`)
.setLabel('Refresh')
.setStyle(discord_js_1.ButtonStyle.Secondary);
const row = new discord_js_1.ActionRowBuilder()
.addComponents(editButton, helpButton, refreshButton);
interaction.reply({
content: message,
components: [row],
});
}
else {
const channel = (0, service_1.getChannelByName)(guild, today());
channel.send(message);
}
}
exports.DailyPickupsThread = DailyPickupsThread;
async function getVolunteerList(services, pickup) {
exports.DailyPickupsWithoutThread = DailyPickupsWithoutThread;
async function getVolunteerList(guild, services, pickup) {
const people = await Promise.all([pickup.volunteer1, pickup.volunteer2, pickup.volunteer3]
.filter((name) => name !== undefined && name !== '')
.filter((name) => name !== undefined && name !== '' && name !== "NEEDED")
.map(async (name) => (await services.personCoreService.getPerson({ name })) ?? {
name
}));
if (people.length === 0)
return (0, discord_js_1.bold)('NEEDED');
return (0, discord_js_1.roleMention)(await getRoleByName(guild, "NEEDED"));
return people
.filter((person) => !!person)
.map((person) => person.discordId ? (0, discord_js_1.userMention)(person.discordId) : person.name)
Expand All @@ -68,8 +87,8 @@ async function createTodaysPickupThread(guild) {
async function getRoleByName(guild, name) {
const role = guild.roles.cache.find((role) => role.name === name);
if (!role)
return await guild.roles.create({ name });
return role;
return await guild.roles.create({ name }).then((role) => role.id);
return role.id;
}
function today() {
return nm_const_1.DAYS_OF_WEEK[new Date().getDay()];
Expand Down
2 changes: 2 additions & 0 deletions build/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ async function main() {
// food count events
client.on(discord_js_1.Events.MessageCreate, (0, events_1.FoodCountInputEvent)(services));
client.on(discord_js_1.Events.InteractionCreate, events_1.FoodCountResponseEvent);
// pickup events
client.on(discord_js_1.Events.InteractionCreate, (0, jobs_1.PickupsRefreshEvent)(services));
// commands
client.on(discord_js_1.Events.InteractionCreate, commands.execute(services));
const { discordConfig: { appToken } } = await (0, nm_secrets_utility_1.GetNmSecrets)();
Expand Down
3 changes: 3 additions & 0 deletions build/nm-service/nm-person-data.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ class NmPersonDataService {
async setActiveState(email, status) {
await this.personSheetService.update({ email }, { status });
}
async updateCache() {
await this.personSheetService.updateCache();
}
}
exports.NmPersonDataService = NmPersonDataService;
3 changes: 3 additions & 0 deletions build/nm-service/nm-pickups-data.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ class NmPickupsDataService {
.filter((pickup) => pickup.day === day)
.filter((pickup) => pickup.activity === 'food pickup'));
}
async updateCache() {
await this.pickupsSheetService.updateCache();
}
}
exports.NmPickupsDataService = NmPickupsDataService;
6 changes: 3 additions & 3 deletions src/commands/pickup.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
await interaction.reply("ony works in server")
return
}
await DailyPickupsWithoutThread(interaction.guild, services)
await interaction.reply("done")

await DailyPickupsWithoutThread(interaction.guild, services, interaction)
}
}
}
1 change: 0 additions & 1 deletion src/events/food-count-input.event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { v4 as uuidv4 } from 'uuid';
import { type ConfigSerive, MessageService } from '../service/index';
import { Dbg, CacheUtility } from '../utility';
import { getChannelByName } from '../service/discord.service';
import { type GuildServiceMapModel } from '../model';

// status for each cached input: does it get inserted unless cancel? or does it require a confirmation?
type CacheStatusType = 'INSERT_UNLESS_CANCEL' | 'DELETE_UNLESS_CONFIRM';
Expand Down
143 changes: 76 additions & 67 deletions src/jobs/daily-pickups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,92 +3,101 @@ import {
type Guild,
bold,
userMention,
EmbedBuilder,
type ChatInputCommandInteraction,
ActionRowBuilder,
ButtonBuilder,
ButtonStyle,
type ButtonInteraction,
type Interaction,
} from 'discord.js';
import { getChannelByName } from '../service';
import { type ConfigSerive, getChannelByName } from '../service';
import { DAYS_OF_WEEK } from '../nm-const';
import { type GuildServiceModel } from '../model';
import { type DayNameType, type GuildServiceModel } from '../model';
import { type PersonModel, type PickUp } from '../nm-service';

export async function DailyPickupsUsingEmbed(
guild: Guild,
services: GuildServiceModel
) {
const channel = getChannelByName(guild, today());

const pickups = await services.pickupsDataService.getPickupsFor(today());
const pickupDescriptions = await Promise.all(
pickups.map(
async (pickup) =>
`* [${pickup.org}](https://www.example.com) at ${
pickup.time
}: ${await getVolunteerList(services, pickup)}`
)
);

const embed = new EmbedBuilder()
.setColor(0x0099ff)
.setTitle(`pickups`)
.setDescription(pickupDescriptions.join('\n'));

channel.send({ embeds: [embed] });
export const PickupsRefreshEvent = (sevicesConfig: ConfigSerive) => async (interaction: Interaction) => {
// Is this our interaction to deal with?
interaction = interaction as ButtonInteraction;
const { customId } = interaction;
if (!customId) return;
const [name, day] = customId.split('--');
if (name !== "pickups-refresh") return;

// Get em services
const guild = interaction.guild
if (!guild) return
const services = await sevicesConfig.getServicesForGuildId(guild.id)

// make sure we have the most up to date info
await services.pickupsDataService.updateCache()

// regenerate the message
const roleId = await getRoleByName(guild, day);
let message = `## ${roleMention(roleId)} pickups!\n`
const pickups = await services.pickupsDataService.getPickupsFor(day as DayNameType);
for (const pickup of pickups) {
message += `> ${bold(pickup.org)} at ${
pickup.time
}: ${await getVolunteerList(guild, services, pickup)}\n`
}

// update it
interaction.message.edit(message)
}

export async function DailyPickupsWithoutThread(
guild: Guild,
services: GuildServiceModel
services: GuildServiceModel,
interaction?: ChatInputCommandInteraction,
) {
const channel = getChannelByName(guild, today());

// ping everyone signed up to help with today
const roleId = await getRoleByName(guild, today()).then((role) => role.id);
channel.send(`Lets go ${roleMention(roleId)}!`);

const link = 'https://davisnightmarket.github.io/';
const roleId = await getRoleByName(guild, today());

// list all the pick ups happening today
const pickups = await services.pickupsDataService.getPickupsFor(today());
for (const pickup of pickups) {
channel.send(
[
`${bold(pickup.org)} at ${
pickup.time
}: ${await getVolunteerList(services, pickup)}`
].join('\n')
);
}
}

export async function DailyPickupsThread(
guild: Guild,
services: GuildServiceModel
) {
const thread = await createTodaysPickupThread(guild);

// ping everyone signed up to help with today
const roleId = await getRoleByName(guild, today()).then((role) => role.id);
thread.send(`Lets go ${roleMention(roleId)}!`);
let message = `## ${roleMention(roleId)} pickups!\n`

// list all the pick ups happening today
const pickups = await services.pickupsDataService.getPickupsFor(today());
for (const pickup of pickups) {
thread.send(
[
`${bold(pickup.org)} at ${pickup.time}. ${
pickup.comments ?? ''
}`,
``,
`people helping: ${await getVolunteerList(services, pickup)}`,
``
].join('\n')
);
message += `> ${bold(pickup.org)} at ${
pickup.time
}: ${await getVolunteerList(guild, services, pickup)}\n`
}

if (interaction) {
const editButton = new ButtonBuilder()
.setCustomId(`pickups-edit--${today()}`)
.setLabel('Edit')
.setStyle(ButtonStyle.Primary);

const helpButton = new ButtonBuilder()
.setCustomId(`pickups-help--${today()}`)
.setLabel('Help')
.setStyle(ButtonStyle.Success);

const refreshButton = new ButtonBuilder()
.setCustomId(`pickups-refresh--${today()}`)
.setLabel('Refresh')
.setStyle(ButtonStyle.Secondary);

const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(editButton, helpButton, refreshButton);

interaction.reply({
content: message,
components: [row],
})
} else {
const channel = getChannelByName(guild, today());
channel.send(message)
}
}

async function getVolunteerList(services: GuildServiceModel, pickup: PickUp) {
async function getVolunteerList(guild: Guild, services: GuildServiceModel, pickup: PickUp) {
const people = await Promise.all(
[pickup.volunteer1, pickup.volunteer2, pickup.volunteer3]
.filter((name) => name !== undefined && name !== '')
.filter((name) => name !== undefined && name !== '' && name !== "NEEDED")
.map(
async (name) =>
(await services.personCoreService.getPerson({ name })) ?? {
Expand All @@ -97,7 +106,7 @@ async function getVolunteerList(services: GuildServiceModel, pickup: PickUp) {
)
);

if (people.length === 0) return bold('NEEDED');
if (people.length === 0) return roleMention(await getRoleByName(guild, "NEEDED"));

return people
.filter((person): person is PersonModel => !!person)
Expand All @@ -115,8 +124,8 @@ async function createTodaysPickupThread(guild: Guild) {

async function getRoleByName(guild: Guild, name: string) {
const role = guild.roles.cache.find((role) => role.name === name);
if (!role) return await guild.roles.create({ name });
return role;
if (!role) return await guild.roles.create({ name }).then((role) => role.id);
return role.id;
}

function today() {
Expand Down
6 changes: 4 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ import {
Partials
} from 'discord.js';
import { GetNmSecrets } from './utility/nm-secrets.utility';
import { type GuildServiceMapModel } from './model';
import { AddCron } from './utility/cron-utility';
import { FoodCountReminder, DailyPickupsWithoutThread } from './jobs';
import { FoodCountReminder, PickupsRefreshEvent } from './jobs';
import { CommandSerice, ConfigSerive } from './service';

async function main() {
Expand Down Expand Up @@ -49,6 +48,9 @@ async function main() {
client.on(Events.MessageCreate, FoodCountInputEvent(services));
client.on(Events.InteractionCreate, FoodCountResponseEvent);

// pickup events
client.on(Events.InteractionCreate, PickupsRefreshEvent(services));

// commands
client.on(Events.InteractionCreate, commands.execute(services))

Expand Down
2 changes: 0 additions & 2 deletions src/model/guild-service.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ export interface GuildServiceModel {
personCoreService: NmPersonDataService;
pickupsDataService: NmPickupsDataService;
}

export type GuildServiceMapModel = Record<string, GuildServiceModel>;
Loading