forked from smallshawn95/Python-Discord-Bot-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.py
120 lines (101 loc) · 3.67 KB
/
task.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import time, discord, datetime
# 導入discord.ext模組中的tasks工具
from discord.ext import tasks, commands
intents = discord.Intents.all()
bot = commands.Bot(command_prefix = "$", intents = intents)
@bot.event
async def on_ready():
await bot.add_cog(TaskBase(bot))
await bot.add_cog(TaskAction(bot))
await bot.add_cog(TaskCount(bot))
await bot.add_cog(TaskTime(bot))
await bot.add_cog(TaskTimes(bot))
print(f"目前登入身份 --> {bot.user}")
class TaskBase(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
# 開始執行函式
self.hi.start()
self.start_time = time.time()
def cog_unload(self):
# 取消執行函式
self.hi.cancel()
# 定義要執行的循環函式
@tasks.loop(seconds = 1)
async def hi(self):
execution_time = int(time.time() - self.start_time)
print(f"{execution_time}sec: Hello, world!")
class TaskAction(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.action.start()
@tasks.loop(seconds = 1)
async def action(self):
print("Action")
self.action.cancel()
# 執行函式前的動作
@action.before_loop
async def action_before(self):
print("Wait")
# 等待機器人上線完成
await self.bot.wait_until_ready()
# 結束執行函式後的動作
@action.after_loop
async def action_after(self):
print("Stop")
class TaskCount(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.count.start()
self.start_time = time.time()
# 循環三次,每五秒輸出執行第幾次
@tasks.loop(seconds = 5, count = 3)
async def count(self):
execution_time = int(time.time() - self.start_time)
print(f"{execution_time}sec: Count {self.count.current_loop}")
# 函式執行三次後要執行的動作
@count.after_loop
async def after_slow_count(self):
execution_time = int(time.time() - self.start_time)
print(f"{execution_time}sec: Count end")
class TaskTime(commands.Cog):
# 臺灣時區 UTC+8
tz = datetime.timezone(datetime.timedelta(hours = 8))
# 設定每日十二點執行一次函式
everyday_time = datetime.time(hour = 0, minute = 0, tzinfo = tz)
def __init__(self, bot: commands.Bot):
self.bot = bot
self.everyday.start()
# 每日十二點發送 "晚安!瑪卡巴卡!" 訊息
@tasks.loop(time = everyday_time)
async def everyday(self):
# 設定發送訊息的頻道ID
channel_id = 1021706869724684376
channel = self.bot.get_channel(channel_id)
embed = discord.Embed(
title = "🛏 晚安!瑪卡巴卡!",
description = f"🕛 現在時間 {datetime.date.today()} 00:00",
color = discord.Color.orange()
)
await channel.send(embed = embed)
class TaskTimes(commands.Cog):
# 設定整點執行一次函式
every_hour_time = [
datetime.time(hour = i, minute = 0, tzinfo = datetime.timezone(datetime.timedelta(hours = 8)))
for i in range(24)
]
def __init__(self, bot: commands.Bot):
self.bot = bot
self.every_hour.start()
# 每小時發送報時訊息
@tasks.loop(time = every_hour_time)
async def every_hour(self):
# 設定發送訊息的頻道ID
channel_id = 1021706869724684376
channel = self.bot.get_channel(channel_id)
embed = discord.Embed(
title = f"⏰ 現在時間【{datetime.time.hour()}】時",
color = discord.Color.random()
)
await channel.send(embed = embed)
bot.run("BOT TOKEN")