b3b16fdc by Barry

Added msg functionality.

1 parent 90ce5a8e
{"Rui": {"Everybody": {"delivery_time": "2015/12/04 00:00:00", "message": "You are a faggot", "channel": "47934985176354816"}}}
\ No newline at end of file
......@@ -2,11 +2,15 @@ import requests
import discord
import random
import datetime
from dateutil.parser import parse
from discord.object import Object
from ago import human
import simplejson as json
from collections import defaultdict
member_status = 'members.json'
deliveries_file = 'deliveries.json'
fortune_file = 'fortunes.json'
games_file = 'games.json'
credentials = 'creds.json'
......@@ -17,6 +21,12 @@ json_data=open(credentials).read()
creds = json.loads(json_data)
client.login(creds['username'], creds['password'])
def leaders(xs, top=20):
counts = defaultdict(int)
for x in xs:
counts[x] += 1
return sorted(counts.items(), reverse=True, key=lambda tup: tup[1])[:top]
def byteify(input):
if isinstance(input, dict):
return {byteify(key):byteify(value) for key,value in input.iteritems()}
......@@ -27,6 +37,9 @@ def byteify(input):
else:
return input
@client.event
def on_socket_raw_send(payload, binary=False):
check_msg_queue()
@client.event
def on_status(member):
......@@ -40,6 +53,8 @@ def on_status(member):
data = {}
try:
username = member.name.lower()
user_id = member.id
mention = member.mention()
if username in data:
is_afk = data[username]['is_afk']
afk_at = data[username]['afk_at']
......@@ -70,6 +85,8 @@ def on_status(member):
if game_id not in games_played:
games_played.append(game_id)
data[username] = {
'id': user_id,
'mention': mention,
'is_afk': is_afk,
'afk_at': afk_at,
'status': status,
......@@ -97,6 +114,60 @@ def get_game_names(game_id_list):
result.append(game['name'])
return result
def get_mention_status(mention):
try:
json_data=open(member_status).read()
data = json.loads(json_data)
except ValueError:
data = {}
if not data:
data = {}
for user in data:
if 'mention' in data[user]:
if data[user]['mention'] == mention:
return data[user]
return None
def check_msg_queue():
print("checking messages")
try:
json_data=open(deliveries_file).read()
data = json.loads(json_data)
except ValueError:
data = {}
if not data:
data = {}
print("Data: %s" % data)
new_data = {}
for username in data:
for author in data[username]:
print("Message: %s" % data[username][author])
delivery = datetime.datetime.strptime(data[username][author]['delivery_time'], '%Y/%m/%d %H:%M:%S')
if delivery <= datetime.datetime.now():
offline = False
for member in client.get_all_members():
print('MEMBER MENTION: %s USERNAME: %s' % (member.mention(), username))
if username == member.mention():
if member.status != 'online':
print('OFFLINE USER, TRY AGAIN LATER')
offline = True
break
if offline:
break
channel = Object(data[username][author]['channel'])
message = data[username][author]['message']
client.send_message(channel, '{}, {} asked me to tell you "{}"'.format(username, author, message))
else:
new_data[username] = {}
new_data[username][author] = data[username][author]
jdata = json.dumps(new_data, ensure_ascii=False)
print("New Data: %s" % new_data)
open(deliveries_file, 'wb+').write(jdata.encode('utf8'))
return
@client.event
def on_message(message):
print message.content
......@@ -114,7 +185,20 @@ def on_message(message):
if message.content.startswith('!help') or message.content.startswith('!commands'):
client.send_message(message.channel, '{} Available Commands:\nYou can ask compound or questions and I will choose. Example: HellsBot Rui is a Faggot or Rui is a faggot?\n!games <username> - Returns a list of games played\n!lastseen <username> - Returns info on when the user was last seen and their status\n!addfortune <fortune>\n!fortune\n!secret\n!bemyirlwaifu'.format(message.author.mention()))
client.send_message(message.channel,
"""{} Available Commands:
You can ask compound or questions and I will choose. Example: HellsBot Rui is a Faggot or Rui is a faggot?
!msg <username> in 5 minutes Tea is ready
!msg <username> in 45 seconds Your finished masterbating
!msg <username> in 2 hours The movie is over
!msg <username> on 12/22/2015 Happy Birthday!
!games <username> - Returns a list of games played for a username.
!gameslist - Returns a list of the top 20 games and the number of people who have played that game.
!lastseen <username> - Returns info on when the user was last seen and their status.
!addfortune <fortune> - Adds a new fortune.
!fortune - Returns your fortune
!secret
!bemyirlwaifu""".format(message.author.mention()))
return
if message.content.startswith('!lastseen'):
......@@ -145,6 +229,33 @@ def on_message(message):
else:
client.send_message(message.channel, 'I don\'t have any data on {} yet {}'.format(username, message.author.mention()))
if message.content.startswith('!gameslist'):
data = None
try:
json_data=open(member_status).read()
data = json.loads(json_data)
except ValueError:
pass
if not data:
client.send_message(message.channel, 'I am a bit confused right now.. maybe I need more data. {}!'.format(message.author.mention()))
else:
game_list = []
for user in data:
if 'games_played' in data[user]:
print('%s' % data[user])
game_list += data[user]['games_played']
print('%s' % game_list)
games_sorted = leaders(get_game_names(game_list))
print('%s' % games_sorted)
out_string = ''
for game in games_sorted:
#print('%s' % game)
out_string += ' {} - {}\n'.format(game[1], game[0])
client.send_message(message.channel, 'The games I have seen people playing are: \n{}'.format(out_string))
return
if message.content.startswith('!games'):
data = None
try:
......@@ -161,6 +272,76 @@ def on_message(message):
client.send_message(message.channel, 'I have seen {} playing: {}'.format(username, games))
else:
client.send_message(message.channel, 'I don\'t have any data on {} yet {}'.format(username, message.author.mention()))
return
# !msg joe in 5 minutes YOU ARE A DICK
if message.content.startswith('!msg'):
try:
json_data=open(deliveries_file).read()
data = json.loads(json_data)
except ValueError:
data = {}
if not data:
data = {}
channel = message.channel
author = message.author
#author = message.author.name
username = ''
try:
message_bits = message.content.split(" ")
msg_datetime = datetime.datetime.now()
msg_idx = 2
if message_bits[2] == 'in' and message_bits[3].isdigit():
time = int(message_bits[3])
msg_idx = 4
if message_bits[4].startswith('sec'):
msg_datetime = msg_datetime + datetime.timedelta(seconds=time)
msg_idx = 5
elif message_bits[4].startswith('hour'):
msg_datetime = msg_datetime + datetime.timedelta(hours=time)
msg_idx = 5
else: # minutes by default
msg_datetime = msg_datetime + datetime.timedelta(minutes=time)
msg_idx = 5
elif message_bits[2] == 'on':
try:
tmp_date = parse(message_bits[3])
msg_datetime = tmp_date
msg_idx = 4
except ValueError:
client.send_message(channel, 'Your shitty message has been rejected {}. Next time learn how to date...MM\\DD\\YYYY'.format(message.author.mention()))
return
username = message_bits[1]
user_mention = ''
for member in client.get_all_members():
print("MEMBER: %s" % member)
if username.lower() == member.name.lower():
user_mention = member.mention()
user_id = member.id
if user_mention == '':
client.send_message(channel, 'Your shitty message has been rejected {}. That user does not exist.'.format(message.author.name))
return
msg_text = byteify(' '.join(message_bits[msg_idx:]))
message = {'user_id': user_id, 'channel': channel.id, 'delivery_time': msg_datetime.strftime('%Y/%m/%d %H:%M:%S'), 'message': msg_text}
print("Message: %s" % message)
data[user_mention] = {}
data[user_mention][author.mention()] = message
jdata = json.dumps(data, ensure_ascii=False)
print("Data: %s" % data)
#test_ch = Object(channel.id)
#client.send_message(test_ch, 'Test Message {}.'.format(author))
except Exception as e:
client.send_message(channel, 'Your shitty message has been rejected {}. {}'.format(author.name, e))
return
open(deliveries_file, 'wb+').write(jdata.encode('utf8'))
if msg_datetime < datetime.datetime.now():
client.send_message(channel, '{} your message will be delivered to {} as soon as they are available.'.format(author.name, user_mention))
else:
client.send_message(channel, '{} your message will be delivered to {} {}.'.format(author.name, user_mention, human(msg_datetime)))
check_msg_queue()
return
if message.content.startswith('!addfortune'):
try:
......@@ -178,7 +359,8 @@ def on_message(message):
return
open(fortune_file, 'wb+').write(jdata.encode('utf8'))
client.send_message(message.channel, 'Your shitty fortune has been added {}.'.format(message.author.mention()))
client.send_message(message.channel, 'Your shitty fortune has been added {}.'.format(message.author.mention()))
if message.content.startswith('!fortune'):
data = None
try:
......@@ -192,7 +374,7 @@ def on_message(message):
client.send_message(message.channel, '{} Your fortune is... {}'.format(message.author.mention(), random.choice(data).encode('utf-8',errors='ignore')))
if message.content.startswith('!secret'):
client.send_message(message.channel, 'git gud {}!'.format(message.author.mention()))
client.send_message(message.channel, 'git gud {}! My source is here: http://git.savsoul.com/barry/discordbot'.format(message.author.mention()))
if message.content.startswith('!bemyirlwaifu'):
client.send_message(message.channel, 'http://orig13.deviantart.net/b25e/f/2014/175/3/d/no_waifu_no_laifu_by_imtheonenexttome-d7nsx3b.gif {}!'.format(message.author.mention()))
......
{"zan": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 02:06:08", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 02:06:08"}, "solidsteak": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 02:43:24", "prev_status": "online", "games_played": [248, null], "game_id": null, "afk_at": "2015/12/03 02:43:24"}, "sheik": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/02 22:02:12", "prev_status": "online", "games_played": [283, null], "game_id": null, "afk_at": "2015/12/02 22:02:12"}, "rae": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 00:51:10", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 00:51:10"}, "marion": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 01:45:24", "prev_status": "online", "games_played": [1, null], "game_id": null, "afk_at": "2015/12/02 08:30:16"}, "shyrith": {"status": "online", "is_afk": false, "status_change_at": "2015/12/02 21:56:19", "prev_status": "idle", "games_played": [1, 644], "game_id": 644, "afk_at": "2015/12/02 21:54:35"}, "rui": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 01:46:23", "prev_status": "idle", "games_played": [], "game_id": null, "afk_at": "2015/12/03 01:35:09"}, "goshzilla": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 01:52:54", "prev_status": "idle", "games_played": [3, null], "game_id": null, "afk_at": "2015/12/03 01:50:54"}, "scoops": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 01:50:36", "prev_status": "offline", "games_played": [347, null, 0], "game_id": 0, "afk_at": "2015/12/02 08:26:41"}, "arka": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 02:21:16", "prev_status": "offline", "games_played": [1, null], "game_id": 1, "afk_at": "2015/12/03 01:04:59"}, "wriggle": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 00:23:48", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/02 15:37:49"}, "tim": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/02 09:53:25", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/02 09:23:29"}, "charisma": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 00:31:10", "prev_status": "idle", "games_played": [313], "game_id": 313, "afk_at": "2015/12/03 00:08:06"}, "sig": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 02:37:00", "prev_status": "online", "games_played": [8, null, 7], "game_id": null, "afk_at": "2015/12/03 02:37:00"}, "mr. nelson": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 02:18:48", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 02:18:48"}, "skeletonhorn": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 00:36:39", "prev_status": "offline", "games_played": [], "game_id": null, "afk_at": "2015/12/03 00:36:39"}, "azia": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 02:17:32", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 02:17:32"}, "projectaria": {"status": "online", "is_afk": false, "status_change_at": "2015/12/02 23:06:07", "prev_status": "idle", "games_played": [1, null], "game_id": null, "afk_at": "2015/12/02 22:31:46"}, "moss": {"status": "online", "is_afk": false, "status_change_at": "2015/12/02 21:56:26", "prev_status": "idle", "games_played": [0, null], "game_id": 0, "afk_at": "2015/12/02 21:53:22"}, "yobi": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 01:27:37", "prev_status": "idle", "games_played": [3], "game_id": 3, "afk_at": "2015/12/03 01:18:27"}, "salt": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 00:55:01", "prev_status": "idle", "games_played": [466, 8, null, 680], "game_id": null, "afk_at": "2015/12/03 00:54:11"}, "grey": {"status": "online", "is_afk": false, "status_change_at": "2015/12/02 20:32:11", "prev_status": "idle", "games_played": [550, null, 1], "game_id": 1, "afk_at": "2015/12/02 20:31:15"}, "hellsbreath": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 02:05:59", "prev_status": "idle", "games_played": [326, null, 660, 680], "game_id": null, "afk_at": "2015/12/02 22:37:41"}, "green": {"status": "online", "is_afk": false, "status_change_at": "2015/12/02 20:04:13", "prev_status": "offline", "games_played": [4], "game_id": 4, "afk_at": "2015/12/02 09:36:27"}, "xorfos": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 02:46:27", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 02:46:27"}, "richter": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 02:24:25", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/02 20:18:33"}, "cae": {"status": "online", "is_afk": false, "status_change_at": "2015/12/02 23:07:33", "prev_status": "idle", "games_played": [], "game_id": null, "afk_at": "2015/12/02 23:06:53"}}
\ No newline at end of file
{"zan": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 08:48:38", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 08:48:38"}, "solidsteak": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 06:54:57", "prev_status": "online", "games_played": [248, null], "game_id": null, "afk_at": "2015/12/03 06:54:57"}, "apple": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 07:23:35", "prev_status": "offline", "games_played": [1], "game_id": 1, "afk_at": "2015/12/03 07:23:35"}, "sheik": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 05:57:13", "prev_status": "online", "games_played": [283, null], "game_id": null, "afk_at": "2015/12/03 05:57:13"}, "rae": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 00:51:10", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 00:51:10"}, "marion": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 05:14:48", "prev_status": "online", "games_played": [1, null], "game_id": null, "afk_at": "2015/12/02 08:30:16"}, "shyrith": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 08:10:52", "prev_status": "online", "games_played": [1, 644, null], "game_id": null, "afk_at": "2015/12/02 21:54:35"}, "rui": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 04:15:47", "prev_status": "offline", "games_played": [], "game_id": null, "afk_at": "2015/12/03 03:48:05"}, "goshzilla": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 08:55:14", "prev_status": "idle", "games_played": [3, null], "game_id": null, "afk_at": "2015/12/03 08:51:54"}, "scoops": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 01:50:36", "prev_status": "offline", "games_played": [347, null, 0], "game_id": null, "afk_at": "2015/12/02 08:26:41"}, "arka": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 08:41:53", "prev_status": "online", "games_played": [1, null, 313], "game_id": null, "afk_at": "2015/12/03 01:04:59"}, "wriggle": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 00:23:48", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/02 15:37:49"}, "tim": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 07:56:42", "prev_status": "idle", "games_played": [], "game_id": null, "afk_at": "2015/12/03 07:37:21"}, "charisma": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 00:31:10", "prev_status": "idle", "games_played": [313, null], "game_id": null, "afk_at": "2015/12/03 00:08:06"}, "sig": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 09:09:59", "mention": "<@104055037243707392>", "prev_status": "online", "game_id": null, "games_played": [8, null, 7, 466], "afk_at": "2015/12/03 05:32:12", "id": "104055037243707392"}, "mr. nelson": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 07:26:31", "prev_status": "idle", "games_played": [], "game_id": null, "afk_at": "2015/12/03 07:14:03"}, "skeletonhorn": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 00:36:39", "prev_status": "offline", "games_played": [], "game_id": null, "afk_at": "2015/12/03 00:36:39"}, "azia": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 09:05:37", "mention": "<@121404665727418368>", "prev_status": "idle", "game_id": null, "games_played": [], "afk_at": "2015/12/03 02:17:32", "id": "121404665727418368"}, "projectaria": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 09:09:11", "mention": "<@48146724769763328>", "prev_status": "idle", "games_played": [1, null], "game_id": null, "afk_at": "2015/12/03 09:08:48", "id": "48146724769763328"}, "moss": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 07:54:49", "prev_status": "online", "games_played": [0, null, 1], "game_id": null, "afk_at": "2015/12/03 07:54:49"}, "yobi": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 08:53:52", "prev_status": "online", "games_played": [3], "game_id": 3, "afk_at": "2015/12/03 08:53:52"}, "salt": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 00:55:01", "mention": "<@48140539459010560>", "prev_status": "idle", "games_played": [466, 8, null, 680], "game_id": null, "afk_at": "2015/12/03 00:54:11", "id": "48140539459010560"}, "grey": {"status": "idle", "mention": "<@48120346410221568>", "is_afk": true, "games_played": [550, null, 1], "game_id": 1, "afk_at": "2015/12/03 09:13:13", "status_change_at": "2015/12/03 09:13:13", "id": "48120346410221568", "prev_status": "online"}, "hellsbreath": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 08:59:43", "mention": "<@78767557628133376>", "prev_status": "offline", "games_played": [326, null, 660, 680], "game_id": null, "afk_at": "2015/12/03 04:34:54", "id": "78767557628133376"}, "green": {"status": "online", "is_afk": false, "status_change_at": "2015/12/03 08:47:15", "prev_status": "offline", "games_played": [4, null], "game_id": null, "afk_at": "2015/12/03 07:45:29"}, "xorfos": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 03:16:08", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 03:16:08"}, "richter": {"status": "offline", "is_afk": false, "status_change_at": "2015/12/03 02:24:25", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/02 20:18:33"}, "cae": {"status": "idle", "is_afk": true, "status_change_at": "2015/12/03 08:27:14", "prev_status": "online", "games_played": [], "game_id": null, "afk_at": "2015/12/03 08:27:14"}}
\ No newline at end of file
......