nonebot2插件篇的新插件!《我存在这个世界多久了?》你没听错,就是计算你存在这个世界多久了。《程序仅供参考,并不是准确数据》。在群里效果如下:
源代码
from nonebot import on_keyword, on_command
from nonebot.typing import T_State
from nonebot.adapters.cqhttp import Message, Bot, Event
from nonebot.adapters.cqhttp.message import MessageSegment
import requests
from nonebot.permission import *
from nonebot.rule import to_me
import datetime
Age = on_keyword({'/我存在这个世界多久了', '/我存在这个世界上多久了'}, to_me())
@Age.got('age', prompt='请回复你的年龄如:(2005-2-8):')
async def get_kuaidi(bot: Bot, state: T_State):
a = state['age']
if a == ' ':
await Age.send("喂!(#`O′)你是不是在逗ZERO-TWO呀!")
else:
await Age.send("提示:内容仅供参考")
birth_time = str(a)
t=datetime.date.today()
birth_list=[]
for birth in str(birth_time).split('-'):
birth_list.append(birth)
year=int(t.year)-int(birth_list[0])
await Age.send(f"你已经存在这个世界上:{year}年")
spend_month=0
for ye in range(int(year)):
spend_month+=12
await Age.send(f"你在这个世界上存在了:{spend_month-12-8}月了")
if int(t.month) >= int(birth_list[1]):
if int(t.day) >= int(birth_list[2]):
spend_day=0
for y in range(int(year)):
spend_day+=360
await Age.send(f"这已经是你存在这个世界上第{spend_day-1}天了")
if int(t.day) == int(birth_list[2]):
await Age.send(f"今天是你的生日呢!")
await Age.send(f"生日快乐!")
Second=datetime.datetime.now().second
day_ = spend_day-1
await Age.send(f"你已经存在这个世界上:{day_*86400+Second}秒")
主要用datetime库就可以实现了,获取用户输入的出生日期:2005-2-8用符号"-"分隔。因为要循环处理嘛。大家也可以自定义,可以用空格或任意符号。把split改一下就行了。
好了今天分享就到这里,交流群:706128290