-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMyBot.py
More file actions
executable file
·237 lines (205 loc) · 7.95 KB
/
MyBot.py
File metadata and controls
executable file
·237 lines (205 loc) · 7.95 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python3
import discord
from discord.ext import tasks, commands
import json
import psutil
import time
import os
from dotenv import load_dotenv
from discord.ext.commands import CommandNotFound
import subprocess
from requests import get
from pretty_help import PrettyHelp
import aiocron
import urllib.request
import requests
import datetime
import logging
import pandas as pd
from bs4 import BeautifulSoup
load_dotenv(dotenv_path=".env")
logging.basicConfig(filename=os.getenv("LOG_FILE"), level=logging.INFO, filemode='a', format='%(asctime)s - %(levelname)s - %(message)s')
with open('commands.json') as command_json:
command_list = json.load(command_json)
bot = commands.Bot(
command_prefix=os.getenv("PREFIX"),
owner_id=int(os.getenv("OWNER_ID")),
case_insensitive=True,
help_command=PrettyHelp()
)
owner_id = bot.owner_id
""" Owner Check """
async def cog_check(self, ctx):
# Making sure author of these commands is the bot owner
return await ctx.bot.is_owner(ctx.author)
@bot.event
async def on_ready():
# print('MyBot has logged with username: {0.user}'.format(bot))
embed=discord.Embed(
title = "MyBot is Online!",
description = "MyBot has successfully started",
color = discord.Color.green()
)
logging.info("MyBot has successfully started")
user = await bot.fetch_user(owner_id)
await user.send(embed=embed)
""" Error Check """
async def cog_command_error(ctx, error):
# Handling any errors within commands
logging.error(str(error))
embed = discord.Embed(
title=f"Error in { ctx.command.qualified_name }",
colour=discord.Color.red(),
description=dedent(f"""
{ error }
Use `{ self.bot.command_prefix }help { ctx.command.qualified_name }` for help with the command.
""")
)
return await ctx.send(embed=embed)
@bot.event
async def on_command_error(ctx, error):
# Handling any errors when calling a command
# if isinstance(error, CommandNotFound):
logging.error(str(error))
embed=discord.Embed(
title = "Oh snap! An error occured",
description = "The error was: " + str(error),
color = discord.Color.red()
)
return await ctx.send(embed=embed)
@bot.before_invoke
async def command_logger(ctx):
#server = ctx.guild.name #Not required for MyBot
logging.info(str(ctx.author) + " ran the command "+ bot.command_prefix + str(ctx.command))
@bot.command(name='shell', description="Run Shell commands from JSON")
async def run_shell(ctx, message):
for command, value in command_list.items():
if str(command) == message:
output = subprocess.getoutput(value)
embed=discord.Embed(
title = "Command Run: '" + command + "'",
description = output,
color = discord.Color.green()
)
return await ctx.send(embed=embed)
embed=discord.Embed(
title = "No Shell command found",
description = "Did not match " + command + ". Commands include: " + str(command_list),
color = discord.Color.red()
)
return await ctx.send(embed=embed)
@bot.command(name='test', description="Test DM command")
async def dm_test(ctx):
user = await bot.fetch_user(owner_id)
await user.send("Test Message Here")
@bot.command(name='myip', description="Returns public IP")
async def my_ip(ctx):
ip = get('https://api.ipify.org').text
embed=discord.Embed(
title = "Public IP",
description = "IP: " + ip,
color = discord.Color.green()
)
return await ctx.send(embed=embed)
@bot.command(name='status', description="Host Status command")
async def systemstatus(ctx):
if int(ctx.author.id) == int(owner_id):
info={
'ram':psutil.virtual_memory().percent,
'cpu':psutil.cpu_percent(),
'uptime':time.time() - psutil.boot_time(),
'disk_usage':psutil.disk_usage("/").free
}
embed=discord.Embed(
title = "System Status",
description = "Bot Host System Status",
color = discord.Color.green()
)
if ((info['ram'] > 90) or (info['cpu'] > 90)):
embed.color = discord.Color.red()
elif ((info['ram'] > 75) or (info['cpu'] > 75)):
embed.color = discord.Color.orange()
embed.add_field(name="Uptime", value=str(round(info['uptime']/60/60,2))+" Hours", inline=True)
embed.add_field(name="Memory", value=str(info['ram'])+"%", inline=True)
embed.add_field(name="CPU", value=str(info['cpu'])+"%", inline=True)
embed.add_field(name="Storage", value=str(round(info['disk_usage']/1024/1024/1024,0))+"GB free", inline=True)
return await ctx.send(embed=embed)
# @aiocron.crontab('* * * * *')
@aiocron.crontab('00 13 * * 1-5')
@bot.command(name='spaceship', description="Compares performance of Spaceship over last two days")
async def spaceshipCheck(ctx=False):
CHECK_SPACESHIP = os.getenv("CHECK_SPACESHIP", 'False').lower() in ['true', '1']
if CHECK_SPACESHIP is True or ctx is not False:
data = json.loads(requests.get("https://newwwie.net/datasets/UNIVERSE2.json").text)
length = len(data) -1
last = data[length]["aud_price"]
last2 = data[length-1]["aud_price"]
day = str(data[length]["date"])
day2 = str(data[length-1]["date"])
change = ((float(last) - float(last2))/float(last2)) * 100
rounded = round(change,2)
message = str("Change between " + day + " and " + day2 + " is " + str(rounded) + "%")
embed=discord.Embed(
title = "Spaceship Value Change",
description = message,
color = discord.Color.green()
)
embed.add_field(name=day2, value=str(last2), inline=True)
embed.add_field(name=day, value=str(last), inline=True)
if change < 0:
embed.color = discord.Color.red()
user = await bot.fetch_user(owner_id)
await user.send(embed=embed)
# @aiocron.crontab('* * * * *')
@aiocron.crontab('00 11 * * 1-7')
@bot.command(name='covid', description="Gets current stats for Covid cases")
async def covidCheck(ctx=False):
url = "https://covid-193.p.rapidapi.com/history"
XR_COVIDKEY = os.getenv("XR_COVIDKEY")
XR_COVIDAPI = os.getenv("XR_COVIDAPI")
COUNTRY = os.getenv("COUNTRY")
date = datetime.datetime.today().strftime('%Y-%m-%d')
querystring = {"country":COUNTRY,"day":date}
headers = {
'x-rapidapi-key': XR_COVIDKEY,
'x-rapidapi-host': XR_COVIDAPI
}
if XR_COVIDKEY is True or ctx is not False:
data = json.loads(requests.request("GET", url, headers=headers, params=querystring).text)
message = data['response'][0]['cases']['new']
embed=discord.Embed(
title = "Covid Cases",
description = "New cases today: " + message,
color = discord.Color.green()
)
user = await bot.fetch_user(owner_id)
await user.send(embed=embed)
# @aiocron.crontab('* * * * *')
@aiocron.crontab('00 11 * * 1-7')
@bot.command(name='vax', description="Gets current stats for Covid cases")
async def vaccinationCheck(ctx=False):
URL = "https://covidlive.com.au/"
page = requests.get(URL)
soup = BeautifulSoup(page.content, "html.parser")
# results = soup.find(id="content")
table = soup.find("table",{"class":"VACCINATIONS-AGE-BAND"})
# table2 = soup.table["VACCINATIONS-AGE-BAND"]
# print(table)
# print(table.find_all('tr'))
results = {}
for row in table.find_all('tr')[1:]:
# print(row)
aux = row.find_all('td')
# print(aux)
results[aux[0].string] = {"first": aux[1].string, "second":aux[2].string}
embed=discord.Embed(
title = "Covid Vaccinations",
description = "Vaccinations (first/seccond dose):",
color = discord.Color.green()
)
embed.add_field(name="NSW", value=str(results["NSW"]["first"]+ "/" + results["NSW"]["second"]), inline=True)
embed.add_field(name="Australia", value=str(results["Australia"]["first"]+ "/" + results["Australia"]["second"]), inline=True)
user = await bot.fetch_user(owner_id)
await user.send(embed=embed)
# print(results["NSW"]["first"])
bot.run(os.getenv("TOKEN"))