-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJanusz.py
More file actions
257 lines (222 loc) · 11.1 KB
/
Janusz.py
File metadata and controls
257 lines (222 loc) · 11.1 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import discord, random, json, os, datetime, re, asyncio
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from hangman import Hangman
sched = AsyncIOScheduler()
client = discord.Client()
hangman_games = []
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
if message.author == client.user:
return
await process_message(message)
@client.event
async def on_message_edit(before, after):
if after.author == client.user:
return
await process_message(after)
@client.event
async def on_reaction_add(reaction, user):
if user == client.user or reaction.message.author == client.user:
return
reactions = reaction.message.reactions
for react in reactions:
if react.me:
return
if random.randint(0, 1) == 1:
if random.randint(0, 1) == 1:
await client.add_reaction(reaction.message, random.choice(reactions).emoji)
else:
await client.add_reaction(reaction.message, '\U0001f984')
async def send_announcement(announcement):
if client.is_logged_in:
for server in client.servers:
for channel in server.channels:
if channel.type != discord.ChannelType.text:
continue
if channel.type == discord.ChannelType.text and channel.position == 0:
if not announcement['working_day'] or (announcement['working_day'] and not is_holiday()):
await client.send_message(channel, random.choice(announcement['message']))
break
async def send_greet_user(message):
await client.send_message(message.channel, get_personal_greeting(get_person_name(message.author)))
reply = await client.wait_for_message(timeout=15, author=message.author, channel=message.channel)
if not reply:
await client.send_message(message.channel, get_person_name(message.author) + " why do you ignore me? :cry:")
async def send_single_name(message):
await client.send_message(message.channel, get_person_name(message.author))
reply = await client.wait_for_message(timeout=15, author=message.author, channel=message.channel)
if not reply:
await client.send_message(message.channel, get_person_name(message.author) + " was there something you meant to tell me? :thinking:")
async def send_single_mention(message):
await client.send_message(message.channel, message.author.mention)
reply = await client.wait_for_message(timeout=15, author=message.author, channel=message.channel)
if not reply:
await client.send_message(message.channel, message.author.mention + " was there something you meant to tell me? :thinking:")
async def send_daily_color(message):
await client.send_message(message.channel, "The color of the day is " + daily_color + " :rainbow:")
async def send_roll_dice(message):
if random.randint(0, 100) == 77:
await client.send_message(message.channel, ":scream_cat: Oh no! (void)dice :scream_cat:")
else:
await client.send_message(message.channel, "A :game_die: was cast by " + get_person_name(message.author) + " and the result was " + str(random.choice([1,2,3,4,5,6])))
async def send_random_member(message):
if message.channel.type == discord.ChannelType.private:
if len(message.channel.recipients) == 1:
await client.send_message(message.channel, "Wait what...! I thought it was just you and me here! :scream:")
else:
await client.send_message(message.channel, get_person_name(random.choice(message.channel.recipients)) + ", I choose you!")
else:
if len(message.channel.server.members) == 2:
await client.send_message(message.channel, "Wait what...! I thought it was just you and me here! :scream:")
else:
members = []
for member in message.channel.server.members:
if member.bot:
continue
members.append(member)
await client.send_message(message.channel, get_person_name(random.choice(members)) + ", I choose you!")
async def process_message(message):
msg = message.clean_content.lower()
await process_hangman(message, msg)
if 'janusz' in msg:
if 'janusz' == msg:
await send_single_name(message)
elif '@janusz' == msg:
await send_single_mention(message)
elif is_in_string_as_whole('play', msg) and not get_active_game(message.channel):
if is_in_string_as_whole('hangman', msg):
await start_hangman(message)
elif is_in_string_as_whole('color', msg):
await send_daily_color(message)
elif (is_in_string_as_whole('roll', msg) or is_in_string_as_whole('cast', msg)) and is_in_string_as_whole('dice', msg):
await send_roll_dice(message)
elif (is_in_string_as_whole('pick', msg) or is_in_string_as_whole('choose', msg) or is_in_string_as_whole('random', msg)) and is_in_string_as_whole('member', msg):
await send_random_member(message)
elif is_in_string_as_whole('tip', msg) or is_in_string_as_whole('help', msg):
await client.send_message(message.channel, random.choice(tips))
elif is_in_string_as_whole('joke', msg):
await client.send_message(message.channel, random.choice(jokes))
elif is_greeting_in_message(msg):
await send_greet_user(message)
else:
if is_in_string_as_whole('play', msg) and get_active_game(message.channel):
await client.send_message(message.channel, "Sorry " + get_person_name(message.author) + ", there is already an active game in this channel. :call_me:")
else:
await client.send_message(message.channel, "Sorry " + get_person_name(message.author) + ", I didn't quite catch that :confused:\nFeel free to ask me for a tip anytime :hugging:")
async def process_hangman(message, msg):
if get_hangman_game(message.channel):
if len(msg) == 1:
if msg.isalpha():
await update_hangman_state(message, msg)
else:
await client.send_message(message.channel, get_person_name(message.author) + " this isn't a letter! :rolling_eyes:")
elif msg == "state":
await client.send_message(message.channel, get_hangman_game(message.channel).get_state())
elif msg == "rules":
await client.send_message(message.channel, Hangman.rules)
elif len(msg.split()) == 2 and msg.split()[0] == "solve":
if msg.split()[1] == "your_word":
await client.send_message(message.channel, get_person_name(message.author) + " you should seriously reconsider your life choices... :pray:")
else:
await try_solve_hangman(message, msg.split()[1])
elif msg == "a single character":
await client.send_message(message.channel, "@everyone gather up, we have a genius here! :upside_down:")
async def start_hangman(message):
game = Hangman(random.choice(wordlist), message.channel)
hangman_games.append(game)
await client.send_message(game.channel, Hangman.rules)
await client.send_message(game.channel, game.get_life_drawing() + "\n" + game.get_solved())
async def check_hangman_end(message, game, end_result):
if end_result:
if end_result == "Lose":
dead_msg = await client.send_message(message.channel, Hangman.dead)
await asyncio.sleep(0.1)
await client.edit_message(dead_msg, Hangman.dead_left)
await asyncio.sleep(0.1)
await client.edit_message(dead_msg, Hangman.dead)
await asyncio.sleep(0.1)
await client.edit_message(dead_msg, Hangman.dead_right)
await asyncio.sleep(0.1)
await client.edit_message(dead_msg, Hangman.dead)
hangman_games.remove(game)
async def update_hangman_state(message, char):
game = get_hangman_game(message.channel)
result = game.update(char)
if result:
await client.send_message(message.channel, result[0].replace(game.author_sign(), get_person_name(message.author)))
await check_hangman_end(message, game, result[1])
async def try_solve_hangman(message, word):
game = get_hangman_game(message.channel)
result = game.solve(word)
if result:
await client.send_message(message.channel, result[0].replace(game.author_sign(), get_person_name(message.author)))
await check_hangman_end(message, game, result[1])
def get_active_game(channel):
return get_hangman_game(channel)
def get_hangman_game(channel):
for game in hangman_games:
if game.get_channel() == channel:
return game
return False
def get_personal_greeting(author):
message = ''
message += random.choice(greetings) + ' ' + author + '! '
message += random.choice(convo_starters)
return message
def get_person_name(person):
if type(person) == discord.Member:
name = person.nick if person.nick else person.name
return name
else:
return person.name.split(" ")[0]
def is_holiday():
day = datetime.date.today().day
month = datetime.date.today().month
for holiday in holidays:
date = holiday.split('-')
if int(date[0]) == day and int(date[1]) == month:
return True
return False
def set_daily_color():
global daily_color
daily_color = random.choice(colors)
def is_in_string_as_whole(word, message):
return True if re.search(r'\b' + word + r'\b', message) else False
def is_greeting_in_message(message):
for greeting in greetings:
if is_in_string_as_whole(greeting.lower(), message):
return True
return False
def load_json_file_from_data(filename):
f = open(os.path.join(data_folder, filename + ".json"), "r")
json_contents = f.read()
data = json.loads(json_contents)
f.close()
return data
data_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'Data')
colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet']
daily_color = ''
set_daily_color()
greetings = load_json_file_from_data("greetings")
convo_starters = load_json_file_from_data("convo_starters")
announcements = load_json_file_from_data("announcements")
holidays = load_json_file_from_data("holidays")
tips = load_json_file_from_data("tips")
jokes = load_json_file_from_data("jokes")
wordlist = load_json_file_from_data("wordlist")
f = open(os.path.join(data_folder, "token.txt"), "r")
token = f.read()
f.close()
#print(json.dumps(announcements, sort_keys=False, indent=4))
for announcement in announcements:
sched.add_job(send_announcement, 'cron', misfire_grace_time=10, year=announcement['year'], month=announcement['month'], day=announcement['day'], week=announcement['week'], day_of_week=announcement['day_of_week'], hour=announcement['hour'], minute=announcement['minute'], second=announcement['second'], args=[announcement])
sched.add_job(set_daily_color, 'cron', misfire_grace_time=10, hour=0, minute=0, second=0)
sched.start()
client.run(token)
# python D:\Projects\OtherProjects\JanuszBotPython\Janusz.py