async def set_server_active(self, ctx): """ Removes all server-active characters and sets the character as active on the current server. Raises NoPrivateMessage() if not in a server. """ if ctx.guild is None: raise NoPrivateMessage() guild_id = str(ctx.guild.id) owner_id = str(ctx.author.id) # unset anyone else that might be active on this server unset_result = await ctx.bot.mdb.characters.update_many( { "owner": owner_id, "active_guilds": guild_id }, {"$pull": { "active_guilds": guild_id }}) # set us as active on this server await ctx.bot.mdb.characters.update_one( { "owner": owner_id, "upstream": self._upstream }, {"$addToSet": { "active_guilds": guild_id }}) if guild_id not in self._active_guilds: self._active_guilds.append(guild_id) return SetActiveResult( did_unset_server_active=unset_result.modified_count > 0)
def convert_roles(self, ctx: Context, full_message: str): guild = ctx.message.guild if not guild: raise NoPrivateMessage() args = shlex.split(full_message) results = [] try: for arg in args: match = ID_MATCH.match(arg) or ROLE_ID_MATCH.match(arg) if match: result = guild.get_role(int(match.group(1))) else: result = guild.get_role(self.role_name_cache[guild.id].get( arg.upper())) if result is None: raise BadArgument(f'Role "{arg}" not found.') results.append(result) except BadArgument: if not results: result = guild.get_role(self.role_name_cache[guild.id].get( full_message.upper())) if result is None: raise results.append(result) else: raise return results
async def block_dms(ctx): result = not (ctx.guild is None) if result is False: raise NoPrivateMessage() return result
def no_pm(ctx): """A :func:`.check` that indicates this command must only be used in a guild context only. Basically, no private messages are allowed when using the command. This check raises a special exception, :exc:`.NoPrivateMessage` that is derived from :exc:`.CheckFailure`. """ if ctx.guild: return True raise NoPrivateMessage('This command cannot be used in private messages.')
async def predicate(ctx): if ctx.guild is None: raise NoPrivateMessage() permissions = ctx.guild.me.guild_permissions missing = [perm for perm, value in kwargs.items() if getattr(permissions, perm, None) != value] if not missing: return True raise BotMissingPermissions(missing)
async def convert(self, ctx, argument): if ctx.guild is None: raise NoPrivateMessage() member = None user_id = None username = None discrim = None match = ID_MATCHER.match(argument) if match is not None: argument = match.group(1) try: user_id = int(argument) member = ctx.guild.get_member(user_id) except ValueError: parts = argument.split('#') if len(parts) == 2 and parts[1].isnumeric(): username = parts[0] discrim = parts[1] elif len(parts) == 1: username = argument if member is not None: return member if user_id is not None: a = await getMessageAuthor(ctx, ctx.guild.id, user_id) if a is not None: member = await Utils.get_member(ctx.bot, ctx.guild, a.id) if member is not None: return member else: raise UserNotFound(argument) for m in ctx.guild.members: if username is not None: potential = None if (discrim is None and (m.name.startswith(username) or (m.nick is not None and m.nick.startswith(username)))) or \ (discrim is not None and (m.name == username or m.nick == username)): potential = m if potential is not None: if member is not None: raise TranslatedBadArgument( 'multiple_potential_targets', ctx) member = potential if discrim is not None: break if member is not None: return member return m
def convert_to_role(ctx, argument): guild = ctx.guild if not guild: raise NoPrivateMessage() match = _get_id_match(argument) or re.match(r'<@&([0-9]+)>$', argument) if match: result = guild.get_role(int(match.group(1))) else: result = discord.utils.get(guild._roles.values(), name=argument) if result is None: raise BadArgument('Role "{}" not found.'.format(argument)) return result
async def convert(self, ctx, argument) -> discord.Role: guild = ctx.guild if not guild: raise NoPrivateMessage() match = self._get_id_match(argument) or re.match( r'<@&([0-9]+)>$', argument) if match: result = discord.utils.get(guild.roles, id=int(match.group(1))) else: result = discord.utils.find( lambda r: r.name.lower() == argument.lower(), guild.roles) if result is None: raise BadArgument('Role "{}" not found.'.format(argument)) return result
async def unset_server_active(self, ctx): """ If this character is active on the contextual guild, unset it as the guild active character. Raises NoPrivateMessage() if not in a server. """ if ctx.guild is None: raise NoPrivateMessage() guild_id = str(ctx.guild.id) # if and only if this character is active in this server, unset me as active on this server unset_result = await ctx.bot.mdb.characters.update_one( {"owner": str(ctx.author.id), "upstream": self._upstream}, {"$pull": {"active_guilds": guild_id}} ) try: self._active_guilds.remove(guild_id) except ValueError: pass return SetActiveResult(did_unset_server_active=unset_result.modified_count > 0)
async def say(self, ctx: Context, channel: TextChannel, *, message): """ Sends a message in any channel in this server that the bot has access\ to. Available only to server administrators. """ if isinstance(ctx.channel, DMChannel): if not await self.bot.is_owner(ctx.author): raise NoPrivateMessage( message="You can't use this command in a DM!") if isinstance(ctx.channel, TextChannel): if ctx.guild.id != channel.guild.id: raise BadArgument( message="You can't send messages outside of your server!") if not ctx.channel.permissions_for(ctx.author).administrator: raise MissingPermissions(["administrator"]) await channel.send(content=message)
def guild_only_check(ctx): if ctx.guild is None: raise NoPrivateMessage() return True
def cog_check(self, ctx): if ctx.guild is None: raise NoPrivateMessage() return True
def check_on_guild(self, ctx: Context) -> bool: """Check if the context is in a guild.""" if ctx.guild is None: raise NoPrivateMessage("This command cannot be used in private messages.") return True
def predicate(ctx: DiscordContext): if ctx.guild is None: raise NoPrivateMessage( 'This command cannot be used in private messages.') return True
def cog_check(self, ctx: commands.Context): """Cog wide check, which disallows commands in DMs.""" if not ctx.guild: raise NoPrivateMessage() return True
def check_on_guild(self, ctx: Context): if ctx.guild is None: raise NoPrivateMessage( "This command cannot be used in private messages.") return True