python - 冷却命令和某天运行命令
问题描述
所以我为这个机器人写了代码,我希望它做两件事:
- 有 15 分钟的冷却时间
- 有一个只在星期五有效的命令。
我是编码新手,所以不和谐文档没有帮助。
我的进口:
import discord, asyncio, time, discord.guild, random, os, youtube_dl
import os
import asyncio
import math
import random
import youtube_dl
import datetime
import json
from discord.ext import commands, tasks
from discord.ext.commands import Bot, guild_only
from itertools import cycle
15分钟冷却的代码:
@client.command()
async def beg(ctx):
await open_account(ctx.author)
users = await get_bank_data()
user = ctx.author
earn = random.randrange(6900)
await ctx.send(f'Someone gave you {earn} coins!')
users[str(user.id)]["wallet"] += earn
with open('main code\economy.json', 'w') as f:
json.dump(users,f)
仅在星期五有效的命令代码:
@client.command()
async def friday(ctx):
vid = '<URL>'
await ctx.send(vid)
解决方案
15分钟冷却时间
@client.command()
@commands.cooldown(1, 900, commands.BucketType.user) # 15 minutes is 900 seconds
async def command_with_cooldown(ctx):
... # code
@command_with_cooldown.error
async def cooldown_error_handler(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("You can only use this command once every 15 minutes!")
仅限周五
使用datetime.datetime.today().weekday()
import datetime
@client.command()
async def friday_command(ctx):
if datetime.datetime.today().weekday() == 4: # 0 is monday, 6 is sunday
... # code
else:
await ctx.send("You can only use this command on fridays!")
推荐阅读
- php - 如何使用谷歌地图从坐标中获取城市名称
- php - PHP 通过 ref 传递在主体内部没有有效使用
- html2canvas - 如果带宽低,在 Tampermonkey 中使用 html2canvas 时下载图像失败
- python - 尝试从 python 运行 shell 命令时没有输出
- javascript - NodeJS如何创建类似bash的输入
- android - 如何在 VIPER 架构中使用导航组件?
- javascript - 如何修复 ReactJs 中的“Array.prototype.filter()”警告
- python - 将天更改为年的功能
- video - 为什么 Apple 媒体验证器说零处理段
- android - UWP 找不到任何使用 StartScanningForDevicesAsync 的 BLE 设备(Xamarin 的 Plugin.BLE)