Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.76 KB | None | 0 0
  1. import discord
  2. from discord.ext import commands
  3. from cogs.utils import checks
  4. from __main__ import set_cog, send_cmd_help, settings
  5. from .utils.dataIO import dataIO
  6. from .utils.chat_formatting import *
  7. import aiohttp
  8. import asyncio
  9. import os
  10. import json
  11.  
  12.  
  13. class ChatLevel:
  14.     """The rank you have for using chat!"""
  15.  
  16.     def __init__(self, bot):
  17.         self.bot = bot
  18.         self.file_path = "data/ChatLevel/settings.json"
  19.         self.file_path2 = "data/ChatLevel/autolevels.json"
  20.         self.file_path3 = "data/ChatLevel/check.json"
  21.         self.rankUpExp = 50
  22.         self.incrementTick = 5
  23.         self.maxRank = 100
  24.         self.weightedIncrementMultiplier = 1
  25.         self.settings = dataIO.load_json(self.file_path)
  26.         self.autolevels = dataIO.load_json(self.file_path2)
  27.         self.check = dataIO.load_json(self.file_path3)
  28.  
  29.     async def levelUp(self, message):
  30.         userId = str(message.author.id)
  31.         user = str(message.author)
  32.  
  33.         if userId not in self.settings:
  34.             self.settings[userId] = {}
  35.             self.settings[userId]["name"] = user
  36.             self.settings[userId]["rank"] = 1
  37.             self.settings[userId]["rankUp"] = 2
  38.             self.settings[userId]["exp"] = 0
  39.             self.settings[userId]["y"] = 0
  40.             self.settings[userId]["rankUpExpUUID"] = self.rankUpExp
  41.             self.settings[userId]["multiplier"] = self.weightedIncrementMultiplier
  42.             dataIO.save_json(self.file_path, self.settings)
  43.         else:
  44.             if self.settings[userId]["rank"] == self.maxRank:
  45.                 if self.settings[userId]["y"] == 0:
  46.                     await self.bot.send_message(message.channel, "You have reached the max level!")
  47.                     self.settings[userId]["y"] = 1
  48.             else:
  49.                 if (self.settings[userId]["rank"] >= 5):
  50.                     """Or you can do something like this, probably smarter"""
  51.                     """self.weightedIncrementMultiplier = self.weightedIncrementMultiplier * (self.settings[userId]["rank"] / maxRank)"""
  52.                 if (self.settings[userId]["rank"] == self.settings[userId]["rankUp"]):
  53.                     """Stuff to do at rank 2. This makes it more difficult"""
  54.                     self.settings[userId]["multiplier"] = 1.20
  55.                     self.settings[userId]["rankUpExpUUID"] = int(self.settings[userId]["rankUpExpUUID"] * self.settings[userId]["multiplier"])
  56.                     self.settings[userId]["rankUp"] += 1
  57.  
  58.             if self.settings[userId]["rank"] == self.maxRank:
  59.                 """does nothing"""
  60.             else:
  61.                 if message.content.find("!") == 0 or self.settings[userId] is "Skynet#0732":
  62.                     """does nothing"""
  63.                 else:
  64.                     self.settings[userId]["exp"] += self.incrementTick
  65.                 if self.settings[userId]["exp"] >= self.settings[userId]["rankUpExpUUID"]:
  66.                     self.settings[userId]["exp"] = 0
  67.                     self.settings[userId]["rank"] += 1
  68.                     letRank = str(int(self.settings[userId]["rank"]))
  69.                     await self.bot.send_message(message.channel, "Congratulations " + str(user) + ", you have leveled up to rank " + letRank + "!")
  70.         dataIO.save_json(self.file_path, self.settings)
  71.  
  72.     @commands.command(pass_context=True)
  73.     async def rank(self, context):
  74.         """Shows your rank"""
  75.         user = str(context.message.author)
  76.         userId = str(context.message.author.id)
  77.         if userId not in self.settings:
  78.             self.settings[userId] = {}
  79.             self.settings[userId]["name"] = user
  80.             self.settings[userId]["rank"] = 1
  81.             self.settings[userId]["rankUp"] = 2
  82.             self.settings[userId]["exp"] = 0
  83.             self.settings[userId]["rankUpExpUUID"] = self.rankUpExp
  84.             self.settings[userId]["multiplier"] = self.weightedIncrementMultiplier
  85.             dataIO.save_json(self.file_path, self.settings)
  86.         totalExp = str(self.settings[userId]["rankUpExpUUID"])
  87.         letRank = str(int(self.settings[userId]["rank"]))
  88.         letExp = str(self.settings[userId]["exp"])
  89.         await self.bot.say("{}, you are rank {} with {} out of {} EXP!".format(user, letRank, letExp, totalExp))
  90.  
  91.     @commands.group(pass_context=True, no_pm=True)
  92.     @checks.admin_or_permissions(manage_server=True)
  93.     async def autolevel(self, context):
  94.         """Config for the autolevel."""
  95.         if context.invoked_subcommand is None:
  96.             await send_cmd_help(context)
  97.        
  98.        
  99.     @autolevel.command(pass_context=True)  
  100.     async def set(self, context, role: discord.Role, level):
  101.         """Sets the autorole for a certain level
  102.        The command should be used like this
  103.        autolevel set "role" "level" """
  104.         server = context.message.server
  105.        
  106.         autolevel = {
  107.             (str(server.id) + " " + level): {
  108.                 'servername' : server.name,
  109.                 'rolename' : str(role.name),
  110.                 'level' : level,
  111.                 'role.id' : role.id,
  112.             }
  113.         }
  114.        
  115.         with open('data/ChatLevel/autolevels.json') as f:
  116.             data = json.load(f)
  117.  
  118.         data.update(autolevel)
  119.  
  120.         with open('data/ChatLevel/autolevels.json', 'w') as f:
  121.             json.dump(data, f)
  122.         await self.bot.say("The autolevel has been set! \n Please do not create a new role on the same level")
  123.         '''await self.bot.say(autolevel[(str(server.id) + " " + level)]['role.id'])'''
  124.        
  125.  
  126.     @autolevel.command(pass_context=True)
  127.     async def remove(self, context):
  128.         """Removes an autolevel"""
  129.         print("test")
  130.  
  131.     @autolevel.command(pass_context=True)
  132.     async def list(self, context):
  133.         """list the autolevels on this server"""
  134.         print("test")
  135.  
  136.        
  137.     async def roleset(self, message):  
  138.         roles = message.server.roles
  139.         userId = message.author.id
  140.         server = message.server.id
  141.         user = message.author
  142.         level = 0
  143.            
  144.         for counter in range(1, 101):
  145.             level +=1
  146.             levelS = str(level)
  147.             try:
  148.                 autolevel = self.autolevels[(server + " " + levelS)]
  149.                 checkId = str(str(userId) +" " + server + " " + levelS)
  150.                 if checkId not in self.check:
  151.                     self.check[checkId] = {}
  152.                     self.check[checkId]["check"] = 0
  153.                     dataIO.save_json(self.file_path3, self.check)
  154.                 if self.check[checkId]["check"] == 0:
  155.                     rid = autolevel['role.id']
  156.                     role = discord.utils.get(roles, id=rid)
  157.                     if self.settings[str(userId)]["rank"] >= counter:
  158.                         await self.bot.add_roles(user, role)
  159.                         self.check[checkId]["check"] = 1
  160.                         dataIO.save_json(self.file_path3, self.check)
  161.                        
  162.             except:
  163.                 #There was no autolevel if this is an exception I think
  164.                 #That, or there are two roles on the same level
  165.                 test = 1 # so that is has a command to do and not break.
  166.  
  167.  
  168. def check_folders():
  169.     if not os.path.exists("data/ChatLevel"):
  170.         print("Creating data/ChatLevel folder...")
  171.         os.makedirs("data/ChatLevel")
  172.  
  173.  
  174. def check_files():
  175.     for f in ("data/ChatLevel/settings.json", "data/ChatLevel/autolevels.json", "data/ChatLevel/check.json"):
  176.         if not dataIO.is_valid_json(f):
  177.             print("Creating default ChatLevel's ", f)
  178.             dataIO.save_json(f, {})
  179.  
  180.  
  181. def setup(bot):
  182.     check_folders()
  183.     check_files()
  184.     n = ChatLevel(bot)
  185.     bot.add_cog(n)
  186.     bot.add_listener(n.levelUp, "on_message")
  187.     bot.add_listener(n.roleset, "on_message")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement