-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
51 lines (46 loc) · 1.52 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
#!/usr/bin/env python3
from discord.ext.commands import Bot
import datetime
import discord
import feedparser
import os
import pytz
BOT_PREFIX = ("?", "!")
TOKEN = os.environ['TOKEN']
def convert_to_local_datetime(date):
# Set UTC
utc = pytz.UTC
# Set local timezone
# TODO get dynamically
pst = pytz.timezone('Europe/Zurich')
# Parse the CTFtime ISO date
d = datetime.datetime.strptime(date, '%Y%m%dT%H%M%S')
# Define it's UTC
d = utc.localize(d)
# Convert to local timezone
d = d.astimezone(pst)
# Create a human readable format
dt = d.strftime('%d. %b %Y @ %H:%M')
return dt
client = Bot(command_prefix=BOT_PREFIX)
@client.event
async def on_ready():
print("Logged in as " + client.user.name)
@client.command()
async def ctf():
url = 'https://ctftime.org/event/list/upcoming/rss/'
d = feedparser.parse(url)
# Next 3 CTFs
for post in d.entries[:3]:
description = ''
# Convert to local datetime
start = convert_to_local_datetime(post.start_date)
finish = convert_to_local_datetime(post.finish_date)
# Create answer
description+= "From: %s \n Until: %s" % (start, finish)
e=discord.Embed(title=post.title, description="["+post.title+"] ("+post.link+")")
# https://cog-creators.github.io/discord-embed-sandbox/
msg=discord.Embed(title=post.title, url=post.url, description=description)
msg.set_thumbnail(url="https://ctftime.org"+post.logo_url)
await client.say(embed=msg)
client.run(TOKEN)