小物件实作

闹钟

# cogs/......略
@commands.command()
async def clock(self, ctx, mins, todo):
    embed = discord.Embed(title = "提醒", description = f"您已预约了{mins}分钟後提醒您", color = 0x4599) 
    embed.set_thumbnail(url = "https://www.formula-ai.com/wp-content/uploads/2020/09/python_or_java_meme.jpg")
    await ctx.send(embed = embed)
    time.sleep(int(mins) * 60)
    sms =  discord.Embed(title = "时间到", description = f"您刚刚预约了{mins}分钟要{todo},现在时间已经到罗", color = 0x4599)
    sms.set_thumbnail(url = "https://www.formula-ai.com/wp-content/uploads/2020/09/python_or_java_meme.jpg")
    await ctx.message.author.send(embed = sms)
  • 示意图

订单功能

  • 目标如下
    1. 打开json档看user.id是否存在
    2. 如果不存在就建立
    3. 存在就添加及改写
@commands.command()
async def plus(self, ctx, nums):
    if nums == None:
        return 
    nums = int(nums)
  • check
# 打开json档看user.id是否存在 
async def read_data():    
    with open("bank.json", "r")as file:            
        users = json.load(file)
    return users          
  • read_data
# 如果不存在就建立    
async def open_account(user):
    users = await read_data()
    if str(user.id) in users:
        return False
    else:
        users[str(user.id)] = {}
        users[str(user.id)]["货物量"] = 0
    
    with open("plus.json", "w") as f:
        json.dump(users, f)
        
    return True 
  • reset
# 存在就添加及改写
async def reset(user, change = 0, mode = "amount"):
    users = await read_data()
    users[str(user.id)][mode] += change

    with open("plus.json", "w") as f:
        json.dump(users, f)
    bal = [users[str(user.id)]["amount"]]    
    return bal
  • 这些都是从前面的功能稍微做一些更动,就可以直接使用了
  • 接下来加上添加元素,前面一样有范例程序码喔~~
  • 读取资料
users = await read_data()
to_count = {}
total = []
for user in users:
    name = int(user)
    total_amount = users[user]["amount"]
    to_count[total_amount] = name
    total.append(total_amount)
  • 资料排序
total = sorted(total, reverse = True)
  • 元素添加
embed = discord.Embed(title = "这次的量", description = "将订货量排序後的结果呈现", color = 0x5568)
embed.set_thumbnail(url = "https://www.formula-ai.com/wp-content/uploads/2020/09/python_or_java_meme.jpg")
for i in total:
    userid = to_count[i]
    member = await bot.fetch_user(userid)
    name = member.name
    embed.add_field(name = f"{name}", value = f"您订购的数量为{i}", inline = False)
    
await ctx.send(embed = embed)
  • 示意图

<<:  Day 27 - Rancher Fleet Kustomize 应用程序部署

>>:  html 多选题

D20. 学习基础C、C++语言

D20. 字串题练习 这题是之前老师出的题目但是我一直解不出来,後来看很久,还跑去问同学才解出来的,...

Day 15 知识地图

画出一张属於自己的学习地图,只有知道自己身在何处,才不会迷路。你必须知道这项学习的终点,该拥有什麽样...

关於计算机,你必须知道的事: CPU 快取

本文目标 记忆体层接 介绍快取的组成 常见的快取机制 使用快取需要面对的同步问题 进入正题 CPU ...

暴力攻击(Brute Force Attack)

-图片来源:Toussaint Ilboudo 我碰到了有关卢克小组中的暴力攻击事件的帖子,并恭敬...

Day 9-假物件 (Fake) - 虚设常式 (Stub)-1 (核心技术-1)

虚设常式(Stub)简介 今天的文章进入了新的系列,那因为接下来的概念是非常抽象的,所以会介绍数个核...