def to_msg(self, show_assignees=True): if show_assignees: key = "result_with_assignees" else: key = "result_without_assignees" return Lang.lang(self.plugin, key, utils.get_best_username(self.assigned.user), self.chosen, utils.get_best_username(self.user))
async def waitforreact_callback(event): msg = "PANIC!" if isinstance(event, ReactionAddedEvent): msg = "{}: You reacted on '{}' with {}!".format( utils.get_best_username(event.user), event.message.content, event.emoji) if isinstance(event, ReactionRemovedEvent): msg = "{}: You took back your {} reaction on '{}'!".format( utils.get_best_username(event.user), event.message.content, event.emoji) await event.channel.send(msg)
async def disable(self, ctx, command, *args): user = ctx.author date_args_start_index = 0 if len(args) > 0: try: user = await commands.MemberConverter().convert(ctx, args[0]) date_args_start_index = 1 except (commands.CommandError, IndexError): date_args_start_index = 0 if user != ctx.author and not permChecks.check_full_access(ctx.author): raise commands.MissingAnyRole(Config().FULL_ACCESS_ROLES) until = utils.analyze_time_input(*args[date_args_start_index:]) result = self.bot.ignoring.add_user_command(user, command, until) if result == IgnoreEditResult.Success: await ctx.message.add_reaction(Lang.CMDSUCCESS) elif result == IgnoreEditResult.Already_in_list: await ctx.message.add_reaction(Lang.CMDERROR) await ctx.send(Lang.lang(self, 'user_cmd_already_blocked', command, utils.get_best_username(user))) elif result == IgnoreEditResult.Until_in_past: await ctx.message.add_reaction(Lang.CMDERROR) await ctx.send(Lang.lang(self, 'no_time_machine')) await utils.log_to_admin_channel(ctx)
async def dm_callback(self, cb, message): self.plugin.logger.debug("Incoming message from {}: {}".format( self.user, message.content)) if message.content.strip() == "": # you never know return if self.plugin.statemachine.state != State.COLLECT: await self.send(Lang.lang(self.plugin, "entry_too_late")) return first = True if self.chosen: first = False self.chosen = message.content if first: await self.send( Lang.lang(self.plugin, "entry_done", utils.get_best_username(self.assigned.user), self.chosen)) else: await self.send(Lang.lang(self.plugin, "entry_change", self.chosen)) self.plugin.assigned()
async def enable_user(self, ctx, user: discord.Member): result = self.bot.ignoring.remove_user(user) if result == IgnoreEditResult.Success: await ctx.message.add_reaction(Lang.CMDSUCCESS) elif result == IgnoreEditResult.Not_in_list: await ctx.message.add_reaction(Lang.CMDERROR) await ctx.send(Lang.lang(self, 'user_not_blocked', utils.get_best_username(user))) await utils.log_to_admin_channel(ctx)
async def get_ran_formatted_text(self, bot, msg: discord.Message, cmd_args: list): """ Formats and replaces the wildcard of a random text of the cmd for using it as custom cmd. If a mentioned user has the command on its ignore list, a UserBlockedCommand error will be raised. :param bot: The bot :param msg: The original message :param cmd_args: The used command arguments :returns: The formatted command text """ cmd_content = self.get_ran_raw_text() # general replaces cmd_content = cmd_content.replace(wildcard_umention, msg.author.mention) cmd_content = cmd_content.replace(wildcard_user, utils.get_best_username(msg.author)) if wildcard_all_args in cmd_content: cmd_content = cmd_content.replace(wildcard_all_args, _get_all_arg_str(0, cmd_args)) all_args_positions = arg_list_re.findall(cmd_content) # if only one argument in cmd text and no arg given: mention the user if len(all_args_positions) == 1 and len(cmd_args) == 0: cmd_args = [(msg.author.mention, "")] # numbered arguments for i in range(0, len(all_args_positions)): if i >= len(cmd_args): break # Replace args wildcard = all_args_positions[i][0] arg_num = int(all_args_positions[i][1]) - 1 arg = cmd_args[arg_num][1] if cmd_args[arg_num][1] else cmd_args[ arg_num][0] # [1] = args inside "" # All following args if all_args_positions[i][2]: arg += " " + _get_all_arg_str(i + 1, cmd_args) # Ignoring, passive user command blocking try: member = await converter.convert_member(bot, msg, arg) except commands.BadArgument: member = None if member is not None and bot.ignoring.check_user_command( member, self.name): raise UserBlockedCommand(member, self.name) cmd_content = cmd_content.replace(wildcard, arg) return cmd_content
async def cmd_raw(self, ctx, cmd_name): cmd_name = cmd_name.lower() if cmd_name in self.commands: creator = self.bot.get_user(self.commands[cmd_name].creator_id) for msg in utils.paginate(self.commands[cmd_name].get_raw_texts(), delimiter="\n", prefix=Lang.lang( self, 'raw_prefix', self.prefix, cmd_name, utils.get_best_username(creator))): await ctx.send(msg) else: await ctx.send(Lang.lang(self, "raw_doesnt_exists", cmd_name))
async def disable_user(self, ctx, user: discord.Member, *args): until = utils.analyze_time_input(*args) result = self.bot.ignoring.add_user(user, until) if result == IgnoreEditResult.Success: await ctx.message.add_reaction(Lang.CMDSUCCESS) elif result == IgnoreEditResult.Already_in_list: await ctx.message.add_reaction(Lang.CMDERROR) await ctx.send(Lang.lang(self, 'user_already_blocked', utils.get_best_username(user))) elif result == IgnoreEditResult.Until_in_past: await ctx.message.add_reaction(Lang.CMDERROR) await ctx.send(Lang.lang(self, 'no_time_machine')) await utils.log_to_admin_channel(ctx)
async def role(self, ctx, user: discord.Member, action, role: discord.Role): if not permChecks.check_full_access(ctx.author): if role.id not in self.rc(): raise commands.CheckFailure( message=Lang.lang(self, 'role_user_not_configured')) need_mod_role_id = self.rc()[role.id]['modrole'] if need_mod_role_id is None or need_mod_role_id == 0: raise commands.CheckFailure( message=Lang.lang(self, 'role_user_no_modrole', role.name)) if need_mod_role_id not in [r.id for r in ctx.author.roles]: raise commands.MissingRole(need_mod_role_id) if action.lower() == "add": if role in user.roles: await ctx.send( Lang.lang(self, 'role_user_already', utils.get_best_username(user), role)) return await add_user_role(user, role) await ctx.send( Lang.lang(self, 'role_user_added', role, utils.get_best_username(user))) elif action.lower() == "del": if role not in user.roles: await ctx.send( Lang.lang(self, 'role_user_doesnt_have', utils.get_best_username(user), role)) return await remove_user_role(user, role) await ctx.send( Lang.lang(self, 'role_user_removed', role, utils.get_best_username(user))) else: raise commands.BadArgument(Lang.lang(self, 'role_user_bad_arg')) await utils.log_to_admin_channel(ctx)
async def enable(self, ctx, command, user: discord.Member = None): if user is None: user = ctx.author if user != ctx.author and not permChecks.check_full_access(ctx.author): raise commands.MissingAnyRole(*Config().FULL_ACCESS_ROLES) result = self.bot.ignoring.remove_user_command(user, command) if result == IgnoreEditResult.Success: await ctx.message.add_reaction(Lang.CMDSUCCESS) elif result == IgnoreEditResult.Not_in_list: await ctx.message.add_reaction(Lang.CMDERROR) await ctx.send(Lang.lang(self, 'user_cmd_not_blocked', command, utils.get_best_username(user))) await utils.log_to_admin_channel(ctx)
async def collecting_phase(self): assert len(self.participants) > 1 msg = [utils.get_best_username(el.user) for el in self.participants] msg = utils.format_andlist(msg, ands=Lang.lang(self, "and")) msg = Lang.lang(self, "list_participants", msg) await self.channel.send(msg) shuffled = self.participants.copy() utils.trueshuffle(shuffled) for i in range(len(self.participants)): self.participants[i].assign(shuffled[i]) for el in self.participants: await el.init_dm()
async def on_command_error(ctx, error): """Error handling for bot commands""" # No command or ignoring list handling if isinstance( error, (commands.CommandNotFound, commands.DisabledCommand)): return if isinstance(error, ignoring.UserBlockedCommand): await ctx.send("User {} has blocked the command.".format( utils.get_best_username(error.user))) # Check Failures elif isinstance(error, (commands.MissingRole, commands.MissingAnyRole)): await ctx.send( "You don't have the correct role for this command.") elif isinstance(error, commands.NoPrivateMessage): await ctx.send("Command can't be executed in private messages." ) elif isinstance(error, commands.CheckFailure): await ctx.send("Permission error.") # User input errors elif isinstance(error, commands.MissingRequiredArgument): await ctx.send("Required argument missing: {}".format( error.param)) elif isinstance(error, commands.TooManyArguments): await ctx.send("Too many arguments given.") elif isinstance(error, commands.BadArgument): await ctx.send("Error on given argument: {}".format(error)) elif isinstance(error, commands.UserInputError): await ctx.send("Wrong user input format: {}".format(error)) # Other errors else: # error handling embed = discord.Embed(title=':x: Command Error', colour=0xe74c3c) # Red embed.add_field(name='Error', value=error) embed.add_field(name='Command', value=ctx.command) embed.add_field(name='Message', value=ctx.message.clean_content) embed.description = '```python\n{}\n```'.format("".join( traceback.TracebackException.from_exception( error).format())) embed.timestamp = datetime.datetime.utcnow() await utils.write_debug_channel(bot, embed) await ctx.send("Error while executing command.")
async def who_mobbing(self, ctx): after_date = (datetime.now(timezone.utc) - timedelta(minutes=30)).replace(tzinfo=None) users = [self.bot.user] messages = await ctx.channel.history(after=after_date).flatten() for message in messages: if message.author not in users: users.append(message.author) bully = random.choice(users) if bully is self.bot.user: text = Lang.lang(self, "bully_msg_self") else: text = Lang.lang(self, "bully_msg", utils.get_best_username(bully)) await ctx.send(text)
async def statuscmd(self, ctx): if self.statemachine.state == State.IDLE: await ctx.send(Lang.lang(self, "not_running")) return elif self.statemachine.state == State.REGISTER: await ctx.send(Lang.lang(self, "status_aborting")) return elif self.statemachine.state == State.DELIVER: await ctx.send(Lang.lang(self, "status_delivering")) return elif self.statemachine.state == State.ABORT: await ctx.send(Lang.lang(self, "status_aborting")) return assert self.statemachine.state == State.COLLECT waitingfor = [] for el in self.participants: if el.chosen is None: waitingfor.append(utils.get_best_username(el.user)) wf = utils.format_andlist(waitingfor, ands=Lang.lang(self, "and"), emptylist=Lang.lang(self, "nobody")) await ctx.send(Lang.lang(self, "waiting_for", wf))
def _user_name_check_func(user: discord.User): """The user name check function""" return utils.get_best_username(user)
async def identify(self, ctx, *args): await ctx.channel.send("I will call you {}.".format( utils.get_best_username(ctx.message.author)))
def get_best_username(config, user, mention=False): if mention: s = user.mention else: s = utils.get_best_username(user) return "{}{}".format(uemoji(config, user), s)
async def registering_phase(self): self.logger.debug("Starting registering phase") self.postgame = False reaction = Lang.lang(self, "reaction_signup") to = self.config["register_timeout"] msg = Lang.lang( self, "registering", reaction, to, utils.sg_pl(to, Lang.lang(self, "minute_sg"), Lang.lang(self, "minute_pl"))) msg = await self.channel.send(msg) try: await msg.add_reaction(Lang.lang(self, "reaction_signup")) except HTTPException: # Unable to add reaction, therefore unable to begin the game await self.channel.send("PANIC") self.statemachine.state = State.ABORT return await asyncio.sleep(to * 60) # Consume signup reactions self.participants = [] await msg.remove_reaction(Lang.lang(self, "reaction_signup"), self.bot.user) signup_msg = discord.utils.get(self.bot.cached_messages, id=msg.id) reaction = None for el in signup_msg.reactions: if el.emoji == Lang.lang(self, "reaction_signup"): reaction = el break candidates = [] blocked = [] if reaction is not None: async for user in reaction.users(): if user == self.bot.user: continue if self.bot.dm_listener.is_blocked(user): blocked.append(utils.get_best_username(user)) else: candidates.append(user) if blocked: blocked = utils.format_andlist(blocked, ands=Lang.lang(self, "and")) await self.channel.send(Lang.lang(self, "dmblocked", blocked)) self.statemachine.state = State.ABORT return for el in candidates: try: self.participants.append(Participant(self, el)) except RuntimeError: await msg.add_reaction(Lang.CMDERROR) self.statemachine.state = State.ABORT return players = len(self.participants) if players <= 1: await self.channel.send(Lang.lang(self, "no_participants")) self.statemachine.state = State.ABORT else: self.statemachine.state = State.COLLECT
async def init_dm(self): self.plugin.logger.debug("Sending init DM to {}".format(self.user)) await self.send( Lang.lang(self.plugin, "ask_for_entry", utils.get_best_username(self.assigned.user)))