Files
LangBot/plugins/misc/ScheNotify/components/commands/dsche.py
T

88 lines
3.4 KiB
Python

# Auto generated by LangBot Plugin SDK.
# Please refer to https://langbot.app/docs/en/plugin/dev/tutor.html for more details.
from __future__ import annotations
from typing import AsyncGenerator
from langbot_plugin.api.definition.components.command.command import Command
from langbot_plugin.api.entities.builtin.command.context import ExecuteContext, CommandReturn
class DscheCommand(Command):
"""
Command to delete a scheduled notification by index.
Usage: !dsche <index>
Example: !dsche 1
"""
def __init__(self):
super().__init__()
@self.subcommand(
name="i",
help="Delete a scheduled notification by index",
usage="dsche i <index>",
aliases=["d"],
)
async def delete_notification(self, context: ExecuteContext) -> AsyncGenerator[CommandReturn, None]:
"""Delete a scheduled notification by index"""
# Get language setting
config = self.plugin.get_config()
language = config.get("language", "zh_Hans")
# Check if index parameter is provided
if len(context.crt_params) == 0:
if language == "en_US":
reply = '[Notify] Command format: !dsche <index>, e.g. !dsche 1. Please use !sche to view scheduled notifications first.'
else:
reply = '[Notify] 命令格式: !dsche <index>,例如 !dsche 1。请先使用 !sche 查看计划中的提醒'
yield CommandReturn(text=reply)
return
try:
index = int(context.crt_params[0]) - 1
except ValueError:
if language == "en_US":
reply = '[Notify] Invalid index. Please provide a number.'
else:
reply = '[Notify] 无效的索引,请提供数字'
yield CommandReturn(text=reply)
return
# Get the full session identity from the trusted session: a target id
# alone is shared by other bots and by the person/group namespaces.
session = context.session
session_key = self.plugin.session_key(
session.bot_uuid, session.launcher_type.value, str(session.launcher_id)
)
# Get scheduled events from plugin
scheduled_events = await self.plugin.get_scheduled_events(session_key)
if index < 0 or index >= len(scheduled_events):
if language == "en_US":
reply = '[Notify] Index out of range'
else:
reply = '[Notify] 索引超出范围'
yield CommandReturn(text=reply)
return
event = scheduled_events[index]
# Delete the event; the plugin re-checks that it belongs to this session.
if not await self.plugin.delete_scheduled_event(event, session_key):
if language == "en_US":
reply = '[Notify] Index out of range'
else:
reply = '[Notify] 索引超出范围'
yield CommandReturn(text=reply)
return
if language == "en_US":
reply = f'[Notify] Deleted notification: {event["time"]} {event["message"]}'
else:
reply = f'[Notify] 已删除提醒:{event["time"]} {event["message"]}'
yield CommandReturn(text=reply)