-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
156 lines (120 loc) · 5.2 KB
/
main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from typing import Final
import os
from dotenv import load_dotenv
from discord import Intents, Client, Message, app_commands, utils, Embed
import asyncio
from difflib import SequenceMatcher
import random
from datetime import timedelta
from random import randrange
load_dotenv()
TOKEN: Final[str] = os.getenv('DISCORD_TOKEN')
intents: Intents = Intents.default()
intents.message_content = True
client: Client = Client(intents=intents)
tree = app_commands.CommandTree(client)
def similar(a, b):
return SequenceMatcher(None, a, b).ratio()
@client.event
async def on_ready():
await tree.sync()
print("Ready!")
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
@client.event
async def on_message(message):
if message.author == client.user:
return
args = message.content.split()
if len(args) == 0:
return
send_channel = message.channel
if args[0] == "!gm":
scan_channel = message.channel
if len(args) >= 2:
scan_channel = utils.get(client.get_all_channels(), name=args[1])
else:
await send_channel.send("Did not specify channel! Will be guessing messages from #" + str(message.channel))
elif args[0] == "!gc":
text_channel_list = []
for channel in message.guild.text_channels:
text_channel_list.append(channel)
scan_channel = random.choice(text_channel_list)
else:
return
recent = False
if "recent" in args:
recent = True
await send_channel.send("Selecting from recent messages!")
else:
await send_channel.send("Selecting from all time messages!")
await send_channel.send("Starting game...")
# pick message
try:
first_message_date = [message async for message in scan_channel.history(limit=1, oldest_first=True)][0].created_at
last_message_date = [message async for message in scan_channel.history(limit=1, oldest_first=False)][0].created_at
except:
await send_channel.send("Did not have access to channel #" + str(args[1]))
return
if not recent:
messages = [message async for message in scan_channel.history(limit=100, around=random_date(first_message_date, last_message_date))]
else:
messages = [message async for message in scan_channel.history(limit=1000)]
def pick():
index = random.randrange(0, len(messages))
message_to_guess = messages[index]
if message_to_guess.embeds:
return pick()
return message_to_guess
def checkAuthor(m):
print("should be " + str(message_to_guess.author))
print("guess " + m.content)
correctGuesser = m.author
return (similar(m.content.lower(), str(message_to_guess.author).lower())) > 0.7 or similar(m.content.lower(), str(message_to_guess.author.display_name).lower()) > 0.7
def checkChannel(m):
print("should be " + str(message_to_guess.channel))
print("guess " + m.content)
return (similar(m.content.lower(), str(message_to_guess.channel).lower())) > 0.7
message_to_guess = pick()
if args[0] == "!gm":
embed = Embed(title="Who sent this?")
if args[0] == "!gc":
embed = Embed(title="What channel was this sent in?")
embed.description = str(message_to_guess.author) + ": "
if message_to_guess.attachments:
embed.set_image(url=message_to_guess.attachments[0].url)
elif message_to_guess.embeds:
embed.description += message_to_guess.embeds[0].description
if message_to_guess.embeds[0].image:
embed.set_image(url=message_to_guess.embeds[0].image.url)
else:
embed.description += message_to_guess.content
await send_channel.send(embed=embed)
try:
if args[0] == "!gm":
msg = await client.wait_for("message", check=checkAuthor, timeout=15.0)
else:
msg = await client.wait_for("message", check=checkChannel, timeout=15.0)
response_embed = Embed(title=str(msg.author) + " got it right!")
response_embed.description = "It was " + str(message_to_guess.author.display_name) + " (" + str(message_to_guess.author) + ")\n\n" + message_to_guess.jump_url
if args[0] == "!gc":
response_embed.description = "It was sent in #" + str(message_to_guess.channel) + "\n\n" + message_to_guess.jump_url
response_embed.set_image(msg.author.avatar_url)
await send_channel.send(embed=response_embed)
except asyncio.TimeoutError:
response_embed = Embed(title="Time's Up!")
response_embed.description = "Message was sent by " + str(message_to_guess.author.display_name) + " (" + str(message_to_guess.author) + ")\n\n" + message_to_guess.jump_url
if args[0] == "!gc":
response_embed.description = "Message was sent in #" + str(message_to_guess.channel)
return await send_channel.send(embed=response_embed)
def main():
client.run(token=TOKEN)
if __name__ == '__main__':
main()