async def give(self, ctx, *args): giver = ctx.author member = await self.get.get_member(ctx, args[0]) amount = int(args[1]) if self.get.get_user(member.id) is None: return if member is None: return await self.error.get_error(ctx, f"User **{args[0]}** not found", "give") if amount < 1: return await self.error.get_error(ctx, f"You can not transfer less than 1 coin to someone!", "give") if amount > self.get.get_coins(giver.id): return await self.error.get_error(ctx, f"You can not transfer more coins than you own!", "give") try: with db.cursor() as cursor: sql = f"UPDATE players SET coins=%s WHERE user_id=%s" cursor.execute(sql, (self.get.get_coins(member.id) + amount, member.id)) sql = f"UPDATE players SET coins=%s WHERE user_id=%s" cursor.execute(sql, (self.get.get_coins(giver.id)-amount, giver.id)) db.commit() embed = discord.Embed(title=f"Successfully transferred {amount} coins to **{member.name}**", color=discord.Color.from_rgb(130, 234, 255)) await ctx.send(embed=embed) except Exception as e: print(f"Error transferring {amount} coins to {member.name} [{member.id}] from {giver.name} [{giver.id}])\n{e}") return await self.error.get_error(ctx, f"**{member.name}** is not registered!", "addcoins")
def get_message(self, nebula): try: with db.cursor() as cursor: sql = "SELECT message FROM `nebulas` WHERE `nebula`=%s" cursor.execute(sql, (nebula,)) result = cursor.fetchone() message = result["message"] return message except Exception as e: print(f"Error looking up message of: {nebula}\n{e}")
def get_coins(self, user_id): try: with db.cursor() as cursor: sql = "SELECT coins FROM `players` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() coins = result["coins"] return coins except Exception as e: print(f"Error looking up coins of: {user_id}\n{e}")
def get_evaluation(self, user_id): try: with db.cursor() as cursor: sql = "SELECT overall_evaluation FROM `players` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() evaluation = result["overall_evaluation"] return evaluation except Exception as e: print(f"Error looking up coins of: {user_id}\n{e}")
def get_prefix(self, ctx): try: with db.cursor() as cursor: sql = "SELECT `prefix` FROM `prefixes` WHERE `guild_id`=%s" cursor.execute(sql, (ctx.guild.id, )) prefix = cursor.fetchone() current_prefix = prefix['prefix'] return current_prefix except Exception as e: print(f'Error looking up prefix 2: {e}')
def get_level(self, user_id): try: with db.cursor() as cursor: sql = "SELECT level, exp_points FROM `general_skills` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() level = f'{result["level"]}, [{result["exp_points"]}/2680 EXP]' return level except Exception as e: print(f"Error looking up coins of: {user_id}\n{e}")
def get_nebula(self, nebula): try: with db.cursor() as cursor: sql = "SELECT `nebula` FROM `nebulas` WHERE `nebula`=%s" cursor.execute(sql, (nebula,)) result = cursor.fetchone() if not result: print(f"Nebula does not exist: {nebula} 1") else: return result except Exception as e: print(f"Error looking up nebula {nebula}\n{e}")
def get_user(self, user_id): try: with db.cursor() as cursor: sql = "SELECT `user_id` FROM `players` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() if not result: print(f"User does not exist: {user_id}") else: return result except Exception as e: print(f"Error looking up userid {user_id}\n{e}")
def sort_members(self, nebula): try: with db.cursor() as cursor: sql = "SELECT member_ids, nebula_rank, contribution, nebula_ranking FROM `nebula_members` WHERE `nebula`=%s" cursor.execute(sql, (nebula,)) data = cursor.fetchone() db.commit() except Exception as e: print(f"Error sorting nebula {nebula}\n{e}") data = list(data.values()) members, ranks, contribution, ranking = data[0].split(", "), data[1].split(", "), data[2].split(", "), data[3].split(", ") sorted_members, sorted_contribution, sorted_ranks, sorted_rankings = "", "", "", "" for i in range(len(members)): idx = i for j in range(i + 1, len(members)): if int(contribution[idx]) < int(contribution[j]): idx = j members[i], members[idx], contribution[i], contribution[idx] = members[idx], members[i], contribution[idx], contribution[i] ranks[i], ranks[idx], ranking[i], ranking[idx] = ranks[idx], ranks[i], ranking[idx], ranking[i] sorted_members += f"{members[i]}" sorted_contribution += f"{contribution[i]}" sorted_ranks += f"{ranks[i]}" sorted_rankings += f"{i + 1}" if i != len(members)-1: sorted_members += ", " sorted_contribution += ", " sorted_ranks += ", " sorted_rankings += ", " try: with db.cursor() as cursor: sql = f"UPDATE nebula_members SET member_ids=%s, nebula_rank=%s, nebula_ranking=%s, contribution=%s WHERE nebula=%s" cursor.execute(sql, (sorted_members, sorted_ranks, sorted_rankings, sorted_contribution, nebula)) db.commit() except Exception as e: print(f"Error sorting nebula members\n{e}")
def get_general_skills(self, user_id): try: with db.cursor() as cursor: sql = "SELECT stamina, strength, agility, magic FROM `general_skills` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() general_skills = list(result.values()) if not result: print(f"User does not exist: {user_id}") else: return general_skills except Exception as e: print(f"Error looking up userid {user_id}\n{e}")
def get_nebula(self, user_id): try: with db.cursor() as cursor: sql = "SELECT nebula FROM `players` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() nebula = result["nebula"] if not result: print(f"User is not in a nebula: {user_id}") else: return nebula except Exception as e: print(f"Error looking up userid {user_id}\n{e}")
def get_members(self, member): nebula = self.get.get_nebula(member.id) self.sort_members(nebula) try: with db.cursor() as cursor: sql = "SELECT member_ids, nebula_rank, contribution, nebula_ranking FROM `nebula_members` WHERE `nebula`=%s" cursor.execute(sql, (nebula,)) result = cursor.fetchone() if not result: print(f"Nebula does not exist: {nebula} 2") else: return result except Exception as e: print(f"Error looking up nebula {nebula}\n{e}")
async def on_message(self, message): if not message.guild: return if self.bot.user.mentioned_in(message): try: with db.cursor() as cursor: sql = "SELECT `prefix` FROM `prefixes` WHERE `guild_id`=%s" cursor.execute(sql, (message.guild.id, )) prefix = cursor.fetchone() current_prefix = prefix['prefix'] await message.channel.send( f"This server's prefix is `{current_prefix}` type `{current_prefix}prefix <new prefix here>` if you wish to change it" ) except Exception as e: print(f'Error looking up prefix 3: {e}')
async def remove_user_from_db(self, ctx, member): member = await self.get.get_member(ctx, member) if member is None: return await self.error.get_error(ctx, f"user {member} not found") if member.id == 122837007834677251: return await ctx.send("no.") try: with db.cursor() as cursor: sql = f"DELETE players, general_skills, personal_skills, personal_attributes FROM (((players INNER JOIN general_skills ON players.user_id = general_skills.user_id) INNER JOIN personal_attributes ON players.user_id=personal_attributes.user_id) INNER JOIN personal_skills ON players.user_id=personal_skills.user_id) WHERE players.user_id = %s" cursor.execute(sql, (member.id)) db.commit() embed = discord.Embed(title=f"[Successfully removed {member.name} [{member.id}] from the database", color=discord.Color.from_rgb(130, 234, 255)) await ctx.send(embed=embed) except Exception as e: print(f"Error removing {member.name} [{member.id}] from the database\n{e}") return await self.error.get_error(ctx, f"{member.name} is not registered!", "remu")
def get_attributes(self, user_id): try: with db.cursor() as cursor: sql = "SELECT attribute, attribute_rating FROM `personal_attributes` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() attribute = result["attribute"].split(", ") attribute_rating = result["attribute_rating"].split(", ") temp = attribute attribute = "" for i in range(0, len(temp)): if i % 2 == 1: attribute += f'{temp[i]} ({attribute_rating[i]}),\n' else: attribute += f'{temp[i]} ({attribute_rating[i]}), ' return attribute except Exception as e: print(f"Error looking up attributes of: {user_id}\n{e}")
def get_skills(self, user_id): try: with db.cursor() as cursor: sql = "SELECT personal_skills, skill_level FROM `personal_skills` WHERE `user_id`=%s" cursor.execute(sql, (user_id, )) result = cursor.fetchone() personal_skills = result["personal_skills"].split(", ") skill_level = result["skill_level"].split(", ") temp = personal_skills personal_skills = "" for i in range(0, len(temp)): if i % 2 == 1: personal_skills += f"[{temp[i]} Lv.{skill_level[i]}],\n" else: personal_skills += f"[{temp[i]} Lv.{skill_level[i]}], " return personal_skills except Exception as e: print(f"Error looking up attributes of: {user_id}\n{e}")
async def remove_nebula_from_db(self, ctx, inp): nebula = inp if self.nebula.new_nebula(inp) == True: nebula = None try: with db.cursor() as cursor: sql = f"DELETE nebulas, nebula_members FROM nebulas INNER JOIN nebula_members ON nebulas.nebula = nebula_members.nebula WHERE nebulas.nebula = %s" cursor.execute(sql, (nebula)) sql = f"UPDATE players SET nebula=Null WHERE user_id=%s" cursor.execute(sql, (ctx.author.id)) db.commit() embed = discord.Embed(title=f"[Successfully removed nebula [{inp}] from the database", color=discord.Color.from_rgb(130, 234, 255)) await ctx.send(embed=embed) except Exception as e: print(f"Error removing nebula [{inp}] from the database\n{e}") return await self.error.get_error(ctx, f"nebula [{inp}] does not exist!", "remneb")
async def give_gold(self, ctx, *args): if (len(args)) >= 2: member = await self.get.get_member(ctx, args[0]) amount = int(args[1]) if member is None: return await self.error.get_error(ctx, f"user {args[0]} not found", "addcoins") else: member = ctx.author amount = int(args[0]) try: with db.cursor() as cursor: sql = f"UPDATE players SET coins=%s WHERE user_id=%s" cursor.execute(sql, (self.get.get_coins(member.id) + amount, member.id)) db.commit() embed = discord.Embed(title=f"[{self.get.get_user_type(member.id)} **{member.name}** you have obtained __{amount}__ coins!]", color=discord.Color.from_rgb(130, 234, 255)) await ctx.send(embed=embed) except Exception as e: print(f"Error updating coins ({amount} to {member.id} from {ctx.author.name} [{ctx.author.id}])\n{e}") return await self.error.get_error(ctx, f"{member.name} is not registered!", "addcoins")
async def create(self, ctx, *, nebula: str = None): if self.get.get_user(ctx.author.id) is None: return await self.error.not_registered_error(ctx, "nebula") in_nebula = self.get.get_nebula(ctx.author.id) if in_nebula is not None: return await self.error.get_error(ctx, f"{self.get.get_user_type(ctx.author.id)} **{ctx.author.name}**, you are already in a nebula!", "nebula create") if nebula is None: return await self.error.get_error(ctx, "You must enter a name for your nebula", "nebula create") ' '.join(nebula.split()) new_nebula = self.new_nebula(nebula) try: with db.cursor() as cursor: sql = "SELECT `guild_id` FROM `nebulas` WHERE `guild_id`=%s" cursor.execute(sql, (ctx.guild.id,)) result = cursor.fetchone() if not result: guild_has_nebula = False else: guild_has_nebula = True except Exception as e: print(f"Error looking up nebula's in guild {ctx.guild.name}\n{e}") if guild_has_nebula: return await self.error.get_error(ctx, "This guild already has a nebula", "nebula create") if new_nebula is False: return await self.error.get_error(ctx,f"The nebula \"{nebula}\" is already taken", "nebula create") embed = discord.Embed(title="Confirmation", description=f"{self.get.get_user_type(ctx.author.id)}, are you sure you want to create the **{nebula}** nebula for __250,000__ coins?", color=discord.Color.from_rgb(130, 234, 255)) msg = await ctx.send(embed=embed) await msg.add_reaction("✅") await asyncio.sleep(.35) await msg.add_reaction("❌") def check(reaction, user): return ctx.author == user and str(reaction.emoji) in ["✅", "❌"] try: reaction, user = await self.bot.wait_for('reaction_add', timeout=5.0, check=check) except asyncio.TimeoutError: await msg.delete(delay=0) msg = await ctx.send(f"{ctx.author.mention}, you waited too long") await msg.delete(delay=3) else: if str(reaction.emoji) == "✅": coin = self.get.get_coins(ctx.author.id) if coin < 250000: await msg.delete(delay=0) return await self.error.get_error(ctx, f"{self.get.get_user_type(ctx.author.id)}, you do not have enough coins to fund a nebula! You currently have __{format(coin, ',d')}__ coins. Nebulas require __250,000__ coins to fund", "nebula create") try: with db.cursor() as cursor: sql = "INSERT INTO `nebulas` (nebula, guild_id, message, leader, vice_leader) VALUES (%s, %s, %s, %s, %s)" cursor.execute(sql, (nebula, ctx.guild.id, None, ctx.author.id, None,)) sql = "INSERT INTO `nebula_members` (nebula, member_ids, nebula_rank, nebula_ranking, contribution) VALUES (%s, %s, %s, %s, %s)" cursor.execute(sql, (nebula, ctx.author.id, 3, 1, 0)) sql = f"UPDATE players SET nebula=%s, coins=%s WHERE user_id=%s" cursor.execute(sql, (nebula, self.get.get_coins(ctx.author.id)-250000, ctx.author.id)) db.commit() embed = discord.Embed(title="Congratulations 🎉", description=f"{self.get.get_user_type(ctx.author.id)} {ctx.author.mention}, you are responsible for the birth of a nebula. You may now challenge the scenarios with other incarnations and constellations as a nebula.", color=discord.Color.from_rgb(130, 234, 255)) embed.add_field(name="\u200b", value=f"*do `{self.get.get_prefix(ctx)}help nebula` for information on commands regarding nebulas*", inline=False) await msg.delete(delay=0) await ctx.send(embed=embed) except Exception as e: print(f"Error adding Nebula: {e}") elif str(reaction.emoji) == "❌": await msg.delete(delay=0)