-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
219 lines (175 loc) · 7.74 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import time
import threading
import discord
from pypresence import Presence
import pystray
from PIL import Image, ImageDraw, ImageOps
from discord.ext import commands
import asyncio
import sys
# Конфигурация
client_id = 'CLIENT ID'
bot_token = 'TOKEN BOT'
guild_id = SERVER_ID
user_id = USER_ID
RPC = Presence(client_id)
RPC.connect()
start_time = int(time.time())
last_active_time = time.time()
activities = {
"wake_up": {"state": "С этого моента", "details": "Я только проснулся", "large_image": "wake_up"},
"eat": {"state": "С этого моента", "details": "Кушаю", "large_image": "eat"},
"toilet": {"state": "С этого моента", "details": "В туалете", "large_image": "toilet"},
"game": {"state": "С этого моента", "details": "Играю", "large_image": "game"},
"busy": {"state": "С этого моента", "details": "Занят важными делами", "large_image": "busy"},
"important_person": {"state": "С этого моента", "details": "Общаюсь с важным человеком",
"large_image": "important_person"},
"away": {"state": "С этого моента", "details": "Отошел на некоторое время", "large_image": "away"},
"nothing": {"state": "С этого моента", "details": "Ничего не делаю", "large_image": "nothing"},
}
current_activity = "wake_up"
invite_url = None
tray_icon = None # Инициализация глобальной переменной для иконки трея
def update_activity():
global current_activity, start_time, invite_url, tray_icon
activity = activities[current_activity]
elapsed_time = int(time.time()) - start_time if current_activity == "away" else None
buttons = [{"label": "Присоединиться", "url": invite_url}] if invite_url and current_activity != "busy" else []
activity_details = f"Что я сейчас делаю: **{activity['details']}**"
if elapsed_time:
activity_details += f"\nПрошло {elapsed_time // 60} минут"
try:
if buttons:
RPC.update(
state=activity["state"],
details=activity_details,
large_image=activity["large_image"],
start=start_time,
buttons=buttons
)
else:
RPC.update(
state=activity["state"],
details=activity_details,
large_image=activity["large_image"],
start=start_time
)
except Exception as e:
print(f"Ошибка обновления RPC: {e}")
if tray_icon:
tray_icon.title = activity["state"] # Обновление заголовка иконки трея
def change_activity(activity, away_time=None):
global current_activity, start_time, last_active_time
current_activity = activity
start_time = int(time.time())
last_active_time = time.time()
update_activity()
if away_time:
threading.Timer(away_time * 60, change_activity, args=("busy",)).start()
def monitor_inactivity():
global last_active_time
while True:
if time.time() - last_active_time > 300:
change_activity("away")
time.sleep(60)
intents = discord.Intents.default()
intents.presences = True
intents.members = True
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f'Logged in as {bot.user.name}')
await update_invite()
@bot.event
async def on_voice_state_update(member, before, after):
if member.id == user_id:
await update_invite()
async def update_invite():
global invite_url
guild = bot.get_guild(guild_id)
member = guild.get_member(user_id)
voice_state = member.voice
if voice_state and voice_state.channel:
channel = voice_state.channel
invite = await channel.create_invite(max_age=300)
invite_url = invite.url
else:
invite_url = None
await update_activity_async()
async def update_activity_async():
await bot.loop.run_in_executor(None, update_activity)
async def bot_start():
await bot.start(bot_token)
async def shutdown():
await bot.close()
RPC.close()
sys.exit()
def run_bot():
asyncio.run(bot_start())
def tray_monitor():
global tray_icon
def create_image():
try:
image = Image.open("Frame-2.ico") # замените на "icon.png" если у вас PNG
return image
except Exception as e:
print(f"Ошибка загрузки иконки: {e}")
width = 64
height = 64
color1 = "black"
color2 = "white"
image = Image.new("RGB", (width, height), color1)
dc = ImageDraw.Draw(image)
dc.rectangle((width // 2, 0, width, height // 2), fill=color2)
dc.rectangle((0, height // 2, width // 2, height), fill=color2)
return image
def on_clicked(icon, item):
activity_map = {
"Ем": "eat",
"В туалете": "toilet",
"Играю": "game",
"Занят": "busy",
"Общаюсь с важным человеком": "important_person",
"Ничего не делаю": "nothing"
}
if item.text in activity_map:
change_activity(activity_map[item.text])
elif item.text.startswith("Отошел"):
away_time = int(item.text.split(" ")[1]) if len(item.text.split(" ")) > 1 else None
change_activity("away", away_time)
elif item.text == "Выход":
icon.stop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(shutdown())
tray_icon = pystray.Icon("SiresStatus", create_image(), activities[current_activity]["state"],
menu=pystray.Menu(
pystray.MenuItem("Ем", on_clicked),
pystray.MenuItem("В туалете", on_clicked),
pystray.MenuItem("Играю", on_clicked),
pystray.MenuItem("Занят", on_clicked),
pystray.MenuItem("Общаюсь с важным человеком", on_clicked),
pystray.MenuItem("Ничего не делаю", on_clicked),
pystray.MenuItem("Отошел", pystray.Menu(
pystray.MenuItem("5 минут", lambda: change_activity("away", 5)),
pystray.MenuItem("10 минут", lambda: change_activity("away", 10)),
pystray.MenuItem("30 минут", lambda: change_activity("away", 30)),
)),
pystray.MenuItem("Выход", on_clicked)
))
tray_icon.run()
# Запуск потоков
bot_thread = threading.Thread(target=run_bot, daemon=True)
bot_thread.start()
tray_thread = threading.Thread(target=tray_monitor, daemon=True)
tray_thread.start()
inactivity_thread = threading.Thread(target=monitor_inactivity, daemon=True)
inactivity_thread.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
print("Выход из программы")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(shutdown())