diff --git a/.gitignore b/.gitignore index c0f0eb9..2d1a429 100644 --- a/.gitignore +++ b/.gitignore @@ -131,5 +131,5 @@ dmypy.json todo.txt .vscode/launch.json storage/prefixes.json -storage/weathertoken.txt .vscode/settings.json +storage/tokens.json \ No newline at end of file diff --git a/cogs/utility.py b/cogs/utility.py index a87169e..32ec430 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -4,7 +4,8 @@ from discord.ext import commands import math import requests -import dotenv +import dotenv +import json # Cog class: class Utility(commands.Cog): @@ -21,6 +22,7 @@ def __init__(self, client): # This is a command: @commands.command() async def shorten(self, ctx, url): + """Shortens a URL. Usage: ?shorten """ import gdshortener s = gdshortener.ISGDShortener() await ctx.send(s.shorten(f'{url}')) @@ -81,11 +83,13 @@ async def purge(self, ctx, amount=0): # Code command @commands.command() async def code(self, ctx): + """Sends bot repo link.""" await ctx.send("https://github.com/TKLprojects/travbot.py") # Desc command @commands.command() async def desc(self, ctx, *, cname): + """Changes voice channel name. Usage: ?desc """ channel = ctx.author.voice.channel await channel.edit(name=cname) await ctx.send(f'Changed channel name to "{cname}"') @@ -94,15 +98,17 @@ async def desc(self, ctx, *, cname): @commands.command(name="weather") async def _weather(self, ctx, city_name): """Get weather information about a location. Usage: ?weather """ - with open("storage/weathertoken.txt") as tokenweather: - weathertoken = tokenweather.readlines(1) + with open("storage/tokens.json") as tokensfile: + tokenfile = json.load(tokensfile) + weathertoken = tokenfile['weather'] base_url = "http://api.weatherapi.com/v1/current.json?" - complete_url = base_url + "key=f44ceda5450e4c16be585450200805" + "&q=" + str(city_name) + complete_url = base_url + "key=" + str(weathertoken) + "&q=" + str(city_name) try: response = requests.get(complete_url) except BaseException: await ctx.send("no") res = response.json() + # TODO #5 Fix KeyError (can't read token) weather = res["current"] location = res["location"] condition = weather["condition"] @@ -127,7 +133,13 @@ async def _weather(self, ctx, city_name): weatherembed.add_field(name="Time:", value=weather_timezone) weatherembed.set_thumbnail(url=image) await ctx.send(embed=weatherembed) + @commands.command() + async def reboot(self, ctx): + """Reboots the bot, if you run via pm2.""" + await ctx.send("Rebooting...") + exit() + # This always needs to be at the end of a cog file: def setup(client): - client.add_cog(Utility(client)) \ No newline at end of file + client.add_cog(Utility(client)) # ga naar weather \ No newline at end of file diff --git a/main.py b/main.py index a3c598a..fbe6c2e 100644 --- a/main.py +++ b/main.py @@ -14,13 +14,6 @@ handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s')) logger.addHandler(handler) -# Dotenv -import dotenv -from dotenv import load_dotenv -load_dotenv() -token = os.getenv('DISCORD_TOKEN') -weathertoken = os.getenv('WEATHER_TOKEN') - # Server specific prefixes def get_prefix(client, message): with open('./storage/prefixes.json', 'r') as f: @@ -82,5 +75,8 @@ async def cprefix(ctx, prefix): json.dump(prefixes, f, indent=4) await ctx.send(f'Prefix changed to: {prefix}') +with open("storage/tokens.json") as tokensfile: + tokenfile = json.load(tokensfile) + token = tokenfile['bot'] client.run(token) \ No newline at end of file