-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
108 lines (90 loc) · 3.36 KB
/
app.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
from columnar import columnar
import mysql.connector
import discord
from dotenv import dotenv_values
vars = dotenv_values(".env")
whitelisted = vars["WHITELIST_IDS"]
syntax = vars["SYNTAX"]
TOKEN = vars["TOKEN"]
client = discord.Client()
config = {
'user': vars["USERNAME"],
'password': vars["PASSWORD"],
'host': vars["HOSTNAME"],
'database': vars["DATABASE"],
'raise_on_warnings': True
}
try:
cnx = mysql.connector.connect(**config)
cursor = cnx.cursor()
print("MySQL Connection Created Successfully")
except Exception as e:
print("Error at:")
print(e)
print()
print("Exiting...")
exit()
def exec(query):
try:
cursor.execute(query)
return cursor.fetchall()
except Exception as e:
return str(e)
# return ['ERR', e]
@client.event
async def on_message(message):
data = []
query = ""
if message.author == client.user:
return
if message.content.startswith(syntax):
if message.author.id not in whitelisted:
await message.channel.send(embed=discord.Embed(title="You are not authorized to use this bot",
description='Please contact the bot hoster to add you to '
'the whitelisted members list',
color=discord.Color.red()))
return
if message.content == syntax + "help":
await message.channel.send(
embed=discord.Embed(title=client.user.name, description='https://devhints.io/mysql',
color=discord.Color.purple()))
return
for word in message.content.split():
query += word + " "
query = query.replace(syntax, '')
if query == " ":
await message.channel.send(
embed=discord.Embed(title=client.user.name, description='Arguments are missing for the query',
color=discord.Color.red()))
return
output = exec(query)
if not output:
msg = await message.channel.send(
embed=discord.Embed(description="Empty list returned", color=discord.Color.blue()))
return
if isinstance(output, list):
for result in output:
sub_data = []
for x in result:
sub_data.append(x)
data.append(sub_data)
await message.channel.send(columnar(data, no_borders=False))
# await message.channel.send(output)
return
else:
msg = await message.channel.send(
embed=discord.Embed(title="MySQL Returned an Error", description=output, color=discord.Color.orange()))
await msg.add_reaction("⚠️")
return
@client.event
async def on_ready():
print(f"Bot | Status: Operational")
print(f"Bot | ID: {format(client.user.id)}")
print(f"Bot | Name: {format(client.user.name)}")
print(f"Bot | Guilds: {len(client.guilds)}")
print(f"Bot Configurations set to:\n{config}")
print(f"Bot is ready to use")
# ? Custom Activity
await client.change_presence(
activity=discord.Activity(type=discord.ActivityType.listening, name="Syntax = " + syntax))
client.run(TOKEN)