瀏覽代碼

feat: logout command

kotoyuuko 2 周之前
父節點
當前提交
4718c8a850
共有 4 個文件被更改,包括 41 次插入1 次删除
  1. 34 0
      src/command/handlers/logout.ts
  2. 1 1
      src/command/handlers/start.ts
  3. 5 0
      src/command/index.ts
  4. 1 0
      src/index.ts

+ 34 - 0
src/command/handlers/logout.ts

@@ -0,0 +1,34 @@
+import { Context } from "grammy";
+
+export interface Env {
+  BOT_INFO: string;
+  BOT_TOKEN: string;
+  COSMOE_CREDENTIALS: KVNamespace;
+  COSMOE_STORAGE: KVNamespace;
+}
+
+export async function handleLogoutCommand(ctx: Context, env: Env): Promise<void> {
+  try {
+    // Get user ID from context
+    const userId = ctx.from?.id;
+    if (!userId) {
+      await ctx.reply("❌ Unable to identify your account. Please try again.");
+      return;
+    }
+
+    // Check if credentials exist for this user
+    const credentials = await env.COSMOE_CREDENTIALS.get(userId.toString());
+    if (!credentials) {
+      await ctx.reply("⚠️ You are not logged in. No credentials to clear.");
+      return;
+    }
+
+    // Delete the stored credentials
+    await env.COSMOE_CREDENTIALS.delete(userId.toString());
+
+    await ctx.reply("✅ Successfully logged out. Your account information has been cleared from our system.");
+  } catch (error) {
+    console.error("Error handling logout request:", error);
+    await ctx.reply("❌ An error occurred while logging out. Please try again.");
+  }
+}

+ 1 - 1
src/command/handlers/start.ts

@@ -2,5 +2,5 @@ import { Context } from "grammy";
 import { CosmoeClient } from "../../client/cosmoe";
 
 export async function handleStartCommand(ctx: Context): Promise<void> {
-  await ctx.reply("Welcome to CosMoe Bot! Available commands:\n/events - See latest events\n/login - Log in to your account\n/history - View your booking history\n/cancel_{id} - Cancel a booking (replace {id} with booking ID)");
+  await ctx.reply("Welcome to CosMoe Bot! Available commands:\n/events - See latest events\n/login - Log in to your account\n/logout - Log out and clear your account information\n/history - View your booking history");
 }

+ 5 - 0
src/command/index.ts

@@ -6,6 +6,7 @@ import { handleEventDetails } from "./handlers/eventDetails";
 import { handleBookEvent } from "./handlers/bookEvent";
 import { handleHistoryCommand } from "./handlers/history";
 import { handleCancelCommand, handleCancelConfirmation } from "./handlers/cancel";
+import { handleLogoutCommand } from "./handlers/logout";
 
 export interface Env {
   BOT_INFO: string;
@@ -53,4 +54,8 @@ export function setupCommands(bot: Bot, env: Env) {
       await ctx.answerCallbackQuery();
     }
   });
+
+  bot.command("logout", async (ctx: Context) => {
+    await handleLogoutCommand(ctx, env);
+  });
 }

+ 1 - 0
src/index.ts

@@ -23,6 +23,7 @@ export default {
     bot.api.setMyCommands([
       { command: "start", description: "Start the bot and get welcome message" },
       { command: "login", description: "Log in to your CosMoe account" },
+      { command: "logout", description: "Log out and clear your account information" },
       { command: "events", description: "List upcoming events" },
       { command: "history", description: "Show your booking history" },
     ]).catch(error => {