-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdc_app.py
executable file
·211 lines (175 loc) · 5.98 KB
/
dc_app.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
#!.venv/bin/python
# app.py
import json
import discord
from datetime import datetime
from discord.ext import commands, tasks
from __init__ import pvei, config
from dc_messages import pveiembeds
with open('timed_tasks.json') as timet:
t_tasks: dict = json.load(timet)
# loads time and command data for scheduled tasks
def em_messages_table() -> dict:
return {
"version": pveiembeds.em_proxmox_version(
pvei.proxmox_version()
),
"b_status": pveiembeds.em_basic_all_status(
pvei.basic_all_status()
),
"n_information": pveiembeds.em_node_information(
pvei.node_information()
),
"machines": pveiembeds.em_all_machines(
pvei.all_machines()
),
"nodes": pveiembeds.em_nodes(
pvei.nodes()
)
}
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='.', intents=intents)
# discord bot defination
dtime = datetime.now()
# dtime declared for timed jobs
@tasks.loop(seconds=1)
async def timed_tasks() -> None:
global dtime
# there is a counter logic here
# dtime holds last time
# t_tasks function checks every second dtimes if its up to date
if datetime.now().year > dtime.year:
# checks last datetime every second
# if datetime is updated it updates dtime variable
dtime = datetime.now()
for command in em_messages_table():
for sce_command in t_tasks['yearly']['commands']:
if command == sce_command:
for channel in t_tasks['yearly']['channels']:
notf_channel = bot.get_channel(channel)
await bot.wait_until_ready()
await notf_channel.send(
embed=em_messages_table()[command]
)
# for embed messages
if datetime.now().month > dtime.month:
dtime = datetime.now()
for command in em_messages_table():
for sce_command in t_tasks['monthly']['commands']:
if command == sce_command:
for channel in t_tasks['monthly']['channels']:
notf_channel = bot.get_channel(channel)
await bot.wait_until_ready()
await notf_channel.send(
embed=em_messages_table()[command]
)
# for embed messages
if datetime.now().hour > dtime.hour:
dtime = datetime.now()
for command in em_messages_table():
for sce_command in t_tasks['minutely']['commands']:
if command == sce_command:
for channel in t_tasks['minutely']['channels']:
notf_channel = bot.get_channel(channel)
await bot.wait_until_ready()
await notf_channel.send(
embed=em_messages_table()[command]
)
# for embed messages
if datetime.now().minute > dtime.minute:
dtime = datetime.now()
for command in em_messages_table():
for sce_command in t_tasks['minutely']['commands']:
if command == sce_command:
for channel in t_tasks['minutely']['channels']:
notf_channel = bot.get_channel(channel)
await bot.wait_until_ready()
await notf_channel.send(
embed=em_messages_table()[command]
)
# for embed messages
# t_tasks function works every 1 second
# and checks hourly, minutely... jobs
# this is look not good but now it works, will be update...
class MainCog(commands.Cog):
def __init__(self, bot) -> None:
self.bot = bot
@commands.command(name='nodes')
async def nodes(self, ctx, *args):
"""
Gives information for all nodes.
"""
await ctx.send(
embed=pveiembeds.em_nodes(
pvei.nodes()
)
)
@commands.command(name='b_status')
async def basic_all_status_report(self, ctx, *args):
"""
b_status command sends basicly embed report
takes embed message from dc_messages, pveiembeds
"""
await ctx.send(
embed=pveiembeds.em_basic_all_status(
pvei.basic_all_status()
)
)
@commands.command(name='n_information')
async def node_information_report(self, ctx, *args):
"""
n_information sends information about node
"""
await ctx.send(
embed=pveiembeds.em_node_information(
pvei.node_information()
)
)
@commands.command(name='machines')
async def all_lxcs(self, ctx, *args):
"""
this command gets various data from lxc and qemu machines
"""
await ctx.send(
embed=pveiembeds.em_all_machines(
pvei.all_machines()
)
)
@commands.command(name='version')
async def version(self, ctx, *args):
"""
returns node's proxmox version
"""
await ctx.send(
embed=pveiembeds.em_proxmox_version(
pvei.proxmox_version()
)
)
@commands.command(name='versions')
async def versions(self, ctx, *args):
"""
returns node's proxmox version
"""
await ctx.send(
embed=pveiembeds.em_proxmox_versions(
pvei.proxmox_versions()
)
)
@commands.command(name='ch_node')
async def ch_node(self, ctx, *args):
"""
changes active node
"""
await ctx.send(
str(
pvei.change_node(
str(args[0])
)
)
)
@bot.event
async def on_ready():
await bot.add_cog(MainCog(bot=bot))
timed_tasks.start()
bot.run(config['DISCORD']['bot_token'])