xia的小窩

一起來coding和碼字吧

0%

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

乞討功能

寫個乞討的功能……

1
2
3
4
# cogs/money.py
@commands.command()
async def beg(self, ctx):
pass

一樣,老步驟,不過最後要微改

1
2
3
4
await open_account(ctx.author)

users = await get_bank_data()
user = ctx.author

乞討來的錢一律隨機

1
earnings = random.randrange(101)

加上

1
await ctx.send(f"某人給了你 {earnings} 塊錢!!")

之後在json檔裡動手腳

1
users[str(user.id)]["wallet"] += earnings

這樣就可以寫入json檔裡了

1
2
with open("bank.json", "w") as f:
json.dump(users, f)

在進入下個部分前……

我們需要先撰寫將data複寫過的函式

1
2
async def update_bank(user, change = 0, mode = "wallet"):
pass

查看錢包

1
2
3
4
5
users = await get_bank_data()
users[str(user.id)][mode] += change

with open("bank.json", "w") as f:
json.dump(users, f)

將money的值還回去(錢包裡的錢)

1
2
money = [users[str(user.id)]["wallet"], users[str(user.id)]["bank"]]    
return money

大賭怡情,小賭傷身,強賭……

引用asyncio

1
2
3
4
5
import asyncio

@commands.command()
async def slots(self, ctx, amount = None, mang = None):
pass

一樣,先處理好倍率跟賭注

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
await open_account(ctx.author)
if mang == None:
await ctx.send("請輸入倍率")
return
bal = await update_bank(ctx.author)
amount = int(amount)
mang = int(mang)
if bal[0] <= 0:
await ctx.send("沒錢的,滾拉")
return
if amount > bal[0]:
await ctx.send("你沒這麼多錢拉幹")
return
if amount < 0:
await ctx.send("正整數...")
return
# 做出限制
if amount > 200 or mang > 10 :
await ctx.send("不接受太高金額及倍率,最高為199$$")
return

最後,錢進到皮包裡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
final = []
for i in range(3):
a = random.choice(["X", "O", "Q"])
final.append(a)
ans = mang * amount
pos = mang * amount * -1
# win
if final[0] == final[1] == final[2]:
await update_bank(ctx.author, ans)
await ctx.send("想贏嗎??")
asyncio.sleep(3)
await ctx.send(str(final))
await ctx.send(f"你贏了 {ans} 塊錢")
# lose
else:
await update_bank(ctx.author, pos)
await ctx.send("想贏嗎??")
asyncio.sleep(3)
await ctx.send(str(final))
await ctx.send(f"你輸了 {pos} 塊錢")

小問題

第一,關於次數,其實當初是有想過限制次數的,但後來想想好像沒必要
第二,關於倍率,這部分可以自己做更改,畢竟是random
第三,搶銀行…….好像挺不道德的

畢竟所謂的亂數並不是真的隨機,有興趣的話可以點這裡看看別人的說法

2.0部分更改

這篇問題不大,就是看了眼花,程式碼是對的,其他小部分就自己修吧,我手裡也沒程式碼了…github上的也沒了,至於怎麼沒了我也忘了,肯定是我刪了github帳號。