Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
strolly
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
dnns01
strolly
Commits
824532b9
Commit
824532b9
authored
4 years ago
by
dnns01
Browse files
Options
Downloads
Patches
Plain Diff
Added functionality to add Appointments to a channel
parent
d8f63b99
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+2
-0
2 additions, 0 deletions
.gitignore
appointments_cog.py
+158
-0
158 additions, 0 deletions
appointments_cog.py
fernuni-bot.py
+10
-1
10 additions, 1 deletion
fernuni-bot.py
tops.json
+0
-6
0 additions, 6 deletions
tops.json
with
170 additions
and
7 deletions
.gitignore
+
2
−
0
View file @
824532b9
...
...
@@ -116,3 +116,5 @@ GitHub.sublime-settings
.history
/roles.json
/help.json
/appointments.json
/tops.json
This diff is collapsed.
Click to expand it.
appointments_cog.py
0 → 100644
+
158
−
0
View file @
824532b9
import
asyncio
import
datetime
import
json
import
re
import
discord
from
discord.ext
import
tasks
,
commands
class
AppointmentsCog
(
commands
.
Cog
):
def
__init__
(
self
,
bot
,
fmt
,
APPOINTMENTS_FILE
):
self
.
bot
=
bot
self
.
fmt
=
fmt
self
.
timer
.
start
()
self
.
appointments
=
{}
self
.
app_file
=
APPOINTMENTS_FILE
self
.
load_appointments
()
def
cog_unload
(
self
):
print
(
"
unload
"
)
self
.
timer
.
cancel
()
def
load_appointments
(
self
):
"""
Loads all appointments from APPOINTMENTS_FILE
"""
appointments_file
=
open
(
self
.
app_file
,
mode
=
'
r
'
)
self
.
appointments
=
json
.
load
(
appointments_file
)
@tasks.loop
(
minutes
=
1
)
async
def
timer
(
self
):
delete
=
[]
for
channel_id
,
channel_appointments
in
self
.
appointments
.
items
():
for
message_id
,
appointment
in
channel_appointments
.
items
():
now
=
datetime
.
datetime
.
now
()
date_time
=
datetime
.
datetime
.
strptime
(
appointment
[
0
],
self
.
fmt
)
remind_at
=
date_time
-
datetime
.
timedelta
(
minutes
=
appointment
[
1
])
if
now
>=
remind_at
:
try
:
channel
=
await
self
.
bot
.
fetch_channel
(
int
(
channel_id
))
message
=
await
channel
.
fetch_message
(
int
(
message_id
))
reactions
=
message
.
reactions
diff
=
int
(
round
(((
date_time
-
now
).
total_seconds
()
/
60
),
0
))
answer
=
f
"
Benachrichtigung!
\n
Der Termin
\"
{
appointment
[
2
]
}
\"
ist
"
if
appointment
[
1
]
>
0
and
diff
>
0
:
answer
+=
f
"
in
{
diff
}
Minuten fällig.
"
appointment
[
1
]
=
0
else
:
answer
+=
f
"
jetzt fällig. :loudspeaker:
"
delete
.
append
(
message_id
)
answer
+=
f
"
\n
"
for
reaction
in
reactions
:
if
reaction
.
emoji
==
"
👍
"
:
async
for
user
in
reaction
.
users
():
if
user
!=
self
.
bot
.
user
:
answer
+=
f
"
<@!
{
str
(
user
.
id
)
}
>
"
await
channel
.
send
(
answer
)
if
str
(
message
.
id
)
in
delete
:
await
message
.
delete
()
except
discord
.
errors
.
NotFound
:
delete
.
append
(
message_id
)
if
len
(
delete
)
>
0
:
for
key
in
delete
:
channel_appointments
.
pop
(
key
)
self
.
save_appointments
()
@timer.before_loop
async
def
before_timer
(
self
):
await
asyncio
.
sleep
(
60
-
datetime
.
datetime
.
now
().
second
)
@commands.command
(
name
=
"
add-appointment
"
)
async
def
cmd_add_appointment
(
self
,
ctx
,
date
,
time
,
reminder
,
title
):
"""
Add appointment to a channel
"""
channel
=
ctx
.
channel
try
:
date_time
=
datetime
.
datetime
.
strptime
(
f
"
{
date
}
{
time
}
"
,
self
.
fmt
)
except
ValueError
:
await
ctx
.
send
(
"
Fehler! Ungültiges Datums und/oder Zeit Format!
"
)
return
if
not
re
.
match
(
r
"
^\d+$
"
,
reminder
):
await
ctx
.
send
(
"
Fehler! Benachrichtigung muss eine positive ganze Zahl (in Minuten) sein!
"
)
return
embed
=
discord
.
Embed
(
title
=
"
Neuer Termin hinzugefügt!
"
,
description
=
f
"
Wenn du eine Benachrichtigung zum Beginn des Termins, sowie
{
reminder
}
"
f
"
Minuten vorher erhalten möchtest, reagiere mit :thumbsup: auf diese Nachricht.
"
,
color
=
19607
)
embed
.
add_field
(
name
=
"
Titel
"
,
value
=
title
,
inline
=
False
)
embed
.
add_field
(
name
=
"
Startzeitpunkt
"
,
value
=
f
"
{
date
}
{
time
}
"
,
inline
=
False
)
embed
.
add_field
(
name
=
"
Benachrichtigung
"
,
value
=
f
"
{
reminder
}
Minuten vor dem Start
"
,
inline
=
False
)
message
=
await
ctx
.
send
(
embed
=
embed
)
await
message
.
add_reaction
(
"
👍
"
)
await
message
.
add_reaction
(
"
🗑️
"
)
if
str
(
channel
.
id
)
not
in
self
.
appointments
:
self
.
appointments
[
str
(
channel
.
id
)]
=
{}
channel_appointments
=
self
.
appointments
.
get
(
str
(
channel
.
id
))
channel_appointments
[
str
(
message
.
id
)]
=
[
date_time
.
strftime
(
self
.
fmt
),
int
(
reminder
),
title
,
ctx
.
author
.
id
]
self
.
save_appointments
()
@commands.command
(
name
=
"
appointments
"
)
async
def
cmd_appointments
(
self
,
ctx
):
"""
List (and link) all Appointments in the current channel
"""
if
str
(
ctx
.
channel
.
id
)
in
self
.
appointments
:
channel_appointments
=
self
.
appointments
.
get
(
str
(
ctx
.
channel
.
id
))
answer
=
f
'
Termine dieses Channels:
\n
'
delete
=
[]
for
message_id
,
appointment
in
channel_appointments
.
items
():
try
:
message
=
await
ctx
.
channel
.
fetch_message
(
int
(
message_id
))
answer
+=
f
'
{
appointment
[
0
]
}
:
{
appointment
[
2
]
}
=>
'
\
f
'
{
message
.
jump_url
}
\n
'
except
discord
.
errors
.
NotFound
:
delete
.
append
(
message_id
)
if
len
(
delete
)
>
0
:
for
key
in
delete
:
channel_appointments
.
pop
(
key
)
self
.
save_appointments
()
await
ctx
.
channel
.
send
(
answer
)
else
:
await
ctx
.
send
(
"
Für diesen Channel existieren derzeit keine Termine
"
)
def
add_appointment
(
self
,
channel
):
pass
def
save_appointments
(
self
):
appointments_file
=
open
(
self
.
app_file
,
mode
=
'
w
'
)
json
.
dump
(
self
.
appointments
,
appointments_file
)
async
def
handle_reactions
(
self
,
payload
):
channel
=
await
self
.
bot
.
fetch_channel
(
payload
.
channel_id
)
channel_appointments
=
self
.
appointments
.
get
(
str
(
payload
.
channel_id
))
if
channel_appointments
:
appointment
=
channel_appointments
.
get
(
str
(
payload
.
message_id
))
if
appointment
:
if
payload
.
user_id
==
appointment
[
3
]:
message
=
await
channel
.
fetch_message
(
payload
.
message_id
)
await
message
.
delete
()
channel_appointments
.
pop
(
str
(
payload
.
message_id
))
self
.
save_appointments
()
This diff is collapsed.
Click to expand it.
fernuni-bot.py
+
10
−
1
View file @
824532b9
import
json
import
os
import
re
import
discord
from
discord.ext
import
commands
from
dotenv
import
load_dotenv
from
appointments_cog
import
AppointmentsCog
from
poll
import
Poll
# .env file is necessary in the same directory, that contains several strings.
...
...
@@ -16,9 +18,13 @@ OWNER = int(os.getenv('DISCORD_OWNER'))
ROLES_FILE
=
os
.
getenv
(
'
DISCORD_ROLES_FILE
'
)
HELP_FILE
=
os
.
getenv
(
'
DISCORD_HELP_FILE
'
)
TOPS_FILE
=
os
.
getenv
(
'
DISCORD_TOPS_FILE
'
)
APPOINTMENTS_FILE
=
os
.
getenv
(
"
DISCORD_APPOINTMENTS_FILE
"
)
DATE_TIME_FORMAT
=
os
.
getenv
(
"
DISCORD_DATE_TIME_FORMAT
"
)
PIN_EMOJI
=
"
📌
"
bot
=
commands
.
Bot
(
command_prefix
=
'
!
'
,
help_command
=
None
,
activity
=
discord
.
Game
(
ACTIVITY
),
owner_id
=
OWNER
)
appointments_cog
=
AppointmentsCog
(
bot
,
DATE_TIME_FORMAT
,
APPOINTMENTS_FILE
)
bot
.
add_cog
(
appointments_cog
)
assignable_roles
=
{}
tops
=
{}
...
...
@@ -198,7 +204,7 @@ async def cmd_remove_top(ctx, top):
"""
Remove TOP from a channel
"""
channel
=
ctx
.
channel
if
not
top
.
isnumeric
(
):
if
not
re
.
match
(
r
'
^-?\d+$
'
,
top
):
await
ctx
.
send
(
"
Fehler! Der übergebene Parameter muss eine Zahl sein
"
)
else
:
if
str
(
channel
.
id
)
in
tops
:
...
...
@@ -366,6 +372,9 @@ async def on_raw_reaction_add(payload):
await
poll
.
delete_poll
()
else
:
await
poll
.
close_poll
()
elif
payload
.
emoji
.
name
==
"
🗑️
"
and
len
(
message
.
embeds
)
>
0
and
\
message
.
embeds
[
0
].
title
==
"
Neuer Termin hinzugefügt!
"
:
await
appointments_cog
.
handle_reactions
(
payload
)
@bot.event
...
...
This diff is collapsed.
Click to expand it.
tops.json
deleted
100644 → 0
+
0
−
6
View file @
d8f63b99
{
"731078162334875688"
:
[
"Richtig nicer TOP"
,
"Letzter TOP!"
]
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment