· 4 years ago · Sep 12, 2021, 04:56 PM
1#!/usr/bin/python3.5
2# -*- coding: utf-8 -*-
3
4import MySQLdb as mdb
5import discord
6import asyncio
7import sys
8
9class bcolors:
10 OKBLUE = '\033[94m'
11 OKGREEN = '\033[92m'
12 WARNING = '\033[93m'
13 FAIL = '\033[91m'
14 RESET = '\033[0m'
15 UNDERLINE = '\033[4m'
16
17
18i = 0
19while i<100: # Just a little hack to clear the screen on every OS
20 print('\n')
21 i = i+1
22
23client = discord.Client() #Create a discord client
24
25mdb.use_unicode=True
26db = mdb.connect(host='localhost', user='BadLogger', passwd='nope tu l'auras pas :3', db='discordlog', charset='utf8') #connect to database
27# db.charset("utf8")
28cur = db.cursor()
29cur.execute("SELECT VERSION()") # Get version of SQL to test the db connection
30version = cur.fetchone()
31
32print(bcolors.UNDERLINE+"\n\nDatabase version :"+bcolors.RESET+" %s \n" % version) # Print the version number (with a beautiful formatting <3)
33
34
35@client.event
36async def on_ready():
37 print("Discord client :"+bcolors.OKGREEN+" ready\n"+bcolors.RESET) # Prints a message to see if the discord client is successfully connected
38
39 chans = []
40 txtChans = []
41 for chan in client.get_all_channels() : #get all the channels of the discord server
42 chans.append(chan)
43
44 print(bcolors.UNDERLINE+"Channels actually visible :"+bcolors.RESET) # Prints a recap of all the visible chans in the terminal
45
46 for chan in chans:
47 tmpstr = "Name : %20s \t --- \t Type : %20s" % (chan.name, str(chan.type))
48 if str(chan.type) == 'text':
49 txtChans.append(chan)
50 print(tmpstr)
51
52 tmpstr = bcolors.UNDERLINE+"\n\nTotal :"+bcolors.RESET+" %3s Channels\n\t%3s Text\n\t%3s Voice" % (str(len(chans)), str(len(txtChans)), str(len(chans)-len(txtChans)))
53 print(tmpstr)
54
55 if len(chans)-len(txtChans) > 0: # Warns the user that the bot only logs the text chans
56 print("\n"+bcolors.WARNING+bcolors.UNDERLINE+"WARNING :"+bcolors.RESET+" actually only logging text channels \n\n")
57
58 try:
59 for chan in txtChans: # Creates a SQL table for each channel
60 name = chan.name
61 cur.execute("DROP TABLE IF EXISTS "+name)
62 sql = " CREATE TABLE "+name+"""
63 (
64 `ID` INT PRIMARY KEY AUTO_INCREMENT,
65 `AUTHOR` VARCHAR(255),
66 `MESSAGE` TEXT(2000),
67 `DATE` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
68 ) ENGINE=InnoDB DEFAULT CHARSET=utf8"""
69 cur.execute(sql)
70 except Exception as e:
71 print("ERROR %s : %s"%(str(e.args[0]), e.args[1]))
72
73 print(bcolors.OKGREEN+bcolors.UNDERLINE+"\n\nAll tables succesfully created\n\n"+bcolors.RESET)
74
75@client.event
76async def on_message(message): # every time a new message is post on the discord server
77 if message.author.nick: #if the author has a nickname
78 author = message.author.nick #get the nickname
79 else: #else
80 author = message.author.name #get his name
81
82 content = message.content #get the message content
83 time = message.timestamp #get the message timestamp (only useful to print it on the terminal)
84
85 # print(bcolors.UNDERLINE+bcolors.WARNING+message.channel.name+" = "+bcolors.OKBLUE+str(message.timestamp)[5:19]+" | "+bcolors.OKGREEN+author+" : "+bcolors.RESET+message.content)
86
87 sql = "INSERT INTO "+message.channel.name+"(`AUTHOR`, `MESSAGE`) VALUES (`"+author+"`, `"+content+"`)" #put the message on the right table
88 print (bcolors.FAIL+"\n\n---------------DEBUG-------------\n"+bcolors.RESET)
89 print(sql)
90 try:
91 cur.execute(sql)
92 except Exception as e:
93 print(bcolors.FAIL+"\n ERROR \n "+bcolors.RESET)
94 for err in e.args:
95 print (err)
96 print (bcolors.FAIL+"\n\n---------------END DEBUG-------------\n"+bcolors.RESET)
97
98 # try:
99 # cur.execute(sql)
100 # except Exception as e:
101 # print("ERROR %s : %s"%(str(e.args[0]), e.args[1]))
102
103client.run("J'vais pas te donner mon token nanmého !") #launch the discord client
104
105# db.close()