async def set_channel(ctx: Context, coginfo: CogInfo, channel: Optional[TextChannel]) -> str: """Set which channel should be used for welcome messages.""" if coginfo.bot: bot: MrFreeze = coginfo.bot else: raise InsufficientCogInfo() # Get old channel, do early return if the channels are the same. old_channel = await get_welcome_channel(ctx.guild, bot) if old_channel.channel == channel and old_channel.type == ChannelReturnType.WELCOME: return f"{ctx.author.mention} The welcome messages are already being posted to {old_channel}" # Try to change the channel, give responses accordingly new_value = channel.id if channel else channel channel_set = bot.settings.set_welcome_channel_by_id(ctx.guild, new_value) if not channel_set: return f"{ctx.author.mention} Sorry, something went wrong when setting the welcome messages channel." else: new_channel = await get_welcome_channel(ctx.guild, bot) msg = f"{ctx.author.mention} Great success! Welcome messages will no longer be posted to {old_channel}, " msg += f"from now on they will be posted in {new_channel}" return f"{ctx.author.mention} Great success! Welcome messages will now be posted to {new_channel}!"
async def get_channel(ctx: Context, coginfo: CogInfo) -> str: """Get which channel is currently used for welcome messages.""" if coginfo.bot: bot: MrFreeze = coginfo.bot else: raise InsufficientCogInfo() channel = await get_welcome_channel(ctx.guild, bot) return f"{ctx.author.mention} The current channel for welcome messages is {channel}"
def say_goodbye(member: Member, coginfo: CogInfo) -> Embed: """Log when a member leaves the server.""" if coginfo.bot and coginfo.default_goodbye_template: bot: MrFreeze = coginfo.bot default_template: Template = coginfo.default_goodbye_template else: raise InsufficientCogInfo() template_string = bot.settings.get_leave_message(member.guild) template = Template(template_string) if template_string else default_template return create_embed(member, template)
def welcome_member(member: Member, coginfo: CogInfo) -> str: """Log when a member joins the chat.""" if coginfo.bot and coginfo.default_welcome_template: bot: MrFreeze = coginfo.bot default_template: Template = coginfo.default_welcome_template else: raise InsufficientCogInfo() template_string = bot.settings.get_welcome_message(member.guild) template = Template( template_string) if template_string else default_template return fill_template(member, template)
async def region_antarctica(ctx: Context, cog: CogInfo, spelling: str) -> bool: """ Analyze arguments to see if user tried to set region to Antarctica. If they did, the method will return True, banish them and send some snarky reply. Otherwise it returns False. """ if cog.bot and cog.logger: bot = cog.bot logger = cog.logger else: raise InsufficientCogInfo() # Check if the user is on an indefinite banish. mute_status = mute_db.mdb_fetch(bot, ctx.author) indefinite_mute = mute_status and not mute_status[0].until # User confirmed to have tried to set region to Antarctica # Initiating punishment reply = f"{ctx.author.mention} is a filthy *LIAR* claiming to live in Antarctica. " if indefinite_mute: reply += "Which they're currently stuck in, FOREVER!" elif spelling == "antarctica": duration = datetime.timedelta(minutes=10) end_date = datetime.datetime.now() + duration duration = bot.parse_timedelta(duration) reply += "I'll give them what they want and banish them to that " reply += "frozen hell for ten minutes!" else: duration = datetime.timedelta(minutes=20) end_date = datetime.datetime.now() + duration duration = bot.parse_timedelta(duration) reply += f"What's more, they spelled it *{spelling.capitalize()}* " reply += "instead of *Antarctica*... 20 minutes in Penguin school " reply += "ought to teach them some manners!" # Carry out the banish with resulting end date if not indefinite_mute: error = await mute_db.carry_out_banish(bot, ctx.author, logger, end_date) if isinstance(error, Exception): reply = f"{ctx.author.mention} is a filthy *LIAR* claiming to live in Antarctica. " reply += "Unfortunately there doesn't seem to be much I can do about that. Not sure " reply += "why. Some kind of system malfunction or whatever." await ctx.send(reply) return True
async def set_region(ctx: Context, cog: CogInfo, args: Tuple[str, ...]) -> None: """ Set the region of a user based on what they said. Determine which region the user tried to set using the !region command, then set that region (if found) and send an appropriate response. If the region isn't found don't set a role, just send an appropriate response. """ if cog.regions: regions = cog.regions[ctx.guild.id] else: raise InsufficientCogInfo() author_roles = [ i.id for i in ctx.author.roles if i.name not in regions.keys() ] all_args = " ".join(args) found_region = False valid_region = True for region in regional_aliases: if found_region: break for alias in regional_aliases[region]: if alias in all_args: author_roles.append(regions[region]) valid_region = regions[region] is not None new_role_name = region found_region = True break if found_region and valid_region: new_roles = [discord.Object(id=i) for i in author_roles] await ctx.author.edit(roles=new_roles) msg = f"{ctx.author.mention} You've been assigned a new role, " msg += f"welcome to {new_role_name}!" else: msg = f"{ctx.author.mention} I couldn't find a match for {' '.join(args)}.\n" msg += "Please check your spelling or type **!region list** for a list of " msg += "available regions." await ctx.send(msg)
async def banish(ctx: Context, coginfo: CogInfo, template_engine: TemplateEngine, args: Tuple[str, ...]) -> None: """Carry out one or more banishes.""" if coginfo.bot: bot: MrFreeze = coginfo.bot else: raise InsufficientCogInfo() # Parse targetted users bot_mentioned = bot.user in ctx.message.mentions self_mentioned = ctx.author in ctx.message.mentions mentions = ctx.message.mentions mods = [ u for u in mentions if u.guild_permissions.administrator and u != bot.user ] users = [ u for u in mentions if not u.guild_permissions.administrator and u != bot.user ] if bot_mentioned or self_mentioned or mods: # Illegal banish, prepare silly response. if bot_mentioned and len(mentions) == 1: logger.debug("Setting template to MuteResponseType.FREEZE") template = MuteResponseType.FREEZE fails_list = [bot.user] elif bot_mentioned and self_mentioned and len(mentions) == 2: logger.debug("Setting template to MuteResponseType.FREEZE_SELF") template = MuteResponseType.FREEZE_SELF fails_list = [bot.user, ctx.author] elif bot_mentioned: logger.debug("Setting template to MuteResponseType.FREEZE_OTHERS") template = MuteResponseType.FREEZE_OTHERS fails_list = mods + users elif self_mentioned and len(mentions) == 1: logger.debug("Setting template to MuteResponseType.SELF") template = MuteResponseType.SELF fails_list = mods elif mods and len(mentions) == 1: logger.debug("Setting template to MuteResponseType.MOD") template = MuteResponseType.MOD fails_list = mods elif mods: logger.debug("Setting template to MuteResponseType.MODS") template = MuteResponseType.MODS fails_list = mods else: logger.warn("Setting template to MuteResponseType.INVALID") logger.warn( f"bot_mentioned={bot_mentioned}, self_mentioned={self_mentioned}" ) logger.warn(f"{len(mentions)} mentions={mentions}") logger.warn(f"{len(mods)} mods={mods}") logger.warn(f"{len(users)} users={users}") template = MuteResponseType.INVALID fails_list = mentions banish_template = template_engine.get_template(ctx.invoked_with, template) mention_fails = default.mentions_list(fails_list) if banish_template: msg = banish_template.substitute(author=ctx.author.mention, fails=mention_fails) await ctx.send(msg) else: # Legal banish, attempt banish. msg = await attempt_banish(ctx, coginfo, template_engine, users, args) await ctx.send(msg)