糾團的功能我把它切成兩個部分
使用者藉由輸入指令把糾團資訊匯入到資料庫裡
目前的能匯入的資訊有任務名稱、備註、時間
之後可能會再添加個人數限制
一開始的指令格式是設定成用空格來區分各項資訊
/task  任務名稱  備註  10/2-20:00"
但我發現蠻多人會不小心在備註或是名稱多打個空格導致程式判斷錯誤
所以後來分隔符我改成用"|"解決
將使用者的訊息分割完後先用len()來判斷list裡面的元素有幾個
會這樣做是因為使用者很常不會照你的指令格式去打,所以先用元素數量判斷格式
判斷完數量正確之後接著判斷每項元素裡的字串是否符合格式
根據測試經驗最常出現錯誤的是時間這個元素
所以我這邊直接try datetime.strptime(time_name, '%m/%d-%H:%M')
出現錯誤直接回傳報錯訊息給使用者
資料的儲存我是用json格式來存,key值我是用數字來辨識,value是個字典格式儲存各項資訊
這邊附上關於使用者輸入訊息的程式碼,這是片段部分所以先不要拿它來執行
@commands.command()
#訊息範本 : task 藏寶圖G12 主線5.0有80等腳色 10/2-20:00
async def task(self,ctx):
    self.count = 0
    error_message = f"\n參考指令範本: /task | 任務名稱 | 備註 | 10/2-20:00"
    with open("setting.json",'r',encoding="utf8") as jfile:
        jdata = json.load(jfile)
    while str(self.id) in jdata:
        self.id = int(self.id)
        self.id += 1
        self.id = str(self.id)
    #拆解訊息
    # print(task)
    # print(condition)
    self.id = str(self.id)
    message_list = ctx.message.content.split("|")
    if len(message_list) == 4:
        #前處理訊息,把開頭結尾空格拿掉
        prifix_name = message_list[0].lstrip().rstrip()
        task_name = message_list[1].lstrip().rstrip()
        condition_name = message_list[2].lstrip().rstrip()
        time_name = message_list[3].lstrip().rstrip()
        # 確認每項資訊符合規則
        if prifix_name != "/task":
            await ctx.message.reply(f"前綴輸入錯誤"+error_message)
        try:
            datetime.strptime(time_name, '%m/%d-%H:%M')
        except BaseException:
            await ctx.message.reply("時間格式輸入錯誤"+error_message)
        now_time = datetime.utcnow().replace(tzinfo=timezone.utc)
        now_time = now_time.astimezone(timezone(timedelta(hours=8)))
        now_time = now_time.strftime('%m/%d-%H:%M')
        if datetime.strptime(now_time, '%m/%d-%H:%M') > datetime.strptime(time_name, '%m/%d-%H:%M'):
            await ctx.message.reply("你填的時間比現在時間早~")
        task_info = {self.id:{"task":task_name,"condition":condition_name,"time":time_name,"url":ctx.message.jump_url}}
        jdata.update(task_info)
        with open("setting.json",'w',encoding="utf8") as jfile:
            jdata = json.dump(jdata,jfile,indent=4)
    elif len(message_list) == 3:
        #前處理訊息,把開頭結尾空格拿掉
        prifix_name = message_list[0].lstrip().rstrip()
        task_name = message_list[1].lstrip().rstrip()
        condition_name = message_list[2].lstrip().rstrip()
        time_name = message_list[3].lstrip().rstrip()
        # 確認每項資訊符合規則
        if prifix_name != "/task":
            await ctx.message.reply(f"前綴輸入錯誤"+error_message)
        try:
            datetime.strptime(time_name, '%m/%d-%H:%M')
        except BaseException:
            await ctx.message.reply("時間格式輸入錯誤"+error_message)
        now_time = datetime.utcnow().replace(tzinfo=timezone.utc)
        now_time = now_time.astimezone(timezone(timedelta(hours=8)))
        now_time = now_time.strftime('%m/%d-%H:%M')
        if datetime.strptime(now_time, '%m/%d-%H:%M') > datetime.strptime(time_name, '%m/%d-%H:%M'):
            await ctx.message.reply("你填的時間比現在時間早~")
        task_info = {self.id:{"task":task_name,"time":time_name,"url":ctx.message.jump_url}}
        jdata.update(task_info)
        with open("setting.json",'w',encoding="utf8") as jfile:
            jdata = json.dump(jdata,jfile,indent=4)
    else:
        await ctx.message.reply("格式輸入錯誤~"+error_message)