admin管理员组文章数量:1646326
文章目录
- 前言
- 一、使用命令及方法
- 二、代码部分
- 1.完整代码
- 1.init.py
- 2.login.py(用于记录签到时间和判断是否签到)
- 3.read.py(用于数据读取)
- 4.write.py (用于数据的写入)
- 2.配套插件
- 总结
前言
本插件基于nonebot框架进行创作
作者萌新源
包结构
qiandao
|__init.py
|login.py
|read.py
|write.py
一、使用命令及方法
##1.使用命令
签到
##2.使用方法
放入plugins文件夹内即可
二、代码部分
1.完整代码
代码如下:
1.init.py
from nonebot import on_regex
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupMessageEvent,Bot,Message
from .read import notice2, read_data
from .login import notice,search
from .write import write_file
import time
import json
import random
qd = on_regex(pattern = r'^签到$')
@qd.handle()
async def lj(bot: Bot, event: GroupMessageEvent, state: T_State):
coints = random.randint(1,100)
qq_id = str(event.user_id)
local_time = time.localtime(event.time)
login_time = time.strftime('%d',local_time)
file_name = 'coints.json'
with open(file_name) as f:
data_user = json.load(f)
if qq_id in data_user:#判断用户是不是第一次签到
await read_data(coints,qq_id,login_time) #首次使用签到功能需要先执行一次签到
lovelive_send = await notice()
else:
await read_data(coints,qq_id,login_time) #首次使用签到功能需要先执行一次签到
lovelive_send = await notice2()
# at_ = f"[CQ:at,qq={event.get_user_id()}]"
await qd.send(Message(str(lovelive_send)))
2.login.py(用于记录签到时间和判断是否签到)
import json
from .write import write_file
send = 0
async def search(coint,qq,time):
file_name = 'coints.json'
with open(file_name) as f:
data_user = json.load(f)
last_time = data_user[f'{qq}login']
global send
if time == last_time:
send = '你已经签到过了,明天再来吧'
else:
send = f'签到成功,获得{coint}个金币'
await write_file(coint,qq,False,data_user,time)
async def notice():
news = send
return news
3.read.py(用于数据读取)
import json
import time
from .write import write_file#调用写入模块
from .login import search
send = 1
async def read_data(coint,qq,time):
file_name = 'coints.json'
global send
try:
with open(file_name) as f:
data_user = json.load(f)
if qq in data_user:#判断用户是不是第一次签到
await search(coint,qq,time)
else:
await write_file(coint,qq,True,data_user,time)
send = f'签到成功,获得{coint}个金币'
except FileNotFoundError:
data = {'null':0}
with open(file_name,'w') as f:
json.dump(data,f)
with open(file_name) as f:
data_2 = json.load(f)
await write_file(coint,qq,True,data_2,time)
async def notice2():
news = send
return news
4.write.py (用于数据的写入)
import json
async def write_file(coint,qq,first,file_read,time):
if first == True:
file_name = 'coints.json'
data = dict(file_read)
data2 = {str(qq):coint,f'{qq}login':time}
data.update(data2)
print(data)
with open(file_name,'w') as f:
json.dump(data,f)
else:
file_name = 'coints.json'
data = dict(file_read)
qq_str = str(qq)
print(data)
coints = data[f'{qq_str}']
data[f'{qq_str}'] = int(coints) + int(coint)
data[f'{qq_str}login'] = str(time)
print(data)
with open(file_name,'w') as f:
json.dump(data,f)
2.配套插件
配套插件search.py(用于查询用户金币余额)
使用指令
我的钱包
from nonebot import on_regex
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupMessageEvent,Bot,Message
import json
qd = on_regex(pattern = r'^我的钱包$')
@qd.handle()
async def lj(bot: Bot, event: GroupMessageEvent, state: T_State):
qq_id = str(event.user_id)
await search_qq(qq_id)
# at_ = f"[CQ:at,qq={event.get_user_id()}]"
lovelive_send = await notice()
await qd.send(Message(str(lovelive_send)))
send = 0
async def search_qq(qq):
global send
file_name = 'coints.json'
with open(file_name) as f:
data_user = json.load(f)
money = data_user[f'{qq}']
send = f'您的金币共有{money}个'
async def notice():
news = send
return news
总结
本插件可以和任何其他插件配套使用
版权声明:本文标题:[nonebot2]QQ机器人签到插件 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1729444546a1201310.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论