def on_member_update(before: discord.Member, after: discord.Member): """Chamado sempre que o membro é alterado.""" if before.nick != after.nick: reply = "{1.mention}: Nickname changed from **{0.nick}** to **{1.nick}**".format(before, after) yield from send_log_message(bot, after.server, reply) elif before.name != after.name: reply = "{1.mention}: Name changed from **{0.name}** to **{1.name}**".format(before, after) yield from send_log_message(bot, after.server, reply) return
def on_server_update(before: discord.Server, after: discord.Server): """Chamado sempre que o servidor é alterado""" if before.name != after.name: reply = "Server name changed from **{0.name}** to **{1.name}**".format(before, after) yield from send_log_message(bot, after, reply) elif before.region != after.region: reply = "Server region changed from {0} to {1}".format(get_region_string(before.region), get_region_string(after.region)) yield from send_log_message(bot, after, reply)
def remove_char(self, ctx, *, name): """Removes a registered character. The syntax is: /stalk removechar name""" if not ctx.message.channel.is_private: return True # This could be used to remove deleted chars so we don't need to check anything # Except if the char exists in the database... yield from self.bot.send_typing(ctx.message.channel) c = userDatabase.cursor() try: c.execute( "SELECT name, user_id, world, ABS(last_level) as level, vocation " "FROM chars WHERE name LIKE ?", (name, )) result = c.fetchone() if result is None: yield from self.bot.say( "There's no character with that name registered.") return user = get_member(self.bot, result["user_id"]) username = "******" if user is None else user.display_name c.execute("DELETE FROM chars WHERE name LIKE ?", (name, )) yield from self.bot.say( "**{0}** was removed successfully from **@{1}**.".format( result["name"], username)) if user is not None: for server in get_user_servers(self.bot, user.id): world = tracked_worlds.get(server.id, None) if world != result["world"]: continue log_msg = "{0.mention} removed **{1}** ({2} {3}) from {4.mention}.".\ format(ctx.message.author, result["name"], result["level"], result["vocation"], user) yield from send_log_message(self.bot, server, log_msg) return finally: c.close() userDatabase.commit()
def on_member_join(member: discord.Member): """Chamado toda vez que um membro entra em um servidor visível ao bot.""" log.info("{0.display_name} (ID: {0.id}) joined {0.server.name}".format(member)) if lite_mode: return server_id = member.server.id server_welcome = welcome_messages.get(server_id, "") pm = (welcome_pm+"\n"+server_welcome).format(member, bot) log_message = "{0.mention} joined.".format(member) # Atualiza o status de ghost do membro update_ghost(member) # Coloca o membro na role "visitantes" roleName = 'visitante' for role in get_role_list(member.server): if role.name.lower() == roleName: yield from bot.add_roles(member, role) log.info("{0.display_name} (ID: {0.id}) added to role {1.name}".format(member, role)) yield from send_log_message(bot, member.server, log_message) yield from bot.send_message(member, pm) yield from bot.send_message(member.server, "Ei, {0.mention}, bem vindo. E nada de cobranças aqui!".format(member))
def on_member_unban(server: discord.Server, user: discord.User): """Chamado sempre que um membro é desbanido de um servidor.""" log.warning("{1.name} (ID:{1.id}) was unbanned from {0.name}".format(server, user)) yield from send_log_message(bot, server, "**{0.name}#{0.discriminator}** was unbanned.".format(user))
def on_member_ban(member: discord.Member): """Chamado sempre que um membro é banido de um servidor.""" log.warning("{0.display_name} (ID:{0.id}) was banned from {0.server.name}".format(member)) yield from send_log_message(bot, member.server, "**{0.name}#{0.discriminator}** was banned.".format(member))
def on_member_remove(member: discord.Member): """Chamado sempre que um membro deixa o servidor ou é kickado do mesmo.""" log.info("{0.display_name} (ID:{0.id}) left or was kicked from {0.server.name}".format(member)) yield from send_log_message(bot, member.server, "**{0.name}#{0.discriminator}** left or was kicked.".format(member))
def add_account(self, ctx, *, params): """Register a character and all other visible characters to a discord user. If a character is hidden, only that character will be added. Characters in other worlds are skipped. The syntax is the following: /stalk addacc user,char""" if not ctx.message.channel.is_private: return True params = params.split(",") if len(params) != 2: yield from self.bot.say( "The correct syntax is: ``/stalk addacc username,character``") return author = ctx.message.author if author.id in mod_ids + owner_ids: author_servers = get_user_servers(self.bot, author.id) else: author_servers = get_user_admin_servers(self.bot, author.id) author_worlds = get_user_worlds(self.bot, author.id) user = get_member_by_name(self.bot, params[0], server_list=author_servers) user_servers = get_user_servers(self.bot, user.id) user_worlds = get_user_worlds(self.bot, user.id) common_worlds = list(set(author_worlds) & set(user_worlds)) yield from self.bot.send_typing(ctx.message.channel) character = yield from get_character(params[1]) if user is None: yield from self.bot.say( "I don't see any user named **{0}** in the servers you manage." .format(params[0])) return if type(character) is not dict: if character == ERROR_NETWORK: yield from self.bot.say( "I couldn't fetch the character, please try again.") elif character == ERROR_DOESNTEXIST: yield from self.bot.say("That character doesn't exists.") return c = userDatabase.cursor() try: chars = character['chars'] # If the char is hidden,we still add the searched character if len(chars) == 0: yield from self.bot.say("Character is hidden.") chars = [character] skipped = list() added = list() added_tuples = list() reassigned_tuples = list() existent = list() error = list() for char in chars: # Character not in followed server(s), skip. if char['world'] not in common_worlds: skipped.append([char["name"], char["world"]]) continue name = char["name"] # If char is the same we already looked up, no need to look him up again if character["name"] == char["name"]: char = character else: char = yield from get_character(char["name"]) if type(char) is not dict: error.append(name) continue # Skip characters scheduled for deletion if char.get("deleted", False): skipped.append([char["name"], char["world"]]) continue c.execute( "SELECT id, name,user_id FROM chars WHERE name LIKE ?", (char['name'], )) result = c.fetchone() # Char is already in database if result is not None: # Registered to different user if str(result["user_id"]) != user.id: current_user = get_member(self.bot, result["user_id"]) # Char is registered to user no longer in server if current_user is None: added.append(char) reassigned_tuples.append(( user.id, result["id"], )) continue else: yield from self.bot.say( "{0} is already assigned to {1}. We can't add any other of these " "characters.".format( char["name"], current_user.display_name)) return # Registered to current user existent.append(char) continue added.append(char) added_tuples.append(( char["name"], char["level"] * -1, char["vocation"], user.id, char["world"], )) c.execute("SELECT id from users WHERE id = ?", (user.id, )) result = c.fetchone() if result is None: c.execute("INSERT INTO users(id,name) VALUES (?,?)", ( user.id, user.display_name, )) c.executemany( "INSERT INTO chars(name,last_level,vocation,user_id, world) VALUES (?,?,?,?,?)", added_tuples) c.executemany("UPDATE chars SET user_id = ? WHERE id = ?", reassigned_tuples) reply = "" log_reply = dict().fromkeys([server.id for server in user_servers], "") if added: reply += "\nThe following characters were registered or reassigned successfully:" for char in added: char["guild"] = char.get("guild", "No guild") reply += "\n\t**{name}** ({level} {vocation}) - **{guild}**".format( **char) # Announce on server log of each server for server in user_servers: # Only announce on worlds where the character's world is tracked if tracked_worlds.get(server.id, None) == char["world"]: log_reply[ server. id] += "\n\t{name} - {level} {vocation} - **{guild}**".format( **char) if existent: reply += "\nThe following characters were already registered to this user:"******"guild"] = char.get("guild", "No guild") reply += "\n\t**{name}** ({level} {vocation}) - **{guild}**".format( **char) if skipped: reply += "\nThe following characters were skipped (not in tracked worlds or scheduled deletion):" for char, world in skipped: reply += "\n\t{0} ({1})".format(char, world) if error: reply += "\nThe following characters couldn't be fetched: " reply += ", ".join(error) yield from self.bot.say(reply) for server_id, message in log_reply.items(): if message: message = "{0.mention} registered the following characters to {1.mention}: {2}".format( author, user, message) yield from send_log_message(self.bot, self.bot.get_server(server_id), message) return finally: c.close() userDatabase.commit()
def add_char(self, ctx, *, params): """Registers a tibia character to a discord user. The syntax is: /stalk addchar user,character""" if not ctx.message.channel.is_private: return True params = params.split(",") if len(params) != 2: yield from self.bot.say( "The correct syntax is: ``/stalk addchar username,character``") return author = ctx.message.author if author.id in mod_ids + owner_ids: author_servers = get_user_servers(self.bot, author.id) else: author_servers = get_user_admin_servers(self.bot, author.id) author_worlds = get_user_worlds(self.bot, author.id) # Only search in the servers the command author is user = get_member_by_name(self.bot, params[0], server_list=author_servers) user_servers = get_user_servers(self.bot, user.id) user_worlds = get_user_worlds(self.bot, author.id) common_worlds = list(set(author_worlds) & set(user_worlds)) yield from self.bot.send_typing(ctx.message.channel) char = yield from get_character(params[1]) if user is None: yield from self.bot.say( "I don't see any user named **{0}** in the servers you manage." .format(params[0])) return if type(char) is not dict: if char == ERROR_NETWORK: yield from self.bot.say( "I couldn't fetch the character, please try again.") elif char == ERROR_DOESNTEXIST: yield from self.bot.say("That character doesn't exists.") return if char["world"] not in common_worlds: yield from self.bot.say( "**{name}** ({world}) is not in a world you can manage.". format(**char)) return if char.get("deleted", False): yield from self.bot.say( "**{name}** ({world}) is scheduled for deletion and can't be added." .format(**char)) return c = userDatabase.cursor() try: c.execute("SELECT id, name, user_id FROM chars WHERE name LIKE ?", (char['name'], )) result = c.fetchone() # Char is already in database if result is not None: # Update name if it was changed if char['name'] != params[1]: c.execute("UPDATE chars SET name = ? WHERE id = ?", ( char['name'], result["id"], )) yield from self.bot.say( "This character's name was changed from **{0}** to **{1}**" .format(params[1], char['name'])) # Registered to a different user if result["user_id"] != user.id: current_user = get_member(self.bot, result["user_id"]) # User no longer in server if current_user is None: c.execute("UPDATE chars SET user_id = ? WHERE id = ?", ( user.id, result["id"], )) yield from self.bot.say( "This character was registered to a user no longer in server. " "It was assigned to this user successfully.") # Log on relevant servers for server in user_servers: world = tracked_worlds.get(server.id, None) if world == char["world"]: log_msg = "{0.mention} registered **{1}** ({2} {3}) to {4.mention}." yield from send_log_message( self.bot, server, log_msg.format(author, char["name"], char["level"], char["vocation"], user)) else: yield from self.bot.say( "This character is already registered to **@{0}**". format(current_user.display_name)) return # Registered to current user yield from self.bot.say( "This character is already registered to this user.") return c.execute( "INSERT INTO chars (name,last_level,vocation,user_id, world) VALUES (?,?,?,?,?)", (char["name"], char["level"] * -1, char["vocation"], user.id, char["world"])) # Check if user is already registered c.execute("SELECT id from users WHERE id = ?", (user.id, )) result = c.fetchone() if result is None: c.execute("INSERT INTO users(id,name) VALUES (?,?)", ( user.id, user.display_name, )) yield from self.bot.say( "**{0}** was registered successfully to this user.".format( char['name'])) # Log on relevant servers for server in user_servers: world = tracked_worlds.get(server.id, None) if world == char["world"]: char["guild"] = char.get("guild", "No guild") log_msg = "{0.mention} registered **{1}** ({2} {3}, {4}) to {5.mention}." yield from send_log_message( self.bot, server, log_msg.format(author, char["name"], char["level"], char["vocation"], char["guild"], user)) return finally: c.close() userDatabase.commit()