Files
  • main.py
  • asyncer.py
  • keep_alive.py
  • requirements.txt
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import discord
import os
import asyncio
import traceback

from keep_alive import keep_alive
import asyncer

client = discord.Client()
devids = [
  '487258918465306634',
  '449654439490355231',
]

db = {}
db2 = {}
storagechannel = None
logchannel = None

async def load_db():
  global db,db2
  data = {}
  async for m in client.logs_from(storagechannel,limit = float('inf')):
    tx = m.content
    if tx.startswith('`') and ':' in tx:
      i = tx.index(':')
      id = int(tx[1:i])
      co = tx[i+1:-1]
      data[id] = co
    elif tx=='`DB BOUND`' and data:
      break
  if data=={}:
    db = {}
    return
  if max(data.keys())+1 != len(data):
    await reply('db has missing chunks',channel = logchannel)
    return
  tex = ''
  for x in range(len(data)):
    tex += data[x]
  db = eval(tex)
  db2 = eval(tex)#keep an original copy
async def save_db():
  await client.send_message(storagechannel,'`DB BOUND`')
  global db,db2
  data = repr(db)
  try:
    assert eval(data)==db#sanity check
  except SytaxError:
    await client.send_message(logchannel,'db-save err: db repr syntaxerror')
  except AssertionError:
    await client.send_message(logchannel,'db-save err: db repr doesn\'t eval to db')
  db2 = eval(data)
  i = 0
  cl = 1997
  chunkid = 0
  while i<len(data):
    try:
      chunk = data[i:i+cl]
      await client.send_message(storagechannel,'`%s:%s`'%(chunkid,chunk))
    except discord.errors.HTTPException:
        
      cl -= 1
    else:
      i += 2000
      cl = 1997
      chunkid += 1
async def clear_old_dbs():
  reached_olds = False
  async for msg in client.logs_from(storagechannel,limit = float('inf')):
    if not reached_olds:
      if msg.content=="`DB BOUND`":
        reached_olds = True
    else:
      await client.delete_message(msg)

@client.event
async def on_ready():
  global storagechannel,logchannel
  print('im in!')
  print('name: %s'%client.user.name)
  print('id: %s'%client.user.id)
  storagechannel = client.get_channel('541801748545798148')
  logchannel = client.get_channel('541801776836247562')
  await load_db()

msgs = 0

@client.event
async def on_message(message):
  global msgs
  if message.author !=  client.user:
    content = message.content
    splitContent = content.split(" ")
    channel = message.channel
    server=message.server
    if content=='spam':
        await client.send_message(channel, "Spam")
    elif content.lower().startswith('faq!'):#main command prefix
      cmd = content[4:]
      ##PUT MAIN COMMANDS HERE##
      if cmd=='':
        await client.send_message(channel,'**Help**\n Currently, there is no help to be displayed. I appologise for the inconvience.')
      elif cmd.startswith('add '):
          args = cmd[4:].split('|')
          #
          assert len(args)==3#this should eventually complain to the user 
          itemName = args[0]
          item = {'desc':args[1],'link':args[2]}#but wait serverName is the first one.
          if server.id not in db:
              db[server.id]={}
          db[server.id][itemName]=item
          #db=={server:{itemname:{itemdesc,itemlink}}}
    elif content.lower().startswith('db!') and message.author.id in devids:
      #these commands only work for devs
      cmd = content[3:]
      if cmd.startswith('eval '):
        val = eval(cmd[5:])
        await client.send_message(channel,"`%s`"%val)
      elif cmd=='killall':
        exit()
      #
      elif cmd=='reload':
        await load_db()
      elif cmd=='query':
        await client.send_message(channel,'`%r`'%db)
      elif cmd=='write':
        await save_db()
      elif cmd=='clear':
        await clear_old_dbs()
      elif cmd.startswith('store:'):
        m = content[6:]
        db[m] = 0
  msgs += 1
  if msgs !=  1:
    print("%s Messages have been sent"%msgs)
  else:
    print("1 message has been sent")
  #important stuff here, obviously
  #NO At the begining lol
  #nope right here

@client.event
async def on_error(event,message=None):
    print('err in %s'%event)
    traceback.print_exc()

token = os.getenv('DISCORD_BOT_SECRET')
if not token:
  print('no .env token!')
  input('press enter for non-bot tests')
  asyncer.start_waiter(asyncer._main)
else:
  keep_alive()
  #asyncio.ensure_future(asyncer.corowaiter())
  client.run(token)