-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHiveSQLBot.py
69 lines (52 loc) · 1.93 KB
/
HiveSQLBot.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
import os
import discord
from discord.ext import tasks
from dotenv import load_dotenv
import pypyodbc
from table2ascii import table2ascii as t2a, PresetStyle
load_dotenv()
discordToken = os.environ.get("DISCORD_TOKEN")
discordAdmin = os.environ.get("DISCORD_ADMIN_ID")
hivesqlServer = os.environ.get("HIVESQL_SERVER")
hivesqlDatabase = os.environ.get("HIVESQL_DATABASE")
hivesqlUser = os.environ.get("HIVESQL_USER")
hivesqlPwd = os.environ.get("HIVESQL_PWD")
intents = discord.Intents.all()
client = discord.Client(command_prefix='!', intents=intents)
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('hi'):
await message.channel.send(message.author)
if message.content.startswith('!hivesql'):
params = message.content.split(" ", 1)
SQLCommand = params[1]
connection = pypyodbc.connect('Driver={ODBC Driver 17 for SQL Server};'
'Server='+hivesqlServer+';'
'Database='+hivesqlDatabase+';'
'uid='+hivesqlUser+';'
'pwd='+hivesqlPwd)
cursor = connection.cursor()
result = cursor.execute(SQLCommand)
result = result.fetchmany(100)
connection.close()
#print(result)
header = []
for column in cursor.description:
header.append(column[0])
output = t2a(
header=header,
body=result,
style=PresetStyle.thin_compact
)
f = open("sqlresult.txt", "w")
f.write(output)
f.close()
await message.channel.send(file=discord.File(r'sqlresult.txt'))
#await message.channel.send(f"```\n{output}\n```")
return
client.run(discordToken)