-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.py
134 lines (104 loc) · 3.63 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
# bot.py
import csv
import datetime
import logging
import os
from typing import Dict, List
import discord
import yaml
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
# Logging configuration
logging.basicConfig(level=logging.INFO)
# Discord configuration
intents = discord.Intents.all()
client = discord.Client(intents=intents)
advent_calendar = {}
files_to_read = ["themes.csv", "music_advent_2023.csv"]
for file_to_read in files_to_read:
with open(file_to_read) as f:
csv_reader = csv.reader(f)
for line in csv_reader:
advent_calendar[line[0]] = line[1]
channel_ids: List[int] = []
verification_channel_ids = []
verification_role_id: Dict[int, int] = {}
async def advent_event():
today = datetime.date.today().strftime("%Y-%m-%d")
logging.info(f"Today is {today}, let's check the theme")
try:
theme = advent_calendar[today]
except KeyError:
logging.info("No theme for today")
return
logging.info(f"The theme is: {theme}")
for channel_id in channel_ids:
await client.get_channel(channel_id).send(f"Dzisiejszy temat to: {theme}")
def read_roles(reaction):
try:
config = open(
"role_config.yaml",
)
except FileNotFoundError:
return None
message_id = reaction.message_id
emoji_name = reaction.emoji.name
roles = yaml.safe_load(config, Loader=yaml.FullLoader)
role_info_message = int(roles["role_info_message_id"])
roles_kv = roles["roles"]
if message_id == role_info_message and emoji_name in roles_kv:
return int(roles_kv[emoji_name])
return None
async def add_role(member, role_id):
role_to_add = discord.utils.get(member.guild.roles, id=role_id)
await member.add_roles(role_to_add)
async def handle_reaction(reaction, state):
role_id = read_roles(reaction)
if role_id:
await add_role(reaction.member, role_id)
@client.event
async def on_raw_reaction_add(reaction):
await handle_reaction(reaction, "done")
@client.event
async def on_raw_reaction_remove(reaction):
await handle_reaction(reaction, "")
@client.event
async def on_message(message):
if (
message.channel.id in verification_channel_ids
and message.content.strip().lower() == "t"
):
logging.info(f"{message.author} verified!")
await add_role(message.author, verification_role_id[message.guild.id])
@client.event
async def on_ready():
global channel_ids
for guild in client.guilds:
logging.info(f"{client.user} has connected to Discord server {guild}!")
for channel in guild.channels:
if isinstance(channel, discord.TextChannel):
if "music" in channel.name:
channel_ids.append(channel.id)
if "weryfikacja" in channel.name:
verification_channel_ids.append(channel.id)
logging.info(
f"Verification channel for {guild.name} ({guild.id}) is {channel.id}"
)
for role in guild.roles:
if role.name == "weryfikacja":
logging.info(
f"Verification role for {guild.name} ({guild.id}) is {role.id}"
)
verification_role_id[guild.id] = role.id
scheduler = AsyncIOScheduler()
scheduler.add_job(
advent_event,
CronTrigger(day_of_week="0-6", hour="4", minute="45", second="0"),
id="advent_event",
replace_existing=True,
)
scheduler.start()
client.run(TOKEN)