|
| 1 | +import json |
| 2 | +import time |
| 3 | +import asyncio |
| 4 | + |
| 5 | +import discord |
| 6 | +from discord.ext import commands |
| 7 | +from sqlalchemy import create_engine |
| 8 | +from sqlalchemy.orm import sessionmaker |
| 9 | + |
| 10 | +from bot.cogs.utils.constants import * |
| 11 | +from bot.cogs.utils.logs import AttributionLogs, Base, log_attribution |
| 12 | +from bot.web import * |
| 13 | + |
| 14 | +# sqlalchemy setup |
| 15 | +engine = create_engine("sqlite:///bot/data/daily_logs.db") |
| 16 | + |
| 17 | +Base.metadata.bind = engine |
| 18 | + |
| 19 | +DBSession = sessionmaker(bind=engine) |
| 20 | + |
| 21 | +session = DBSession() |
| 22 | + |
| 23 | +class BeltsAttributions(commands.Cog): |
| 24 | + """This is a class to handle the discord attribution of belt roles.""" |
| 25 | + |
| 26 | + def __init__(self, client: commands.Bot): |
| 27 | + self.client = client |
| 28 | + |
| 29 | + @commands.command(name="promove") |
| 30 | + @commands.has_any_role( |
| 31 | + Roles["ADMIN"].name, Roles["CHAMPION"].name |
| 32 | + ) |
| 33 | + async def promove( |
| 34 | + self, ctx: discord.ext.commands.Context, user: str, belt: str |
| 35 | + ) -> None: |
| 36 | + """This function promotes a user to the next belt.""" |
| 37 | + |
| 38 | + mentions = ctx.message.raw_mentions |
| 39 | + guild = ctx.guild |
| 40 | + member = guild.get_member(mentions[0]) |
| 41 | + ninja = Ninja(guild, member) |
| 42 | + |
| 43 | + if belt == "Branco" and ninja.current_belt() == None: |
| 44 | + role = get_role_from_name(guild, belt) |
| 45 | + |
| 46 | + await member.add_roles(guild.get_role(role.id), reason=None, atomic=True) |
| 47 | + |
| 48 | + # send request to update ninja belt |
| 49 | + ninja_username = member.name + '#' + member.discriminator |
| 50 | + await update_belt(ninja_username, belt) |
| 51 | + |
| 52 | + # Public message |
| 53 | + asyncio.create_task(ctx.send(f"{user} agora és cinturão {belt} :tada:")) |
| 54 | + |
| 55 | + # Private message |
| 56 | + asyncio.create_task(self.send_private_message(member, belt)) |
| 57 | + |
| 58 | + # Adding the log to the database |
| 59 | + asyncio.create_task(self.log(ctx, member, belt)) |
| 60 | + |
| 61 | + elif belt == ninja.current_belt().name: |
| 62 | + await ctx.reply(f"Esse já é o cinturão do ninja {user}!") |
| 63 | + |
| 64 | + elif belt == ninja.next_belt().name: |
| 65 | + role = get_role_from_name(guild, belt) |
| 66 | + |
| 67 | + await member.add_roles(guild.get_role(role.id), reason=None, atomic=True) |
| 68 | + |
| 69 | + # send request to update ninja belt |
| 70 | + ninja_username = member.name + '#' + member.discriminator |
| 71 | + await update_belt(ninja_username, belt) |
| 72 | + |
| 73 | + # Public message |
| 74 | + await ctx.send(f"{user} agora és cinturão {belt} :tada:") |
| 75 | + |
| 76 | + # Private message |
| 77 | + asyncio.create_task(self.send_private_message(member, belt)) |
| 78 | + |
| 79 | + # Adding the log to the database |
| 80 | + asyncio.create_task(self.log(ctx, member, belt)) |
| 81 | + |
| 82 | + elif belt != ninja.next_belt().name: |
| 83 | + await ctx.send(f"{user} esse cinturão não é valido de se ser atribuido.") |
| 84 | + |
| 85 | + |
| 86 | + async def log(self, ctx, member, belt): |
| 87 | + """This function logs the belt attribution.""" |
| 88 | + |
| 89 | + new_log = AttributionLogs( |
| 90 | + ninja_id=str(member), |
| 91 | + mentor_id=str(ctx.author), |
| 92 | + belt_attributed=belt, |
| 93 | + timestamp=int(time.time()), |
| 94 | + ) |
| 95 | + |
| 96 | + session.add(new_log) |
| 97 | + session.commit() |
| 98 | + |
| 99 | + async def send_private_message(self, member, belt): |
| 100 | + """This function sends a private message to the member.""" |
| 101 | + |
| 102 | + file_handler = FileHandler(belt) |
| 103 | + emoji = translator_to_emoji[belt] |
| 104 | + user = member |
| 105 | + embed = discord.Embed( |
| 106 | + title=f"{emoji} Parabéns, subiste de cinturão :tada:", |
| 107 | + description=file_handler.msg, |
| 108 | + color=file_handler.color, |
| 109 | + ) |
| 110 | + |
| 111 | + await user.send(embed=embed) |
| 112 | + |
| 113 | +class FileHandler: |
| 114 | + """ |
| 115 | + This is a class to handle a json file. |
| 116 | + |
| 117 | + Attributes: |
| 118 | + file (string): The path to the json file being handled. |
| 119 | + """ |
| 120 | + |
| 121 | + file = "bot/data/belts.json" |
| 122 | + |
| 123 | + def __init__(self: str, belt: str): |
| 124 | + """ |
| 125 | + The constructor for the FileHandler class. |
| 126 | + |
| 127 | + Parameters: |
| 128 | + color (int): Color code to be displayed in discord embed. |
| 129 | + """ |
| 130 | + self.belt = belt |
| 131 | + self.msg = self.get_info()[0] |
| 132 | + self.color = self.get_info()[1] |
| 133 | + |
| 134 | + def get_info(self) -> tuple: |
| 135 | + """ |
| 136 | + The function to get the info from the belts.json file. |
| 137 | + |
| 138 | + Returns: |
| 139 | + msg (string): Variable that contains the message of the respective belt. |
| 140 | + color (int): Color code to be displayed in discord embed. |
| 141 | + """ |
| 142 | + with open(self.file) as json_file: |
| 143 | + data = json.load(json_file) |
| 144 | + msg = f"Subiste para {self.belt} :clap:\n\nPróximos objetivos:" |
| 145 | + color = int(data[self.belt]["color"], 16) |
| 146 | + for param in data[self.belt]["goals"]: |
| 147 | + msg += "\n" + param |
| 148 | + |
| 149 | + return (msg, color) |
| 150 | + |
| 151 | +class Ninja: |
| 152 | + """This is a class to get information about a specific ninja.""" |
| 153 | + |
| 154 | + def __init__(self, guild: discord.Guild, member: discord.Member): |
| 155 | + self.guild = guild |
| 156 | + self.member = member |
| 157 | + self.roles = list(member.roles) |
| 158 | + |
| 159 | + def current_belt(self): |
| 160 | + """This function returns the current belt of the ninja.""" |
| 161 | + |
| 162 | + highest_belt = None |
| 163 | + for role in self.roles: |
| 164 | + for belt in Belts: |
| 165 | + if belt.name == role.name: |
| 166 | + highest_belt = belt |
| 167 | + |
| 168 | + print(highest_belt) |
| 169 | + return highest_belt |
| 170 | + |
| 171 | + def next_belt(self) -> Belts: |
| 172 | + """This function returns the next belt of the ninja.""" |
| 173 | + |
| 174 | + if self.current_belt() == None: |
| 175 | + value = 0 |
| 176 | + else if self.current_belt().value < 8: |
| 177 | + value = self.current_belt().value + 1 |
| 178 | + |
| 179 | + else: |
| 180 | + value = 8 |
| 181 | + |
| 182 | + return Belts(value) |
| 183 | + |
| 184 | + |
| 185 | +def setup(client: commands.Bot) -> None: |
| 186 | + client.add_cog(BeltsAttributions(client)) |
0 commit comments