-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
245 lines (221 loc) · 11.1 KB
/
bot.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import discord
# import Stundenplan_parser #Importiert das Modul
import Stundenplan
# from datetime import date
import string
import json
import os
# from datetime import datetime
from discord.ext import commands
import asyncio
from discord_components import DiscordComponents, Button
import autodelete
# s = Stundenplan_parser.stundenplan.Stundenplan() # Erstellt eine Stundenplan_Instanz
token = os.environ.get('BOT_TOKEN')
client = commands.Bot(command_prefix='!')
prefix = "!"
channels = []
#background task to get time and delete messages automatically
async def autodelete_background_task():
while True:
await autodelete.deleteMessages(client)
await asyncio.sleep(30*60) #time in seconds
def create_embed(klasse, s):
# create embed
for x in s.courses:
if x.color == "red":
embedColor = 0xffff00
break
else:
embedColor = 0x00ff00
embedPlanHeute = discord.Embed(title=s.title, description="---", color=embedColor)
embedPlanHeute.set_author(name=f"Klasse {klasse}")
isEmpty = True
for vertretung in s.vertretungen:
if vertretung.heading == "Prüfungen:":
embedPlanHeute.add_field(name=vertretung.heading, value=f"""**```fix\n{vertretung.content}```**---""", inline=False)
elif not vertretung.heading == "Allgemein":
isEmpty = False
embedPlanHeute.add_field(name=vertretung.heading, value=f"""**```fix\n{vertretung.content}```**""", inline=False)
else:
embedPlanHeute.add_field(name=vertretung.heading, value=f"{vertretung.content}\n---", inline=False)
if isEmpty:
embedPlanHeute.add_field(name=":x: Für diese Klasse sind keine Vertretungen eingestellt", value="Versuche es später noch einmal", inline=False)
embedPlanHeute.set_footer(text="Stand: " + s.geandert)
return embedPlanHeute
def create_embed_regular(klasse, s):
# create embed for all courses
for x in s.courses:
if x.color == "red":
embedColor = 0xffff00
break
else:
embedColor = 0x00ff00
embedPlanHeute = discord.Embed(title=s.title, description="---", color=embedColor)
embedPlanHeute.set_author(name=f"Klasse {klasse}")
isEmpty = True
for count, stunde in enumerate(s.courses):
if stunde.heading == "Prüfungen:":
embedPlanHeute.add_field(name=stunde.heading, value=f"""**```fix\n{stunde.content}```**""", inline=False)
elif not stunde.heading == "Allgemein":
isEmpty = False
if s.courses[count-1].heading == stunde.heading:
continue
if stunde.color == "red":
try:
if s.courses[count+1].heading == stunde.heading:
if s.courses[count+1].color == "black":
embedPlanHeute.add_field(name=stunde.heading, value=f"""**```fix\n{stunde.content}```**{s.courses[count+1].content}""", inline=False)
else:
embedPlanHeute.add_field(name=stunde.heading, value=f"""**```fix\n{stunde.content}\n{s.courses[count+1].content}```**""", inline=False)
else:
embedPlanHeute.add_field(name=stunde.heading, value=f"""**```fix\n{stunde.content}```**""", inline=False)
except:
embedPlanHeute.add_field(name=stunde.heading, value=f"""**```fix\n{stunde.content}```**""", inline=False)
else:
embedPlanHeute.add_field(name=stunde.heading, value=stunde.content, inline=False)
else:
embedPlanHeute.add_field(name=stunde.heading, value=f"{stunde.content}\n---", inline=False)
if isEmpty:
embedPlanHeute.add_field(name=":x: Für diese Klasse sind keine Vertretungen eingestellt", value="Versuche es später noch einmal", inline=False)
embedPlanHeute.set_footer(text="Stand: " + s.geandert)
return embedPlanHeute
def helpEmbed():
embedHelp = discord.Embed(title="Vertretungsplan Bot Commands:", description="---", color=0xfd0f02)
embedHelp.set_author(name="Bot help")
embedHelp.set_thumbnail(
url="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT2MH0LGbSQPti92wXwtdVygovKCH2UNcNJug&usqp=CAU")
embedHelp.add_field(name=f"{prefix}[Klasse]", value=f"Vertretungsplan heute\n Bsp: {prefix}9a", inline=False)
embedHelp.add_field(name=f"{prefix}[Klasse] morgen", value=f"Vertretungsplan für morgen \n Bsp: {prefix}9a morgen",
inline=True)
embedHelp.add_field(name=f"{prefix}invite", value="Füge den Bot auf deinen Server hinzu",
inline=False)
embedHelp.add_field(name="Administration", value="---", inline=False)
embedHelp.add_field(name=f"!start", value=f"Aktiviert den Bot in diesem channel (Admin Berechtigung erforderlich!)",
inline=False)
embedHelp.add_field(name=f"!stop",
value=f"Deaktiviert den Bot in diesem channel (Admin Berechtigung erforderlich!)", inline=False)
embedHelp.set_footer(text='Made by adamane and Chris00004')
return embedHelp
@client.event
async def on_ready():
DiscordComponents(client)
print('Logged in as {}'.format(client.user.name))
with open("./data_files/data_file.json", "r") as read_file:
x = json.load(read_file)
global channels
channels = x["channels"]
# Stundenplan_parser.stundenplan.Stundenplan.remove_plan() #Cleanup old Leftovers
# s = Stundenplan_parser.stundenplan.Stundenplan() # Creates a Stundenplan Instance
client.loop.create_task(autodelete_background_task()) #starts the background task
while True:
await client.change_presence(status=discord.Status.idle, activity=discord.Activity(type=discord.ActivityType.playing, name="Der Bot funktioniert vorübergehend nicht, bis der umstieg auf Untis geklappt hat"))
#! uncomment when bot is updated
# await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="!help")) #create custom bot state
# await asyncio.sleep(15)
# await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name="!invite me onto your server"))
# await asyncio.sleep(15)
@client.event
async def on_message(message):
if message.author == client.user:
return
if not message.content.startswith(prefix):
return
if message.content == prefix + 'help': # Helper Message Handler
# create help embed
embedHelp = helpEmbed()
await message.channel.send(embed=embedHelp)
return
if message.content == prefix + 'invite':
await message.channel.send(
"Füge mich auf deinem Server hinzu:",
components = [
Button(label = "Auf Server einladen", style=5, url="https://discord.com/api/oauth2/authorize?client_id=797529729632567358&permissions=92160&scope=bot")
],)
return
if message.content == prefix + 'start':
if message.author.guild_permissions.administrator:
if not message.channel.id in channels:
channels.append(message.channel.id)
await message.channel.send("Der Vertretungsplan Bot ist auf diesen Channel aktiv")
with open("./data_files/data_file.json", "w") as read_file:
data = {
"channels": channels,
}
json.dump(data ,read_file)
return
else:
await message.channel.send("Der Bot ist schon auf diesem Channel aktiviert!")
return
if message.content == prefix + 'stop':
if message.author.guild_permissions.administrator:
if message.channel.id in channels:
channels.remove(message.channel.id)
await message.channel.send("Der Vertretungsplan Bot wurde für diesen Channel deaktiviert")
with open("./data_files/data_file.json", "w") as read_file:
data = {
"channels": channels,
}
json.dump(data ,read_file)
return
else:
await message.channel.send("Der Bot ist noch nicht auf diesem Channel aktiviert!")
return
if not message.channel.id in channels:
return
if not message.content.lower().strip("! ")[0].isdigit():
deleteInstant = message.id
await client.http.delete_message(message.channel.id, deleteInstant) #deletes the wrong message instantly
embedError = discord.Embed(title=":x: Error", description="Invalid command", color=0xfd0f02)
botError = await message.channel.send(embed=embedError)
client.loop.create_task(autodelete.deleteIn(client,botError,30)) #delete the bot Error after 30 seconds
return
if "morgen" not in message.content: # This is requesting the plan everytime a command is issued !TODO: make it check the age of the plan and use the already downloaded
today = False
# s.get_plan(False)
print("detected today")
else:
today = True
# s.get_plan(True)
klasse = message.content.strip("!").lower().replace("heute", "")
klasse = klasse.replace("morgen", "")
print(klasse)
if klasse[0].isdigit() and klasse[1] in list(string.ascii_lowercase) or klasse[0].isdigit() and klasse[1].isdigit() and klasse[2] in list(string.ascii_lowercase):
pass
else:
return
if not today:
d=0
else:
d=1
plan = Stundenplan.stundenplan.Stundenplan()
plan.getPlan(klasse, d)
# s.parse_plan(today=today)
embedPlanHeute = create_embed(klasse=klasse, s=plan)
# botEmbed = await message.channel.send(embed=embedPlanHeute)
botEmbed = await message.channel.send(
embed=embedPlanHeute,
components = [
Button(label = "Stundenplan anzeigen", style=1, id = message.id)
]
)
if today == True:
await autodelete.msgAddAutodelete(botEmbed, 2) # deletes the embed after 2 days
await autodelete.msgAddAutodelete(message, 2)
elif today == False:
await autodelete.msgAddAutodelete(botEmbed, 1) # deletes the embed after 1 day
await autodelete.msgAddAutodelete(message, 1)
#handle button clicks
while True:
interaction = await client.wait_for("button_click", check = lambda i: i.component.id == f"{message.id}")
for x in botEmbed.components[0]:
if x.label == "Stundenplan anzeigen":
await botEmbed.edit(embed=create_embed_regular(klasse=klasse, s=plan), components=[Button(label = "weniger anzeigen", style=1, id = message.id)])
x.label = "weniger anzeigen"
else:
await botEmbed.edit(embed=embedPlanHeute, components=[Button(label = "Stundenplan anzeigen", style=1, id = message.id)])
x.label = "Stundenplan anzeigen"
await interaction.respond(type=6)
# with open("./bot.token", "r") as IO_bot_token:
# token = IO_bot_token.read()
client.run(token)