blob: f5b850afc76140ede665e044c4310d3f93bd279c (
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
41
42
43
44
45
46
|
#!/usr/bin/python3
import logging
import asyncio
import discord
import redis
import config
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
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):
logging.info("{}#{}@{}: {}".format(message.server.name, message.channel.name, message.author.name, message.clean_content))
fetch_history = store_message(message)
if fetch_history:
logging.info("+++ 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)
|