-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
61 lines (54 loc) · 1.74 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
#vote-checking-bot
import os
import discord
import botVoteData as data
from dotenv import load_dotenv
load_dotenv(".env")
TOKEN = os.getenv('DISCORD_TOKEN')
intents = discord.Intents.default()
client = discord.Client(intents=intents)
channel = ""
reaction = "\u267B"
@client.event
async def on_ready():
print(f'connected as {discord.user}')
await update_attendance()
# Utility function that returns the attendance channel
def get_channel():
channels = client.get_all_channels()
for chnl in channels:
if chnl.name == "attendance":
channel_id = chnl.id
break
channel = client.get_channel(channel_id)
return channel
# Utility function that returns the first message in the attendance channel
async def get_message():
channel = get_channel()
messages = await channel.history(oldest_first=True).flatten()
if len(messages)>0:
first = messages[0]
await channel.send("...Wait out.")
for message in messages[1:]:
await message.delete()
return first
else:
await channel.send("...Wait out.")
return await get_message()
# Render the attendance image and provide a reaction to update
async def update_attendance():
data.render_attendance()
image = discord.File("attendance.png")
#Delete the first message (old attendance information)
first = await get_message()
await first.delete()
#Send updated information in its place
await get_channel().send(file=image)
message = await get_message()
await message.add_reaction(reaction)
# Regenerate the attendance once a reaction is pressed.
@client.event
async def on_reaction_add(reaction, user):
if user != client.user:
await update_attendance()
client.run(TOKEN)