summaryrefslogtreecommitdiff
path: root/grabbers/discord_to_redis.py
blob: 0b21d26c242d005519c313e812d4183cb29cad12 (plain)
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
#!/usr/bin/python3

import discord
import redis
import asyncio
import config

client = discord.Client()
r = redis.StrictRedis(**{ **config.redis, "decode_responses": True })

"""
Save a message to redis and store statistics.
@return True if the message belongs to a channel without known history.
"""
def store_message(message):
    if message.author.bot:
        return False

    r.set("message:{}".format(message.id), message.content)
    r.incr("stats:{}:{}:{}".format(message.server.id, message.channel.id, message.author.id))
    return r.incr("stats:{}:{}".format(message.server.id, message.channel.id)) == 1


@client.event
async def on_message(message):
    print(message.content)

    fetch_history = store_message(message)
    if fetch_history:
        print("+++ fetching channel history +++")
        try:
            async for logmsg in client.logs_from(message.channel, before=message, limit=100000):
                store_message(logmsg)
        except Exception as e:
            pass  # forbidden, TODO use proper error code

    r.publish("discord_to_redis", "")  # notify of new content


client.run(*config.discord, bot=False)