-
Notifications
You must be signed in to change notification settings - Fork 0
/
bensvenn.py
executable file
·142 lines (100 loc) · 4.27 KB
/
bensvenn.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
#!/usr/bin/env python3
import discord
import random
import asyncio
import vennsteam as vs
import tokens
class MyClient(discord.Client):
async def on_ready(self):
print('Logged in as ' + self.user.name)
print('ID is ' + str(self.user.id))
print('------')
async def on_message(self, message):
# we do not want the bot to reply to itself
if message.author.id == self.user.id:
return
if message.content.startswith('$guess'):
await message.channel.send('Guess a number between 1 and 10.')
def is_correct(m):
return m.author == message.author and m.content.isdigit()
answer = random.randint(1, 10)
try:
guess = await self.wait_for('message', check=is_correct, timeout=5.0)
except asyncio.TimeoutError:
return await message.channel.send('Sorry, you took too long it was {}.'.format(answer))
if int(guess.content) == answer:
await message.channel.send('You are right!')
else:
await message.channel.send('Oops. It is actually {}.'.format(answer))
elif message.content.startswith('?bensvenn'):
# parse command to get target channel, defaut to General
target_channel = message.content[len('?bensvenn'):].strip()
target_handle = None
steamids = []
if target_channel == '':
target_channel = 'General'
# find a handle to target_channel
for c in client.get_all_channels():
if c.name == target_channel:
target_handle = c
break
# check channel and population
if target_handle is None:
await message.channel.send('no such voice channel %s' % target_channel)
return
if len(target_handle.members) == 0:
await message.channel.send("nobody's home in %s!" % target_channel)
return
# convert discord ids to steam ids
for usr in target_handle.members:
usr_id = str(usr.id)
if usr_id in dis_to_stm:
steamids.append(dis_to_stm[usr_id])
else:
await message.channel.send('No steamID registered for %s' % usr.name)
continue
# currently no on is in the mapping that's not my friend, but that could change
for sid in steamids:
if sid not in my_friends:
steam_nick = vs.get_names(steam_id=str(sid))
steamids.remove(sid)
await message.channel.send('%s not steam-friends with WhileTrueGame' % steam_nick[sid])
continue
if len(steamids) < len(target_handle.members):
await message.channel.send('(Only got games for %d people)' % len(steamids))
if len(steamids) > 0:
gameset = vs.venn_games(steam_ids=','.join(steamids), app_names=app_names)
report_all = 'Games everyone in %s has:\n' %target_channel
report_all += '\n'.join(gameset[len(steamids)])
if len(report_all) > 2000:
await message.channel.send('Character limit exceeded, this list will be abridged')
report_all = report_all[:2000]
await message.channel.send(report_all)
elif message.content.startswith('?debug'):
print('enter debug')
import ipdb; ipdb.set_trace()
print('exit debug')
def dump_guild(self):
G = self.guilds[0]
fid = open('guild_dump.tsv', 'w')
fields = ('Member id', 'name', 'discriminator', 'nick')
print('%s\t%s\t%s\t%s' % fields, file=fid)
for m in G.members:
fields = (m.id, m.name, m.discriminator, m.nick)
print('%s\t%s\t%s\t%s' % fields, file=fid)
fid.close()
# </Class definition>
# try loading some global lookup tables here ---------------
# discord to steam name map, make them all strings
dis_to_stm = {}
with open('user_database.tsv', 'r') as fid:
for line in fid.readlines():
discord_id, steam_id = line.strip().split('\t')
dis_to_stm[discord_id] = steam_id
# steam name to steam id map
my_friends = vs.get_my_friends()
# appid to game name map
app_names = vs.get_app_list()
# Loaded. Ready. Run! ------------------------------------
client = MyClient()
client.run(tokens.vennbot_token)