xia的小窩

一起來coding和碼字吧

0%

python-Discord-bot,從0開始到做出一個機器人系列-16

製造一個停止條件

我會在這邊設置新變數

1
mat = 0

但是我也必須要有一個可以寫入的函式

1
2
3
4
5
6
7
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

把剛才的變數加入rand函式裡

1
2
3
4
5
6
7
@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("本日次數已用完")

關於mat,就是傳給write_data()這個函式的一個變數。
所以我們可以利用計數的方式,去讓它做一個中止點。在上面先假設了mat = 0,之後就依照所求去做斷點。

1
2
3
4
5
6
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("本日次數已用完")

關於mat回傳值

回到write_data()

1
2
3
4
# user = author,但為甚麼要加上ctx呢,因為姑且算是文字吧XD
async def write_data(user, mat):
# 省略
return Ture

其實應該要回傳2個東西回去,但是這邊只有一個,所以需要更改

我們需要知道在json裡面的key and value,但這邊要先設想一個情況,如果key根本沒在json裡怎麼辦

1
2
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0

再來,我們處理json key的部分

1
2
3
4
if str(user.id) not in users : 
users[str(user.id)] = {}
users[str(user.id)]["count"] = 0
mat = 1

如果key不在裡面,就創建一個附與給他,然後直接給它一個mat = 1

出現了問題

第一,mat的值
第二,我們忘記的match_data函式還沒下手

1
2
users[str(user.id)]["count"] = 0
mat = 1

如果說,我們是用 mat 來計數的話,那我們還要處理一下這個部分

1
2
3
4
if users[str(user.id)]["count"] == 0:
users[str(user.id)] = {}
users[str(user.id)]["count"] = 1
mat = 1

我們把value改成了1,雖然說mat值不變,但你要記得回傳給它

1
return True, mat

然後我們處理一下 match_data()

1
2
async def match_data(user, mat):
users = await read_data()

這邊我主要是改mat的值,因為不管怎麼說,使用了!rand指令後,不管有沒有在json檔裡面,創建好{ }後,value都會直接變成1

1
2
3
if mat == 1:
mat = 2
return True, mat

這部分的程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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))

2.0部分更改

小改一部份

1
2
3
4
5
6
7
8
9
class randomLucky(commands.cog):
def __init__(self, bot):
self.bot = bot

# @commands.command()
# async def rand(self,ctx):

async def setup(bot: commands.Bot):
await bot.add_cog(randomLucky(bot))

json的部分應該沒毛病。