Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- from discord.ext import commands
- from cogs.utils import checks
- from __main__ import set_cog, send_cmd_help, settings
- from .utils.dataIO import dataIO
- from .utils.chat_formatting import *
- import aiohttp
- import asyncio
- import os
- import json
- class ChatLevel:
- """The rank you have for using chat!"""
- def __init__(self, bot):
- self.bot = bot
- self.file_path = "data/ChatLevel/settings.json"
- self.file_path2 = "data/ChatLevel/autolevels.json"
- self.file_path3 = "data/ChatLevel/check.json"
- self.rankUpExp = 50
- self.incrementTick = 5
- self.maxRank = 100
- self.weightedIncrementMultiplier = 1
- self.settings = dataIO.load_json(self.file_path)
- self.autolevels = dataIO.load_json(self.file_path2)
- self.check = dataIO.load_json(self.file_path3)
- async def levelUp(self, message):
- userId = str(message.author.id)
- user = str(message.author)
- if userId not in self.settings:
- self.settings[userId] = {}
- self.settings[userId]["name"] = user
- self.settings[userId]["rank"] = 1
- self.settings[userId]["rankUp"] = 2
- self.settings[userId]["exp"] = 0
- self.settings[userId]["y"] = 0
- self.settings[userId]["rankUpExpUUID"] = self.rankUpExp
- self.settings[userId]["multiplier"] = self.weightedIncrementMultiplier
- dataIO.save_json(self.file_path, self.settings)
- else:
- if self.settings[userId]["rank"] == self.maxRank:
- if self.settings[userId]["y"] == 0:
- await self.bot.send_message(message.channel, "You have reached the max level!")
- self.settings[userId]["y"] = 1
- else:
- if (self.settings[userId]["rank"] >= 5):
- """Or you can do something like this, probably smarter"""
- """self.weightedIncrementMultiplier = self.weightedIncrementMultiplier * (self.settings[userId]["rank"] / maxRank)"""
- if (self.settings[userId]["rank"] == self.settings[userId]["rankUp"]):
- """Stuff to do at rank 2. This makes it more difficult"""
- self.settings[userId]["multiplier"] = 1.20
- self.settings[userId]["rankUpExpUUID"] = int(self.settings[userId]["rankUpExpUUID"] * self.settings[userId]["multiplier"])
- self.settings[userId]["rankUp"] += 1
- if self.settings[userId]["rank"] == self.maxRank:
- """does nothing"""
- else:
- if message.content.find("!") == 0 or self.settings[userId] is "Skynet#0732":
- """does nothing"""
- else:
- self.settings[userId]["exp"] += self.incrementTick
- if self.settings[userId]["exp"] >= self.settings[userId]["rankUpExpUUID"]:
- self.settings[userId]["exp"] = 0
- self.settings[userId]["rank"] += 1
- letRank = str(int(self.settings[userId]["rank"]))
- await self.bot.send_message(message.channel, "Congratulations " + str(user) + ", you have leveled up to rank " + letRank + "!")
- dataIO.save_json(self.file_path, self.settings)
- @commands.command(pass_context=True)
- async def rank(self, context):
- """Shows your rank"""
- user = str(context.message.author)
- userId = str(context.message.author.id)
- if userId not in self.settings:
- self.settings[userId] = {}
- self.settings[userId]["name"] = user
- self.settings[userId]["rank"] = 1
- self.settings[userId]["rankUp"] = 2
- self.settings[userId]["exp"] = 0
- self.settings[userId]["rankUpExpUUID"] = self.rankUpExp
- self.settings[userId]["multiplier"] = self.weightedIncrementMultiplier
- dataIO.save_json(self.file_path, self.settings)
- totalExp = str(self.settings[userId]["rankUpExpUUID"])
- letRank = str(int(self.settings[userId]["rank"]))
- letExp = str(self.settings[userId]["exp"])
- await self.bot.say("{}, you are rank {} with {} out of {} EXP!".format(user, letRank, letExp, totalExp))
- @commands.group(pass_context=True, no_pm=True)
- @checks.admin_or_permissions(manage_server=True)
- async def autolevel(self, context):
- """Config for the autolevel."""
- if context.invoked_subcommand is None:
- await send_cmd_help(context)
- @autolevel.command(pass_context=True)
- async def set(self, context, role: discord.Role, level):
- """Sets the autorole for a certain level
- The command should be used like this
- autolevel set "role" "level" """
- server = context.message.server
- autolevel = {
- (str(server.id) + " " + level): {
- 'servername' : server.name,
- 'rolename' : str(role.name),
- 'level' : level,
- 'role.id' : role.id,
- }
- }
- with open('data/ChatLevel/autolevels.json') as f:
- data = json.load(f)
- data.update(autolevel)
- with open('data/ChatLevel/autolevels.json', 'w') as f:
- json.dump(data, f)
- await self.bot.say("The autolevel has been set! \n Please do not create a new role on the same level")
- '''await self.bot.say(autolevel[(str(server.id) + " " + level)]['role.id'])'''
- @autolevel.command(pass_context=True)
- async def remove(self, context):
- """Removes an autolevel"""
- print("test")
- @autolevel.command(pass_context=True)
- async def list(self, context):
- """list the autolevels on this server"""
- print("test")
- async def roleset(self, message):
- roles = message.server.roles
- userId = message.author.id
- server = message.server.id
- user = message.author
- level = 0
- for counter in range(1, 101):
- level +=1
- levelS = str(level)
- try:
- autolevel = self.autolevels[(server + " " + levelS)]
- checkId = str(str(userId) +" " + server + " " + levelS)
- if checkId not in self.check:
- self.check[checkId] = {}
- self.check[checkId]["check"] = 0
- dataIO.save_json(self.file_path3, self.check)
- if self.check[checkId]["check"] == 0:
- rid = autolevel['role.id']
- role = discord.utils.get(roles, id=rid)
- if self.settings[str(userId)]["rank"] >= counter:
- await self.bot.add_roles(user, role)
- self.check[checkId]["check"] = 1
- dataIO.save_json(self.file_path3, self.check)
- except:
- #There was no autolevel if this is an exception I think
- #That, or there are two roles on the same level
- test = 1 # so that is has a command to do and not break.
- def check_folders():
- if not os.path.exists("data/ChatLevel"):
- print("Creating data/ChatLevel folder...")
- os.makedirs("data/ChatLevel")
- def check_files():
- for f in ("data/ChatLevel/settings.json", "data/ChatLevel/autolevels.json", "data/ChatLevel/check.json"):
- if not dataIO.is_valid_json(f):
- print("Creating default ChatLevel's ", f)
- dataIO.save_json(f, {})
- def setup(bot):
- check_folders()
- check_files()
- n = ChatLevel(bot)
- bot.add_cog(n)
- bot.add_listener(n.levelUp, "on_message")
- bot.add_listener(n.roleset, "on_message")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement