Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.40 KB | None | 0 0
  1. import discord, asyncio, aiohttp, json, time, datetime, pickle, os.path, re, traceback, COC, sys, time
  2. from discord.ext import commands, tasks
  3. from tinydb import TinyDB, Query, where
  4. import logging
  5.  
  6. logger = logging.getLogger('discord')
  7. logger.setLevel(logging.ERROR)
  8. handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
  9. handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
  10. logger.addHandler(handler)
  11.  
  12. home_coc_token = """REDACTED"""
  13. clan_DB = TinyDB('clan_database.json')
  14. bot_DB = TinyDB('bot_database.json')
  15.  
  16. url = "https://api.clashofclans.com/v1/"
  17. params = None
  18. headers = {
  19.             'Accept': "application/json",
  20.             'Authorization': "Bearer " + home_coc_token
  21.         }
  22.  
  23. SCOPES = [REDACTED]
  24.  
  25. class AdminCog(commands.Cog,command_attrs=dict(hidden=False)):#name="Admin",
  26.     def __init__(self,bot):
  27.         self.bot = bot
  28.  
  29.     async def cog_check(self, ctx):
  30.         return ctx.author.id == 148319665188372481
  31.    
  32.     @commands.command()
  33.     async def calendar(self, ctx, new_cal_id : str):
  34.         """Changes calendar ID."""
  35.         id_str = Query()
  36.         bot_DB.update({'calendar_id': new_cal_id}, id_str.calendar_id)
  37.         await ctx.send('Changed calendar id.')
  38.  
  39.     @commands.command()
  40.     async def reload(self, ctx, extension_name : str):
  41.         """Reloads an extension."""
  42.         try:
  43.             self.bot.unload_extension(extension_name)
  44.             self.bot.load_extension(extension_name)
  45.         except (AttributeError, ImportError) as e:
  46.             await ctx.message.channel.send("```py\n{}: {}\n```".format(type(e).__name__, str(e)))
  47.             return
  48.         await ctx.message.channel.send("{} loaded.".format(extension_name))
  49.  
  50.     @commands.command()
  51.     async def add_extension(self, ctx, extension_name : str):
  52.         """Adds an extension to be automatically loaded at bot start."""
  53.         record = bot_DB.search(where('server_id') == 321963490111913987)
  54.         extension_list = record[0]['extensions']
  55.         extension_list.append(extension_name)
  56.         bot_DB.update({'extensions': extension_list}, where('server_id') == 321963490111913987)
  57.         await ctx.message.channel.send(f"{extension_name} added to list.")
  58.  
  59.     @commands.command()
  60.     async def remove_extension(self, ctx, extension_name : str = None):
  61.         """Removes an extension to be automatically loaded at bot start."""
  62.         record = bot_DB.search(where('server_id') == 321963490111913987)
  63.         extension_list = record[0]['extensions']
  64.         if extension_name == None:  await ctx.message.channel.send(f'The current extensions are: {extension_list}')
  65.         else:
  66.             try:
  67.                 extension_list.remove(extension_name)
  68.                 bot_DB.update({'extensions': extension_list}, where('server_id') == 321963490111913987)
  69.                 await ctx.message.channel.send(f"{extension_name} removed from list.")
  70.             except:
  71.                 await  ctx.message.channel.send(f'There was an error, the current extensions are: {extension_list}')
  72.  
  73.     @commands.command() #adds a new clan to the clan_db database
  74.     async def addclan(self, ctx, clantag, channel: discord.TextChannel, role: discord.Role):
  75.         clan_url = f'{url}clans/%23{clantag}'
  76.         async with aiohttp.ClientSession() as session:
  77.             async with session.get(clan_url, params = params, headers=headers) as r:
  78.                 if r.status == 200:
  79.                     resp = await r.json()
  80.                     clan_DB.upsert({'name': resp['name'],
  81.                                     'clantag': resp['tag'],
  82.                                     'channel': channel.id,
  83.                                     'role': role.id,
  84.                                     'current_war': {'startTime': None,
  85.                                                     'endTime': None,
  86.                                                     'members': None},
  87.                                     'past_war': {'startTime': None,
  88.                                                 'endTime': None,
  89.                                                 'members': None}},
  90.                                                 )
  91.                     await ctx.channel.send(f'Successfully added {resp["name"]} to the database!')
  92.                 else: await ctx.channel.send('There was an error, check log for details.')
  93.  
  94. def setup(bot):
  95.     bot.add_cog(AdminCog(bot))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement