From fd7125c87b0ce47d41e239d6d2a71bfc29384e70 Mon Sep 17 00:00:00 2001 From: Keanu Date: Fri, 15 May 2020 10:06:22 +0200 Subject: [PATCH 1/2] Added todo for #5 --- cogs/utility.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cogs/utility.py b/cogs/utility.py index a87169e..1a04580 100644 --- a/cogs/utility.py +++ b/cogs/utility.py @@ -21,6 +21,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 +82,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}"') @@ -97,12 +100,13 @@ async def _weather(self, ctx, city_name): with open("storage/weathertoken.txt") as tokenweather: weathertoken = tokenweather.readlines(1) 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,6 +131,12 @@ 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): From 2725c23dec3f3d167787cdcf85d16e0f3930ad67 Mon Sep 17 00:00:00 2001 From: keanuplayz Date: Sat, 23 May 2020 12:05:47 +0200 Subject: [PATCH 2/2] Fix KeyError (can't read token) Fixes #5 Also migrated away from dotenv. Co-authored-by: Zeehondie --- .gitignore | 2 +- cogs/utility.py | 10 ++++++---- main.py | 10 +++------- 3 files changed, 10 insertions(+), 12 deletions(-) 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 1a04580..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): @@ -97,8 +98,9 @@ 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=" + str(weathertoken) + "&q=" + str(city_name) try: @@ -140,4 +142,4 @@ async def reboot(self, ctx): # 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