-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_client.py
78 lines (64 loc) · 2.48 KB
/
discord_client.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
import discord
import sys
def split(message):
start_index = 0
parts = []
while len(message[start_index:]) > 2000:
final_index = min(start_index+1999, len(message)-1)
while message[final_index] != ' ':
final_index -= 1
if final_index == start_index:#if there are no spaces to split around
final_index = start_index+1999
break
parts.push(message[start_index:final_index])
start_index = final_index+2
parts.append(message[start_index:])
return parts
class DiscordClient(discord.Client):
def __init__(self, messages, names, usernames, addresses, giftees):
intents = discord.Intents.default()
intents.members = True
super().__init__(intents=intents)
self.usernames = usernames
self.messages = messages
self.names = names
self.addresses = addresses
self.giftees = giftees
async def on_ready(self):
print(f"We have logged in as {self.user}")
await self.send_matches()
async def send_matches(self):
fail = False
successful_users = []
for i in range(len(self.messages)):
user = discord.utils.get(self.get_all_members(),
name=self.usernames[i])
if user is None:
print("user " + self.usernames[i] + " not found",
file=sys.stderr)
fail = True
else:
try:
await user.send("Le tirage au sort va commencer, veuillez patienter...")
except:
print("Could not DM user " + self.usernames[i])
fail = True
else:
successful_users += [user]
if fail:
print("Aborting...")
for user in successful_users:
await user.send("Une erreur est survenue lors du tirage au sort, et il a été abandonné.")
raise RuntimeError
else:
for i in range(len(successful_users)):
for part in split(self.messages[i]):
await successful_users[i].send(part)
async def send_message(self, username, message):
await self.wait_until_ready()
user = discord.utils.get(self.get_all_members(),
name=username)
try:
await user.send(message)
except:
print("Could not DM user " + username)