Intiial commit
This commit is contained in:
140
src/commands/locations/router.ts
Normal file
140
src/commands/locations/router.ts
Normal file
@@ -0,0 +1,140 @@
|
||||
import * as StarKitten from '@star-kitten/discord';
|
||||
import type { LocationsState } from './state';
|
||||
import { getDB } from '@/lib/db';
|
||||
import type { StructureType } from '@/lib/db/types/routes';
|
||||
|
||||
export enum Page {
|
||||
main = 'main',
|
||||
addLocationModal = 'add-location-modal',
|
||||
addLocationModalSubmit = 'add-location-modal-submit',
|
||||
editLocationModal = 'edit-location-modal',
|
||||
editLocationModalSubmit = 'edit-location-modal-submit',
|
||||
editServicesModal = 'edit-services-modal',
|
||||
editServicesModalSubmit = 'edit-services-modal-submit',
|
||||
removeLocationModal = 'remove-location-modal',
|
||||
removeLocationModalSubmit = 'remove-location-modal-submit',
|
||||
}
|
||||
|
||||
export default function (ctx: StarKitten.PageContext<LocationsState>): Page {
|
||||
switch (ctx.custom_id) {
|
||||
case Page.addLocationModal:
|
||||
ctx.state.data.selected = undefined;
|
||||
return Page.addLocationModal;
|
||||
|
||||
case Page.editLocationModalSubmit:
|
||||
case Page.addLocationModalSubmit: {
|
||||
if (!ctx.interaction.isModalSubmit()) {
|
||||
throw new Error('Expected a modal submit interaction.');
|
||||
}
|
||||
|
||||
const location: any = {};
|
||||
for (const component of ctx.interaction.data.components) {
|
||||
if (StarKitten.isModalLabel(component)) {
|
||||
if (StarKitten.isModalTextInput(component.component)) {
|
||||
if (StarKitten.componentHasIdPrefix(component.component, 'loc-id')) {
|
||||
location.location_id = component.component.value.trim();
|
||||
} else if (StarKitten.componentHasIdPrefix(component.component, 'loc-name')) {
|
||||
location.name = component.component.value.trim();
|
||||
} else if (StarKitten.componentHasIdPrefix(component.component, 'loc-short-name')) {
|
||||
location.short_name = component.component.value.trim();
|
||||
} else if (StarKitten.componentHasIdPrefix(component.component, 'loc-system')) {
|
||||
location.system = component.component.value.trim();
|
||||
}
|
||||
} else if (
|
||||
StarKitten.isStringSelectMenu(component.component) &&
|
||||
StarKitten.componentHasIdPrefix(component.component, 'loc-structure-type')
|
||||
) {
|
||||
location.structure_type = component.component.values[0] as StructureType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.custom_id === Page.addLocationModalSubmit ? getDB().addLocation(location) : getDB().updateLocation(location);
|
||||
ctx.state.data.selected = undefined;
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
case Page.editLocationModal: {
|
||||
if (!ctx.interaction.isSelectMenu()) {
|
||||
console.error('Expected a message component interaction.');
|
||||
return Page.main;
|
||||
}
|
||||
const data = ctx.interaction.data;
|
||||
const locationId = Number.parseInt(data.values[0]);
|
||||
const location = getDB().getLocationById(locationId);
|
||||
if (location) {
|
||||
ctx.state.data.selected = location;
|
||||
return Page.editLocationModal;
|
||||
}
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
case Page.editServicesModal: {
|
||||
if (!ctx.interaction.isSelectMenu()) {
|
||||
console.error('Expected a message component interaction.');
|
||||
return Page.main;
|
||||
}
|
||||
const data = ctx.interaction.data;
|
||||
const locationId = Number.parseInt(data.values[0]);
|
||||
const location = getDB().getLocationById(locationId);
|
||||
if (location) {
|
||||
ctx.state.data.selected = location;
|
||||
return Page.editServicesModal;
|
||||
}
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
case Page.editServicesModalSubmit: {
|
||||
if (ctx.interaction.isModalSubmit()) {
|
||||
const location = ctx.state.data.selected;
|
||||
if (!location) {
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
let supported_route_types = 0;
|
||||
for (const component of ctx.interaction.data.components) {
|
||||
if (StarKitten.isModalLabel(component) && StarKitten.isStringSelectMenu(component.component)) {
|
||||
supported_route_types = supported_route_types | parseInt(component.component.values[0]);
|
||||
}
|
||||
}
|
||||
|
||||
getDB().updateLocation({
|
||||
...location,
|
||||
supported_route_types,
|
||||
});
|
||||
}
|
||||
ctx.state.data.selected = undefined;
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
case Page.removeLocationModal: {
|
||||
if (!ctx.interaction.isSelectMenu()) {
|
||||
console.error('Expected a message component interaction.');
|
||||
return Page.main;
|
||||
}
|
||||
const data = ctx.interaction.data;
|
||||
const locationId = Number.parseInt(data.values[0]);
|
||||
const location = getDB().getLocationById(locationId);
|
||||
if (location) {
|
||||
ctx.state.data.selected = location;
|
||||
return Page.removeLocationModal;
|
||||
}
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
case Page.removeLocationModalSubmit: {
|
||||
if (ctx.interaction.isModalSubmit()) {
|
||||
const location = ctx.state.data.selected;
|
||||
if (!location) {
|
||||
return Page.main;
|
||||
}
|
||||
getDB().removeLocation(location.location_id);
|
||||
}
|
||||
ctx.state.data.selected = undefined;
|
||||
return Page.main;
|
||||
}
|
||||
|
||||
default:
|
||||
return Page.main;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user