Skip to content

Instantly share code, notes, and snippets.

@RainbowLegend
Created October 10, 2018 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RainbowLegend/cb8600686152c39af572b0782273a4f1 to your computer and use it in GitHub Desktop.
Save RainbowLegend/cb8600686152c39af572b0782273a4f1 to your computer and use it in GitHub Desktop.
Bot src files for Burt Khalifia
"""
Fortnite Tracker Bot - cogs.fortnite.py
~~~~~~~~~~~~~~~~~~~
A basic bot for the Burt Khalifa Discord Guild.
"""
"""
The MIT License (MIT)
Copyright (c) 2018 RainbowLegend
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
from discord.ext import commands
import discord
import data.config as config
import aiohttp
import json
import asyncio
class Fortnite:
"""Fortnite Stat Utilities"""
def __init__(self, bot):
self.bot = bot
self.guild = self.bot.get_guild(498168868271816704)
async def _check_info(self, platform, usn):
headers = {'TRN-Api-Key': config.fn_api}
async with aiohttp.ClientSession(headers=headers) as cs:
async with cs.get('https://api.fortnitetracker.com/v1/profile/{0}/{1}'.format(platform, usn)) as r:
res = await r.json()
if "Player Not Found" in res.values():
return discord.Embed(colour=0x36393E, description=f'Stats for player `{usn}` on platform '
f'`{platform}` not found.')
with open('data/fortnite.json', 'r') as f:
js = json.loads(f.read())
em = discord.Embed(colour=0x36393E)
if usn in js.keys():
em.set_author(name=f'Stats for {usn} ({platform})',
icon_url=self.guild.get_member(js[usn]).avatar_url)
else:
em.set_author(name=f'Stats for {usn} ({platform})')
stats = res['lifeTimeStats']
em.add_field(name='Wins', value=stats[8]['value'])
em.add_field(name='Top 10 Finishes', value=str(int(stats[0]['value']) +
int(stats[1]['value']) +
int(stats[2]['value']) +
int(stats[3]['value'])))
em.add_field(name='Top 25 Finishes', value=str(int(stats[4]['value']) + int(stats[5]['value'])))
em.add_field(name='Misc Stats', value=f"Matches Played: **{stats[7]['value']}**\n"
f"Win Percentage: **{stats[8]['value']}**\n"
f"Kills: **{stats[10]['value']}\n**"
f"KDR: **{stats[11]['value']}**")
return em
@commands.command()
async def add_person(self, ctx, discord: discord.User, *, fortnite_name):
"""You can associate a Discord with a Fortnite username
?add_person [discord, i.e. @MystLegend#4107] {fortnite name, i.e. xXxXFortn1t3G0dXxXx}"""
with open('data/fortnite.json', 'r+') as f:
jse = json.loads(f.read())
jse[fortnite_name] = discord.id
f.seek(0)
f.truncate()
json.dump(jse, f)
await ctx.send('Person successfully added to database.')
@commands.command()
async def stats(self, ctx, platform, *, name):
"""Check the stats of a Fortnite player.
?stats [platform: 'pc' 'ps4' or 'xbl'] {fortnite name}"""
if platform not in ['pc', 'ps4', 'xbl']:
await ctx.send('Incorrect platform specified.')
await ctx.send(embed=await self._check_info(platform, name))
def setup(bot):
bot.add_cog(Fortnite(bot))
print('FN cog loaded')
token = _YOUR_TOKEN_HERE
fn_api = _FORTNITE_TRACKER_API_KEY
prefix = '?'
"""
Fortnite Tracker Bot - main.py
~~~~~~~~~~~~~~~~~~~
A basic bot for the Burt Khalifa Discord Guild.
"""
"""
The MIT License (MIT)
Copyright (c) 2018 RainbowLegend
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
import discord
from discord.ext import commands
import sys
import traceback
from data import config
cogs = ['cogs.owner', 'cogs.fortnite']
bot = commands.Bot(description='A bot coded for the Burt Khalifa server.', command_prefix=config.prefix,
case_insensitive=True, pm_help=True)
if __name__ == '__main__':
for cog in cogs:
try:
bot.load_extension(cog)
print(f'Extension {cog} loaded')
except Exception as e:
print(f'Failed to load cog {cog}.', file=sys.stderr)
traceback.print_exc()
bot.load_extension("jishaku")
print('Waiting for on ready...')
@bot.event
async def on_ready():
"""When bot started up"""
print(f'\n\nLogged in as: {bot.user.name} - {bot.user.id}\nVersion: {discord.__version__}\n')
print(f'Successfully logged in and booted...!')
bot.run(config.token, bot=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment