顯示出現在的時間
使用datatime模組
1 2 3 4 5
| from datetime import datetime
now = datetime.now() date_time = now.strftime("%Y-%m-%d, %H:%M:%S") print(date_time)
|
為何使用datetime,datetime模組提供處理日期與時間的class,在支持日期時間、數學運算的同時,它的關注點在於更有效的解析其屬性用於格式化輸出與數據上的操作
將時間丟入Bot內
1 2 3 4 5 6 7
|
@commands.command(pass_context = True) async def time(ctx): now = datetime.now() await ctx.send(now.strftime("%Y-%m-%d %H:%M:%S"))
|
寫入random模組
第一,我們將會使用到random模組
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
import random
lucky = ["lucky~~~", "not bad", "Soso", "green hat", "fuck your self"]
class randomLucky(Cog_Extension): @commands.command(pass_context = True) async def rand(ctx): cat = random.randint(0, 4) await ctx.channel.send(lucky[int(cat)])
|
關於次數問題
如果我只想要一人一天只能發送一次呢??
我們要怎麼做?
剛才使用的author可以來出來了~~
建立名為match的json檔
然後為了方便,一律寫進函式裡
1 2 3 4 5 6 7
| async def read_data(): with open("match.json", "r") as file: users = json.load(file) return users
|
這個函式可以讀取json檔(唯讀),所以我將他每個users的這個key回傳
2.0部分更改
注意Cog_Extension
要寫commands.cog
具體如下
1 2 3 4 5 6 7
| class Daily(commands.Cog): def __init__(self, bot: commands.Bot): self.bot = bot
@commands.command() async def hello(self, ctx): await ctx.send('嗨')
|
類似這種。