-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
138 lines (104 loc) · 4.24 KB
/
weather.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from discord_webhook import DiscordWebhook, DiscordEmbed
import random
import time
import sqlite3
db = sqlite3.connect("timezone.db", check_same_thread=False)
cursor = db.cursor()
month_num = {
1: "января", 2: "февраля", 3: "марта", 4: "апреля", 5: "мая",
6: "июня", 7: "июля", 8: "августа", 9: 'сентября', 10: 'октября', 11: "ноября", 12: "декабря"
}
weathers = ["Ясная", "Ясная", "Облачная", "Дождливая", "Облачная", "Дождь с грозой", "Ясная"]
def run_change(guild_id, count_bool):
cursor.execute(f"UPDATE guilds SET run = {count_bool} WHERE guild = {guild_id}")
db.commit()
def clear(guild_id):
url = cursor.execute(f"SELECT webhook FROM guilds WHERE guild = {guild_id}").fetchone()[0]
cursor.execute(f"DELETE FROM guilds WHERE guild = {guild_id}")
db.commit()
return url
def arguments_change(guild_id, command: list):
com = False
pole = ""
if command[1] == "timeout":
pole = "timeout"
elif command[1] == "year":
pole = "year"
elif command[1] == "day":
pole = "day"
elif command[1] == "month":
pole = "month"
elif command[1] == "realtime":
pole = "realtime"
if command[1] in ["timeout", "year", "day", "month", "realtime"]:
com = True
if com:
cursor.execute(f"UPDATE guilds SET {pole} = {command[2]} WHERE guild = {guild_id}")
db.commit()
return True
else:
return False
def create(guild_id, hook_url):
cursor.execute(f"""CREATE TABLE IF NOT EXISTS guilds(
webhook TEXT,
run INT,
guild INT,
timeout INT,
realtime INT,
year INT,
day INT,
month INT,
time TEXT
)""")
if cursor.execute(f"SELECT guild FROM guilds WHERE guild = {guild_id}").fetchone() is None:
cursor.execute(f"INSERT INTO guilds VALUES('{hook_url}', 0, {guild_id}, 4, 60, 2022, 1, 1, '6:00')")
db.commit()
hook = DiscordWebhook(url=hook_url, content="Вебхук создан!")
hook.execute()
return True
else:
return False
def change_time(guild_id):
time_was = cursor.execute(f"SELECT time FROM guilds WHERE guild = {guild_id}").fetchone()[0]
year = cursor.execute(f"SELECT year FROM guilds WHERE guild = {guild_id}").fetchone()[0]
day = cursor.execute(f"SELECT day FROM guilds WHERE guild = {guild_id}").fetchone()[0]
month = cursor.execute(f"SELECT month FROM guilds WHERE guild = {guild_id}").fetchone()[0]
_time_ = time_was.split(":")
_hour = int(_time_[0]) + cursor.execute(f"SELECT timeout FROM guilds WHERE guild = {guild_id}").fetchone()[0]
_minute = int(_time_[1])
if _minute >= 60:
_hour += 1
_minute -= 60
if _hour >= 24:
_hour -= 24
day += 1
if day >= 30:
day = 0
month += 1
if month >= 13:
year += 1
month = 1
time_result = str(_hour) + ":" + str(_minute)
cursor.execute(f'UPDATE guilds SET time = "{time_result}", day = {day}, month = {month}, year = {year} WHERE guild = {guild_id}')
return [year, day, month, time_result]
def webhook_send(timezone: list, weather, url):
webhook = DiscordWebhook(url=url)
month = ""
for k, v in month_num.items():
if k == timezone[2]:
month = v
embed = DiscordEmbed(title="Время и погода", color=0x4169E1)
embed.description = f"""
На часах {timezone[3]}
Погода: {weather}
Сегодня {timezone[1]} {month} {timezone[0]} год"""
webhook.add_embed(embed)
webhook.execute()
def start(guild_id):
while cursor.execute(f"SELECT run FROM guilds WHERE guild = {guild_id}").fetchone()[0]:
weather = random.choice(weathers)
timemout = change_time(guild_id)
db.commit()
url = cursor.execute(f"SELECT webhook FROM guilds WHERE guild = {guild_id}").fetchone()[0]
webhook_send(timemout, weather, url)
time.sleep(cursor.execute(f"SELECT realtime FROM guilds WHERE guild = {guild_id}").fetchone()[0]*60)