mat = 0
async def write_data(user, mat):
users = await read_data()
if str(user) in users:
return False
with open("match.json", "w") as f:
json.dump(users, f)
return Ture
@bot.command(pass_context = True)
async def rand(ctx):
users, mat = await write_data(ctx.author, mat = 0)
get = await match_data(ctx.author, mat)
cat = random.randint(0, 4)
await ctx.send(lucky[int(cat)])
await ctx.send("本日次数已用完")
if mat == 1:
get = await match_data(ctx.author, mat)
cat = random.randint(0, 4)
await ctx.send(lucky[int(cat)])
else:
await ctx.send("本日次数已用完")
# user = author,但为甚麽要加上ctx呢,因为姑且算是文字吧XD
async def write_data(user, mat):
# 省略
return Ture
其实应该要回传2个东西回去,但是这边只有一个,所以需要更改
我们需要知道在json里面的key and value,但这边要先设想一个情况,如果key根本没在json里怎麽办
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
if str(user.id) not in users :
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
mat = 1
users[str(user.id)]["count"] = 0
mat = 1
if users[str(user.id)]["count"] == 0:
users[str(user.id)] = {}
users[str(user.id)]["count"] = 1
mat = 1
return True, mat
async def match_data(user, mat):
users = await read_data()
if mat == 1:
mat = 2
return True, mat
from discord.ext import commands
import discord
from discord.ext.commands import bot
from core.any import Cog_Extension
import json
import random
lucky = ["lucky~~~", "not bad", "Soso", "green hat",
"fuck your self"]
class randomLucky(Cog_Extension):
@commands.command()
async def rand(self,ctx):
users, mat = await write_data(ctx.author, mat = 0)
if mat == 1:
get = await match_data(ctx.author, mat)
cat = random.randint(0, 4)
await ctx.send(lucky[int(cat)])
else:
await ctx.send("本日次数已用完")
async def read_data():
with open("match.json", "r") as file:
users = json.load(file)
return users
async def write_data(user, mat):
users = await read_data()
if str(user) in users:
return False
#find json.key
if str(user.id) not in users :
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
mat = 1
if users[str(user.id)]["count"] == 0:
users[str(user.id)] = {}
users[str(user.id)]["count"] = 1
mat = 1
with open("match.json", "w") as f:
json.dump(users, f)
return True, mat
async def match_data(user, mat):
users = await read_data()
if mat == 1:
mat = 2
return True, mat
def setup(bot):
bot.add_cog(randomLucky(bot))
JavaScript型别 前面有说过JavaScript是动态型别,也就是说在执行时,变数会依照赋予...
Intro 自订资料型态可以是 把不同 data type 合成成一个 复合的型态 或是重新定义一个...
嗨大家~ 像昨天文章里提的,这专案会采用 Notion 来当 CMS (包含资料库),意思是部落格文...
今天既昨天之後,接着继续摸索脚本的编写吧! 打开脚本之後,如果觉得字太小可以按住"Ctrl...
官方说明文件:https://sinotrade.github.io/tutor/accountin...