Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,5 @@ dmypy.json
todo.txt
.vscode/launch.json
storage/prefixes.json
storage/weathertoken.txt
.vscode/settings.json
storage/tokens.json
22 changes: 17 additions & 5 deletions cogs/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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 <url>"""
import gdshortener
s = gdshortener.ISGDShortener()
await ctx.send(s.shorten(f'{url}'))
Expand Down Expand Up @@ -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 <name>"""
channel = ctx.author.voice.channel
await channel.edit(name=cname)
await ctx.send(f'Changed channel name to "{cname}"')
Expand All @@ -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 <location>"""
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"]
Expand All @@ -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))
client.add_cog(Utility(client)) # ga naar weather
10 changes: 3 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)