refactor: extract Discord.js code from shared services into bot layer
Move terminal.service.ts and prune.service.ts entirely to bot/modules/ since they are Discord-specific. Split lootdrop.service.ts: pure logic (activity tracking, DB ops, claim) stays in shared/, Discord operations (message sending, channel interactions) move to bot/modules/economy/ lootdrop.handler.ts. Move effect registry/handlers/types from bot/ to shared/modules/inventory/ since they contain no Discord.js imports and are needed by inventory.service.ts in shared. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
60
bot/modules/economy/lootdrop.handler.ts
Normal file
60
bot/modules/economy/lootdrop.handler.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { Message, TextChannel } from "discord.js";
|
||||
import { lootdropService } from "@shared/modules/economy/lootdrop.service";
|
||||
import { getLootdropMessage } from "./lootdrop.view";
|
||||
import { terminalService } from "@modules/system/terminal.service";
|
||||
|
||||
/**
|
||||
* Process a Discord message for lootdrop activity tracking.
|
||||
* Called from messageCreate event handler.
|
||||
*/
|
||||
export async function processLootdropMessage(message: Message): Promise<void> {
|
||||
if (message.author.bot || !message.guild) return;
|
||||
|
||||
const { shouldSpawn } = lootdropService.trackActivity(message.channel.id);
|
||||
|
||||
if (shouldSpawn) {
|
||||
await spawnLootdrop(message.channel as TextChannel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn a lootdrop in a Discord channel.
|
||||
* Used by both bot events and API routes.
|
||||
*/
|
||||
export async function spawnLootdrop(
|
||||
channel: TextChannel,
|
||||
overrideReward?: number,
|
||||
overrideCurrency?: string
|
||||
): Promise<void> {
|
||||
const { reward, currency } = lootdropService.calculateReward(overrideReward, overrideCurrency);
|
||||
const { content, files, components } = await getLootdropMessage(reward, currency);
|
||||
|
||||
try {
|
||||
const sentMessage = await channel.send({ content, files, components });
|
||||
await lootdropService.persistLootdrop(sentMessage.id, channel.id, reward, currency);
|
||||
terminalService.update(channel.guildId);
|
||||
} catch (error) {
|
||||
console.error("Failed to spawn lootdrop:", error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a lootdrop from DB and Discord.
|
||||
*/
|
||||
export async function deleteLootdrop(messageId: string): Promise<boolean> {
|
||||
const result = await lootdropService.removeLootdrop(messageId);
|
||||
if (!result) return false;
|
||||
|
||||
try {
|
||||
const { AuroraClient } = await import("@/lib/BotClient");
|
||||
const channel = await AuroraClient.channels.fetch(result.channelId) as TextChannel;
|
||||
if (channel) {
|
||||
const message = await channel.messages.fetch(messageId);
|
||||
if (message) await message.delete();
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn("Could not delete lootdrop message from Discord:", e);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user