-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
153 lines (110 loc) · 4.06 KB
/
main.py
File metadata and controls
153 lines (110 loc) · 4.06 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import gspread
from google.oauth2.service_account import Credentials
import discord
import zenquotespy
# Load our APIs for google sheets
google_scopes = [
"https://www.googleapis.com/auth/spreadsheets"
]
google_creds = Credentials.from_service_account_file("google.json", scopes=google_scopes)
google_client = gspread.authorize(google_creds);
#Definetely could be loaded better but eh this is a favor for someone who doesn't know computers
# Now get our id for our sheet
sheet_id = ""
gid = ""
with open("sheet_id.txt", 'r', encoding='utf-8') as sheetfile:
sheet_id = sheetfile.read();
with open("sheet_gid.txt", 'r', encoding='utf-8') as gidfile:
gid = gidfile.read();
# Also get ID for discord
discord_token = ""
discord_channel_id = 0
discord_guild_id = 0
with open("discord.txt", 'r', encoding='utf-8') as tokenfile:
discord_token = tokenfile.read();
with open("channel.txt", 'r', encoding='utf-8') as channelfile:
discord_channel_id = int(channelfile.read());
with open("guild.txt", 'r', encoding='utf-8') as guildfile:
discord_guild_id = int(guildfile.read());
# And use the ID to get our sheet
sheet = google_client.open_by_key(sheet_id).get_worksheet_by_id(gid)
# Hardcoded values that are relevant to my spreadsheet, yours may differ
static_ids = sheet.row_values(37)
static_names = sheet.row_values(35)
dyanmic_ids = sheet.row_values(38)
dyanmic_names = sheet.row_values(36)
role_id = sheet.get("B39")
names = []
ids = []
for id in static_ids:
try:
ids.append(int(id))
except ValueError:
pass
for id in dyanmic_ids:
try:
ids.append(int(id))
except ValueError:
pass
static_names.pop(0)
dyanmic_names.pop(0)
for name in static_names:
if name != "":
names.append(name)
for name in dyanmic_names:
if name != "":
names.append(name)
print(ids)
print(names)
message = f"Today is <@&{role_id[0][0]}>. Fair winds and best of luck out there! 👋"
daily_quote = zenquotespy.today()
message += f"\n\n\{daily_quote}"
if len(names) == 1:
message += f"\n\nToday is {names[0]}'s Kelly day! Have fun! 🥳"
elif len(names) > 1:
message += "\n\nToday is "
first_name = names.pop(0)
last_name = names.pop()
message += first_name
for name in names:
message += f", {name}"
message += f" and {last_name}'s Kelly day! Have fun! 🥳"
class MyClient(discord.Client):
async def on_ready(self):
await discord.Client.wait_until_ready(self)
# get everything we need
channel = discord.Client.get_channel(self, discord_channel_id)
guild = discord.Client.get_guild(self, discord_guild_id)
role = guild.get_role(int(role_id[0][0]))
def check_added(old, new):
return old.id == new.id and role in new and role not in old
def check_removed(old, new):
return old.id == new.id and role in old and role not in new
if channel and guild and role:
print("Removing discord roles...")
# Remove everyone's role that is on a kelly day so they don't get pung
for id in ids:
print(id)
member = guild.get_member(int(id))
print(member)
if (member):
await member.remove_roles(role)
#await client.wait_for('member_update', check=check_removed, timeout = 60.0)
print ("Sending message...")
await channel.send(message)
print ("Adding discord roles...")
# add everyone's role back since they're still there!
for id in ids:
member = guild.get_member(int(id))
print(member)
if (member):
await member.add_roles(role)
#await client.wait_for('member_update', check=check_added, timeout = 60.0)
# Now we are done!
await client.close()
exit(0)
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = MyClient(intents = intents)
client.run(discord_token)