加上random与time模组,限制次数与时间的管理(1)

显示出现在的时间

  • 使用datatime模组
from datetime import datetime

now = datetime.now()
date_time = now.strftime("%Y-%m-%d, %H:%M:%S")
print(date_time)
  • 为何使用datetime,datetime模组提供处理日期与时间的class,在支持日期时间、数学运算的同时,它的关注点在於更有效的解析其属性用於格式化输出与数据上的操作

将时间丢入Bot内

# cogs/Times.py
# 略
@commands.command(pass_context = True)@bot.command()
async def time(ctx):
    now = datetime.now()
    await ctx.send(now.strftime("%Y-%m-%d %H:%M:%S"))
    # 略

写入random模组

  • 第一,我们将会使用到random模组
    不多说,直接上码
# cogs/randomLucky.py
# 略
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档

{

}
  • 然後为了方便,一律写进函式里
# 写在最下面
async def read_data():
    with open("match.json", "r") as file:
        users = json.load(file)
    return users 

# bot.run...
  • 这个函式可以读取json档(唯读),所以我将他每个users的这个key回传

  • 然後,要怎麽写??


<<:  DAY 3- Enigma

>>:  {DAY 3}如何处理一笔数据?(下)

Day22-Go net/http

前言 今天要来介绍 Go 语言里的网路操作,这边会以介绍 net/http 这个套件为主,并且介绍什...

新手应该要学什麽程序语言?

Photo by Victoriano Izquierdo on Unsplash 文章同步发布於...

I Want To Know React - 初探 Context

在本章中,我们将介绍 React Context。React Context 与 props 相同,...

Day 03:离散数学

前言 今天先来谈谈数学的部分。 微积分 在「线性代数」或「离散数学」都有极少的部分会用到,而现在的我...

Day4 回忆篇 那个关於乖乖的传说-1

最近在某网通群组刚好有聊到,所以乾脆来写一篇文解释一下乖乖传说背後的科学论证吧 先说明缘起,到底是哪...