-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
87 lines (69 loc) · 2.7 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
from os import getenv
from dotenv import load_dotenv
load_dotenv()
from discord import Client, Intents
client = Client(intents=Intents.default())
# setting trigger time
from datetime import time, timezone
utc = timezone.utc
trigger_time = time(hour=5, minute=30, tzinfo=utc)
@client.event
async def on_ready():
print(f'{client.user.name} has connected to Discord!')
trigger_wotd.start()
trigger_potd.start()
trigger_qotd.start()
from discord.ext import tasks
@tasks.loop(time=trigger_time)
async def trigger_potd():
from wikimedia.image import fetch_image
image_data = fetch_image()
image_url = image_data.get("image_url")
image_page_id = image_data.get("image_page_id")
from wikimedia.caption import fetch_caption
caption = fetch_caption(image_page_id)
description = "**Wikimedia Commons: Picture of the day** \n\n## _*" + caption + "*_"
channel = client.get_channel(int(getenv("POTD_CHANNEL")))
from aiohttp import ClientSession
async with ClientSession() as session: # creates session
async with session.get(image_url) as resp: # gets image from url
img = await resp.read() # reads image from response
from io import BytesIO
with BytesIO(img) as file: # converts to file-like object
print(file.__sizeof__())
from discord import File
await channel.send(description, file=File(file, "t.png"))
@tasks.loop(time=trigger_time)
async def trigger_wotd():
from wordnik.word import WordNik
channel = client.get_channel(int(getenv("WOTD_CHANNEL")))
from datetime import date, datetime
cur_date = date.today()
date_iso = cur_date.isoformat()
word_text = WordNik().get_WOTD(date_iso)
x_date = datetime.now()
today = x_date.strftime("%A,%d %B, %Y")
caption = f'WordNik API Present: Word of the day [{today}]\n{word_text}'
await channel.send(caption)
@tasks.loop(time=trigger_time)
async def trigger_qotd():
from wikiquote import qotd
channel = client.get_channel(int(getenv("QOTD_CHANNEL")))
quote, author = qotd()
quote_text = f'```asciidoc\n.Quote of the Day\n“{quote}”\n\n.Author\n{author}\n```'
await channel.send(quote_text)
client.run(getenv("BOT_TOKEN"))
# @client.event
# async def on_message(message):
# if message.author == client.user:
# return
#
# if message.content == '$potd':
# channel_id = message.channel.id
# await trigger_potd(channel_id)
# elif message.content == '$wotd':
# channel_id = message.channel.id
# await trigger_wotd(channel_id)
# elif message.content == 'raise-exception':
# from discord import DiscordException
# raise DiscordException